AdSense

網頁

2024/3/27

Jenkins Pipeline checkout/clone GitLab repo

Jenkins Pipeline將GitLab repo程式碼抓取下來的設定方式如下。


範例環境:

  • Jenkins 2.440.2


設定Credential

在Jenkins首頁點選左側的[Manage Jenkins]。

在[Manage Jenkins]頁面,點選[Security]區塊下的[Credentials]。



在[Credentials]頁面,點選[Stores scoped to Jenkins]區塊下的[System]。

在[System]頁面,點選[Global credentials (unrestricted)]。



在[Global credentials (unrestricted)]頁面,點選右上方的[Add Credentials]按鈕。



在[New credentials]頁面,

  • [Kind]欄位 - 選擇[Username with password]、
  • [Scope]欄位 - 選擇[Global (Jenkins, nodes, all child items, etc)]、
  • [Username]欄位 - 填入GitLab帳號名稱。
  • [Passord]欄位 - 填入GitLab帳號的登入密碼。

其餘欄位維持預設,按[Create]按鈕建立。



建立後Jenkins便會為credential產生一個[ID],待會作為credentialsId的值。




Pipeline script

在Pipeline的Pipeline script設定Declarative Pipeline如下,使用Pipeline: SCM Step plugincheckout step。

  • scmGit - 用來提供Git基本操作,接收以下參數:
    • branches - 要建制專案的分支名稱。
    • userRemoteConfigs - 設定要checkout的遠端repo參數。
      • credentialsId - checkout repo的憑證。
      • url - repo url。
pipeline {
    agent any
    stages {
        stage('Git Checkout') {
            steps {
                checkout scmGit(
                    branches: [[name: 'main']],
                    userRemoteConfigs: [
                        [ 
                            credentialsId: 'e8c79d5a-33b5-498b-a88c-92da4a8406c8',
                            url: 'https://gitlab.com/p4843213/go-demo.git' 
                        ]
                    ]
                )
            }
        }
    }
}



或是用Git plugingit step。

pipeline {
    agent any
    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'main',
                    credentialsId: 'e8c79d5a-33b5-498b-a88c-92da4a8406c8',
                    url: 'https://gitlab.com/p4843213/go-demo.git'
            }
        }
    }
}


測試

點選Pipeline項目的[Build Now]開始建置,然後檢視[Console Output]如下:

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline-1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Git Checkout)
[Pipeline] checkout
The recommended git tool is: NONE
using credential e8c79d5a-33b5-498b-a88c-92da4a8406c8
 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/pipeline-1/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://gitlab.com/p4843213/go-demo.git # timeout=10
Fetching upstream changes from https://gitlab.com/p4843213/go-demo.git
 > git --version # timeout=10
 > git --version # 'git version 2.40.1'
using GIT_ASKPASS to set credentials 
 > git fetch --tags --force --progress -- https://gitlab.com/p4843213/go-demo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse origin/main^{commit} # timeout=10
Checking out Revision cf00dc26acecbc259315b3e3f01c7250e1e8ea73 (origin/main)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f cf00dc26acecbc259315b3e3f01c7250e1e8ea73 # timeout=10
Commit message: "fix"
 > git rev-list --no-walk cf00dc26acecbc259315b3e3f01c7250e1e8ea73 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

前往Jenkins Pipline的workspace目錄可看到clone下來的專案原始碼。

$ ls /var/lib/jenkins/workspace/pipeline-1
README.md  cmd  go.mod


沒有留言:

AdSense