简体   繁体   English

尝试使用 Jenkins 管道脚本克隆 github 存储库时抛出“git:找不到命令”

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

New to CI/CD building jenkins pipeline. CI/CD 构建 jenkins 管道的新功能。

Use case: copy files from a git repo to AWS S3 bucket.用例:将文件从 git 存储库复制到 AWS S3 存储桶。 We run Jenkins agents in Kube.netes pods.我们在 Kube.netes pod 中运行 Jenkins 个代理。

So far so good.到目前为止,一切都很好。 I am able to connect to Hashicorp vault in the Jenkins pipeline and able to authenticate to our aws account successfully.我能够连接到 Jenkins 管道中的 Hashicorp 保险库,并能够成功验证我们的 aws 帐户。 But when I try to use "git" command to clone the desired repo, it doesn't like git command.但是当我尝试使用“git”命令克隆所需的 repo 时,它不喜欢 git 命令。

Not sure how to install git on the jenkins agent as part of the jenkins pipeline不确定如何在 jenkins 代理上安装 git 作为 jenkins 管道的一部分

this is the successfully running code except the "git command" How to download git in this jenkins pipeline before I clone a repository using 'git" commands这是成功运行的代码,除了“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

} }

This is the error complaining about "git" command not found.这是抱怨找不到“git”命令的错误。 I understand that git has to be downloaded before we use the git branch command to clone the repo.我知道在我们使用 git 分支命令克隆 repo 之前必须下载 git。 But how can I do a silent install of git on the jenkins agent?但是我怎样才能在 jenkins 代理上静默安装 git?

在此处输入图像描述

You can follow the git installation instructions from here https://github.com/git-guides/install-git#debianubuntu您可以按照此处的 git 安装说明进行操作https://github.com/git-guides/install-git#debianubuntu

You will also need to change the git command.您还需要更改 git 命令。 Your code has:你的代码有:

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

This is jenkins pipeline syntax, so it shouldn't be inside a shell script.这是 jenkins 管道语法,所以它不应该在 shell 脚本中。

I would move the git shell before the shell step, because each shell step opens a new terminal session so the variables you set up inside a shell step only exist during the invocation of that step.我会在 shell 步骤之前移动 git shell,因为每个 shell 步骤都会打开一个新终端 session,因此您在 shell 步骤中设置的变量仅在调用该步骤期间存在。 If you broke your code into a shell step, a git step and then a shell step, you would have to setup your env variables twice.如果您将代码分成 shell 步骤、git 步骤和 shell 步骤,则必须设置环境变量两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM