簡體   English   中英

嘗試使用 Jenkins 管道腳本克隆 github 存儲庫時拋出“git:找不到命令”

[英]Trying to clone a github repository using Jenkins pipeline script throws "git: command not found"

CI/CD 構建 jenkins 管道的新功能。

用例:將文件從 git 存儲庫復制到 AWS S3 存儲桶。 我們在 Kube.netes pod 中運行 Jenkins 個代理。

到目前為止,一切都很好。 我能夠連接到 Jenkins 管道中的 Hashicorp 保險庫,並能夠成功驗證我們的 aws 帳戶。 但是當我嘗試使用“git”命令克隆所需的 repo 時,它不喜歡 git 命令。

不確定如何在 jenkins 代理上安裝 git 作為 jenkins 管道的一部分

這是成功運行的代碼,除了“git 命令”如何在使用“git”命令克隆存儲庫之前在此 jenkins 管道中下載 git

   @Library('enterprise-pipeline-library@master') _
  //Set AWS and Jenkins build properties/parameters
  def agent = "service-deployment-${UUID.randomUUID().toString()}"

  //Container templates

  podTemplates(

      label: agent,

      containers: [
            containerTemplate(name: 'aws-cli', image: 'artifactory.xxx/amazon/aws-cli:2.2.32', 
       ttyEnabled: true, command: 'cat'),
         containerTemplate(name: 'vault', image: 'artifactory.xxx/ease/hashicorp-vault:latest', ttyEnabled: true, command: 'cat')
],              volumes: [hostPathVolume(hostPath: '/var/run/docker.sock', 
     mountPath: 
        '/var/run/docker.sock')],
        imagePullSecrets: ['xxx']
   ){ 
        node(agent) {
                stage("Checkout Code") {
                 echo "hello"
        }
            withCredentials([
                string(credentialsId: 'VAULT_ROLE_ID_UAT', variable: 'VAULT_ROLE_ID_UAT'),
                string(credentialsId: 'VAULT_SECRET_ID_UAT', variable: 'VAULT_SECRET_ID_UAT'),
                string(credentialsId: 'VAULT_ROLE_ID_PCI_UAT', variable: 
              'VAULT_ROLE_ID_PCI_UAT'),
                string(credentialsId: 'VAULT_SECRET_ID_PCI_UAT', variable: 
          'VAULT_SECRET_ID_PCI_UAT'
            ]) {

            stage("Vault Creds for source environment") {                   

                    container('vault') {

                        sh '''

                        export VAULT_TLS_SERVER_NAME=hcvault.xxx
                        export VAULT_ADDR=https://xxxx
                        export VAULT_NAMESPACE=cloud 
                        export ROLE_ID=${VAULT_ROLE_ID_PCI_UAT}
                        export SECRET_ID=${VAULT_SECRET_ID_PCI_UAT}
                        AWS_Account_ID=xxx
                        VAULT_CRED=vault.cred
                        vault write auth/approle/login role_id=$ROLE_ID secret_id=$SECRET_ID  > $VAULT_CRED 2> /dev/null
                        export VAULT_TOKEN=$( awk '/token / {print $2}' $VAULT_CRED ) 
                        AWS_CRED=aws.cred
                        vault read aws/creds/${AWS_Account_ID}-VaultAssumeRole  > $AWS_CRED 2> /dev/null 
                        echo "Removing Vault Credentials..."
                        rm -f $VAULT_CRED 

                        '''
                    } //container                   

             } //stage vault creds

            stage('Sending the files to S3 bucket') {
                container('aws-cli') {                

                        sh '''                          

                        JOB_TITLE=`echo ${JOB_BASE_NAME} | sed 's/ /_/g'`                          

                        set +x

                        AWS_CRED=aws.cred
                        export AWS_ACCESS_KEY_ID=$( awk '/access_key / {print $2}' $AWS_CRED )
                        export AWS_SECRET_ACCESS_KEY=$( awk '/secret_key / {print $2}' $AWS_CRED )
                        export AWS_SESSION_TOKEN=$( awk '/security_token / {print $2}' $AWS_CRED )
                        env | grep AWS

                        set -x

                        git branch: 'main', credentialsId: 'system-id', url: 'https://gitlab.xxx/AMAZON_myaccount/myproject.git'

                        if [ "$sourceEnv" == "UAT_PCI" ]
                        then

                            echo "copying the file to the UAT USE1 bucket"                           

                            #aws s3 cp 7day_rate/pfm_data.json s3://uatpci-pfm-data/data/test2/                              

                            echo "copying the file to the UAT USW2 bucket"
                            #aws s3 cp 7day_rate/pfm_data.json s3://uatpci-pfm-data-usw2/data/test/                          

                        fi 

                        '''

                    }
                }
        } // withcredentials
} // node agent

}

這是抱怨找不到“git”命令的錯誤。 我知道在我們使用 git 分支命令克隆 repo 之前必須下載 git。 但是我怎樣才能在 jenkins 代理上靜默安裝 git?

在此處輸入圖像描述

您可以按照此處的 git 安裝說明進行操作https://github.com/git-guides/install-git#debianubuntu

您還需要更改 git 命令。 你的代碼有:

git branch: 'main', credentialsId: 'system-id', url: 'https://gitlab.xxx/AMAZON_myaccount/myproject.git'

這是 jenkins 管道語法,所以它不應該在 shell 腳本中。

我會在 shell 步驟之前移動 git shell,因為每個 shell 步驟都會打開一個新終端 session,因此您在 shell 步驟中設置的變量僅在調用該步驟期間存在。 如果您將代碼分成 shell 步驟、git 步驟和 shell 步驟,則必須設置環境變量兩次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM