是否无法在Jenkinsfile中签出其他分支? [英] Is it impossible to checkout a different branch in Jenkinsfile?

查看:146
本文介绍了是否无法在Jenkinsfile中签出其他分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BitBucket上有两个分支:masterdevelop.我还在Jenkins服务器上配置了一个BitBucket团队文件夹作业,以构建该存储库.在develop分支上,有以下Jenkinsfile:

I have two branches on BitBucket: master and develop. I've also got a BitBucket Team Folder job configured on my Jenkins server to build that repository. On the develop branch there's the following Jenkinsfile:

node {
    stage('Checkout') {
        checkout scm
    }

    stage('Try different branch') {
        sh "git branch -r"
        sh "git checkout master"
    }
}

Jenkins运行它时,当尝试结帐master时,构建失败:

When Jenkins runs it, the build fails when it attempts to checkout master:

[Pipeline] stage
[Pipeline] { (Try different branch)
[Pipeline] sh
[e_jenkinsfile-tests_develop-4R65E2H6B73J3LB52BLACQOZLBJGN2QG22IPONX3CV46B764LAXA] Running shell script
+ git branch -r
  origin/develop
[Pipeline] sh
[e_jenkinsfile-tests_develop-4R65E2H6B73J3LB52BLACQOZLBJGN2QG22IPONX3CV46B764LAXA] Running shell script
+ git checkout master
error: pathspec 'master' did not match any file(s) known to git.
[Pipeline] }

我曾期望git branch -r命令同时打印出origin/masterorigin/develop,但是由于某些原因,它只打印出origin/masterorigin/develop.

I had expected the git branch -r command to print out both origin/master and origin/develop, but for some reason it only prints the latter.

我已经阅读并尝试提出实现此目的的任何方法:例如,我尝试为Jenkins安装SSH代理插件,并将Jenkinsfile更改为:

I've read around and tried to come up with any ways to do this: For example, I tried installing the SSH Agent Plugin for Jenkins and changed the Jenkinsfile to:

node {
    stage('Checkout') {
        checkout scm
    }

    stage('Try different branch') {
        sshagent(['Bitbucket']) {
            sh "git branch -r"
            sh "git checkout master"
        }
    }
}

但是它仍然没有找到origin/master.更糟糕的是,SSH代理似乎在尝试检出master之前被杀死:

But it still doesn't appear to find origin/master. What's worse, the SSH agent seems to be killed before it attempts to checkout master:

[Pipeline] { (Try different branch)
[Pipeline] sshagent
[ssh-agent] Using credentials ThomasKasene (Used to communicate with Bitbucket)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent]   Exec ssh-agent (binary ssh-agent on a remote machine)
$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-M6pIguCUpAV4/agent.11899
SSH_AGENT_PID=11902
$ ssh-add /var/jenkins_home/workspace/e_jenkinsfile-tests_develop-4R65E2H6B73J3LB52BLACQOZLBJGN2QG22IPONX3CV46B764LAXA@tmp/private_key_2394129657382526146.key
Identity added: /var/jenkins_home/workspace/e_jenkinsfile-tests_develop-4R65E2H6B73J3LB52BLACQOZLBJGN2QG22IPONX3CV46B764LAXA@tmp/private_key_2394129657382526146.key (/var/jenkins_home/workspace/e_jenkinsfile-tests_develop-4R65E2H6B73J3LB52BLACQOZLBJGN2QG22IPONX3CV46B764LAXA@tmp/private_key_2394129657382526146.key)
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
[e_jenkinsfile-tests_develop-4R65E2H6B73J3LB52BLACQOZLBJGN2QG22IPONX3CV46B764LAXA] Running shell script
+ git branch -r
  origin/develop
[Pipeline] sh
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 11902 killed;
[ssh-agent] Stopped.
[e_jenkinsfile-tests_develop-4R65E2H6B73J3LB52BLACQOZLBJGN2QG22IPONX3CV46B764LAXA] Running shell script
+ git checkout master
error: pathspec 'master' did not match any file(s) known to git.
[Pipeline] }

我最终的计划是将某些内容提交给develop,然后将其合并到master,但是到目前为止,我的运气很少.有没有人有可能的解决方案或解决方法?

My eventual plan is to commit something to develop and then merge it into master, but so far I have had very little luck. Has anybody got a possible solution or workaround?

PS:这似乎只是Jenkinsfile中的一个问题.我有一项自由式工作,可以完成与我想要的工作类似的工作,并且效果很好.

PS: This only seems to be a problem in Jenkinsfile; I have a freestyle job that does something similar to what I want, and it works fine.

推荐答案

经过数小时的反复试验,我提出了一个可能的解决方案.它部分基于Matt的回答,但我不得不对其进行更改以使其起作用.

After some hours of trial and error, I came up with a possible solution. It builds partly on Matt's answer, but I had to alter it to make it work.

Matt在本质上是正确的:checkout scm根本不够灵活,无法执行我需要的操作,因此我不得不使用GitSCM对其进行自定义.主要景点是:

Matt was correct in the essentials: checkout scm simply wasn't flexible enough to allow me to do what I needed, so I had to use GitSCM to customize it. The major points of interest are:

  • 添加了扩展名LocalBranch以确保我签出到实际分支,而不仅仅是分离的HEAD.
  • 添加了扩展名WipeWorkspace,以删除工作空间中的所有内容并强制执行完整克隆.我不认为这是解决我的问题的一部分,但仍然很方便.
  • 由于存储库是私有的,因此使用credentialsId属性指定了SSH凭据.
  • Added extension LocalBranch to make sure I check out to an actual branch, and not just a detached HEAD.
  • Added extension WipeWorkspace to delete everything in the workspace and force a complete clone. I don't think this was a part of the solution to my question, but it was still handy to have.
  • Specified the SSH credentials using the credentialsId property since the repository is private.

无论出于何种原因,在执行checkout步骤时,它只会检出分支,而不会将其设置为跟踪远程分支.在找到更优雅的解决方案之前,我不得不手动执行此操作.

For whatever reason, when the checkout step is executed, it only checks out the branch, but does not set it to track the remote branch. Until I find a more elegant solution, I had to do this manually.

完成所有操作后,只要将它们包含在sshagent步骤中,我就可以使用常规的sh "git checkout master"甚至sh "git push".

After all that was done, I could use regular sh "git checkout master" and even sh "git push", as long as I enclosed them in an sshagent step.

我在下面添加了一个生成的Jenkinsfile的工作示例,但是请记住,不应将它用于生产环境中,因为它仍处于起步阶段.硬编码的版本号,例如,不检查您位于哪个分支.

I added a working example of the resulting Jenkinsfile below, but please keep in mind that it shouldn't be used for anything close to production as it's still very much in its infancy; hardcoded version numbers and no checks for which branch you're in, for example.

node {
    mvnHome = tool 'Maven'
    mvn = "${mvnHome}/bin/mvn"

    stage('Checkout') {
        checkout([
            $class: 'GitSCM',
            branches: scm.branches,
            extensions: scm.extensions + [[$class: 'LocalBranch'], [$class: 'WipeWorkspace']],
            userRemoteConfigs: [[credentialsId: 'Bitbucket', url: 'git@bitbucket.org:NAVFREG/jenkinsfile-tests.git']],
            doGenerateSubmoduleConfigurations: false
        ])
    }

    stage('Release') {
        // Preparing Git
        sh "git branch -u origin/develop develop"
        sh "git config user.email \"jenkins@thomaskasene.com\""
        sh "git config user.name \"Jenkins\""

        // Making and committing new verison
        sh "${mvn} versions:set -DnewVersion=2.0.0 -DgenerateBackupPoms=false"
        sh "git commit -am \"Released version 2.0.0\""

        // Merging new version into master
        sh "git checkout master"
        sh "git merge develop"
        sh "git checkout develop"

        // Making and committing new snapshot version
        sh "${mvn} versions:set -DnewVersion=3.0.0-SNAPSHOT -DgenerateBackupPoms=false"
        sh "git commit -am \"Made new snapshot version 3.0.0-SNAPSHOT\""

        // Pushing everything to remote repository
        sshagent(['Bitbucket']) {
            sh "git push"
            sh "git checkout master"
            sh "git push"
        }
    }
}

这篇关于是否无法在Jenkinsfile中签出其他分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆