简体   繁体   English

如何在 Jenkins 管道中将 git 标签推送到远程?

[英]How to push git tags to remote in Jenkins pipeline?

I'm building a CI flow for my project, according to the official tutorial mentioned here根据这里提到的官方教程,我正在为我的项目构建 CI 流程

Build a Node.js and React app with npm . 使用 npm 构建 Node.js 和 React 应用程序

There some key points in my flow:我的流程中有一些关键点:

  1. My plan to this flow is building the source code, making a Git tag, and push the tag to remote Git server on the Jenkins server.我对这个流程的计划是构建源代码,制作一个 Git 标签,然后将标签推送到 Jenkins 服务器上的远程 Git 服务器。
  2. The project repo is with public read access and pulled from private Git server through ssh protocol, the server host a GitLab service.项目 repo 具有公共读取访问权限,并通过 ssh 协议从私有 Git 服务器拉取,服务器托管 GitLab 服务。
  3. BlueOcean UI built the CI flow with Multibranch Pipeline and provided me an ssh public key which has the ID: jenkins-generated-ssh-key . BlueOcean UI 使用 Multibranch Pipeline 构建了 CI 流程,并为我提供了一个 ssh 公钥,其 ID 为: jenkins-generated-ssh-key I have committed it to GitLab ssh settings.我已将其提交给 GitLab ssh 设置。
  4. The root directory of my project contains the Jenkinsfile.我的项目的根目录包含 Jenkinsfile。

The file content:文件内容:

pipeline {
    agent { docker 'node:6' }
    stages {
        stage('Build') {
            steps {
                sh 'npm run test'
                sh 'npm run build'
            }
        }
        stage('Deploy') {
            steps {
                sshagent (credentials: ['jenkins-generated-ssh-key']) {
                    sh 'git push --tags'
                }
            }
        }
    }
}

The 'Build' stage contains git committing and tagging steps. “构建”阶段包含 git 提交和标记步骤。 All stuffs for pushing tag are ready.所有推标签的东西都准备好了。

Every time in performing 'Deploy' stage, the sshagent will get killed before pushing git tags.每次在执行“部署”阶段时,sshagent 都会在推送 git 标签之前被杀死。

[Pipeline] sshagent
[ssh-agent] Using credentials fe (jenkins-generated-ssh-key)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent]   Exec ssh-agent (binary ssh-agent on a remote machine)
$ docker exec d2ba3af75c0e5826d3d00676ebba523117a541d9015ee77c74106862d2665025 env ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-dewb3KyPiizc/agent.130
SSH_AGENT_PID=134
$ docker exec d2ba3af75c0e5826d3d00676ebba523117a541d9015ee77c74106862d2665025 env SSH_AGENT_PID=134 SSH_AUTH_SOCK=/tmp/ssh-dewb3KyPiizc/agent.130 ssh-add /var/jenkins_home/workspace/achilles_master-GZG6BNOXJZHXAGELOPVE2O57MTLCFCSFEEMQVJOWKHCSAQDCMS4Q@tmp/private_key_838589255993535437.key
Identity added: /var/jenkins_home/workspace/achilles_master-GZG6BNOXJZHXAGELOPVE2O57MTLCFCSFEEMQVJOWKHCSAQDCMS4Q@tmp/private_key_838589255993535437.key (rsa w/o comment)
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
$ docker exec d2ba3af75c0e5826d3d00676ebba523117a541d9015ee77c74106862d2665025 env SSH_AGENT_PID=134 SSH_AUTH_SOCK=/tmp/ssh-dewb3KyPiizc/agent.130 ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 134 killed;
[ssh-agent] Stopped.
[achilles_master-GZG6BNOXJZHXAGELOPVE2O57MTLCFCSFEEMQVJOWKHCSAQDCMS4Q] Running shell script
+ git push --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.

I have tried adjusting the Jenkinsfile many times, and I found that if the step is 'git push' or 'ssh -T git@git.xxx', it won't get the ssh-agent wrapping the inner steps.我试过多次调整Jenkinsfile,我发现如果步骤是'git push'或'ssh -T git@git.xxx',它不会让ssh-agent包裹内部步骤。 Then I get the permission denied.然后我得到了拒绝的许可。 The ssh-agent always get killed before doing ssh connection but will wrap other steps, which don't require ssh auth, normally. ssh-agent 总是在进行 ssh 连接之前被杀死,但会包装其他步骤,通常不需要 ssh 身份验证。

Host key verification failed.fatal: Could not read from remote repository.主机密钥验证失败。致命:无法从远程存储库读取。

To resolve the above error, we need to manually ssh once or use this parameter要解决上面的错误,我们需要手动ssh一次或者使用这个参数

StrictHostKeyChecking=no StrictHostKeyChecking=no

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

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