繁体   English   中英

Jenkins 管道 ssh 代理 git push 失败

[英]Jenkins pipeline ssh agent git push failes

在我的 jenkins 管道中,我可以很好地克隆存储库,但使用 SSH 代理插件推回标签失败。 我已经确保 github 上的部署密钥具有写访问权限,所以似乎还有一些其他问题......

pipeline {
   agent { docker { image 'node:8' } }

   stages {
      stage('Pull Repo') {
          steps {
            git (
                branch: 'master',
                credentialsId: 'cred-id',
                url: 'github.com:***'
            )
            sshagent(['github-omnia']) {
                sh("git tag -a \"release-2.3.${BUILD_NUMBER}\" -m \"Jenkins built ${BUILD_NUMBER}\"")
                sh("git push --tags")
            }
          }
      }
   }
}

我错过了什么吗?

编辑:这是错误的控制台输出

[ssh-agent] Using credentials git (Access to Github-**)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent]   Exec ssh-agent (binary ssh-agent on a remote machine)
$ docker exec a6cee721d592b10bb94abbde0471d24a4320dcd07362affb1f18454d6ebe028d ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-TI7dNVoYszsC/agent.12
SSH_AGENT_PID=17
Running ssh-add (command line suppressed)
Identity added: /var/jenkins_home/workspace/Build-And-Deploy-***@tmp/private_key_7884642190516796613.key (/var/jenkins_home/workspace/Build-And-Deploy-***@tmp/private_key_7884642190516796613.key)
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
+ git config --global user.email jenkins@***.se
[Pipeline] sh
+ git config --global user.name Jenkins
[Pipeline] sh
+ git remote set-url origin git@github.com:***/***
[Pipeline] sh
+ git tag -a release-2.3.3 -m Jenkins built 3
[Pipeline] sh
+ git push origin --tags
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我一直在寻找一种方法来做到这一点,而无需完全忽略主机验证,也无需修改我的 Jenkins 机器的known_hosts因为我想使用 docker。 我最终得到了这样的结果:

  1. 在 Jenkins 中,创建一个“Secret text”类型的新凭证(我们称之为GITHUB_HOST_KEY ),并将其值设置为主机密钥,例如:
# gets the host for github and copies it. You can run this from
# any computer that has access to github.com (or whatever your
# git server is)
ssh-keyscan github.com | clip
  1. 在您的 Jenkinsfile 中,在使用sshagent之前将字符串保存到known_hosts 这是我的管道; 它需要一个名为master-v5的分支,并生成一个包含许多构建文件的分支master-v5-dist
pipeline {
    agent { docker { image 'node:14' } }

    stages {
        stage('Checkout') {
            steps {
                git branch: 'master-v5',
                    url: 'git@github.com:internetarchive/bookreader.git',
                    credentialsId: 'YOUR_GH_CREDENTIALS'
            }
        }
        stage('Build') { steps { sh 'npm install && npm run build' } }
        stage('Push') {
            steps {
                sh 'git config user.email "foo@bar.com"'
                sh 'git config user.name "Mr. Foo Bar"'
                
                sh 'git add BookReader'
                sh 'git commit -m Build files [ci skip]'

                withCredentials([string(credentialsId: 'GITHUB_HOST_KEY', variable: 'GITHUB_HOST_KEY')]) {
                    sh 'mkdir -p ~/.ssh && echo "$GITHUB_HOST_KEY" >> ~/.ssh/known_hosts'
                }
                sshagent (credentials: ['YOUR_GH_CREDENTIALS']) {
                    sh 'git push -f origin HEAD:master-v5-dist'
                }
            }
        }
    }
}

这可确保您使用受信任的主机密钥,因为您在确定已连接到真正的 github.com 时获得了主机密钥(大概)。

暂无
暂无

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

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