[英]Pull the current branch with Jenkins workflow multibranch plugin
I have set up Jenkins and I am using the Jenkins workflow multibranch plugin. 我已经设置了Jenkins,并且正在使用Jenkins工作流多分支插件。 I have configured it to listen for commits in a GitHub repo for any branch. 我已将其配置为侦听GitHub存储库中任何分支的提交。
This is a sample of the Jenkinsfile
committed in one of the branches: 这是在其中一个分支中提交的Jenkinsfile
的示例:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a GitHub repository
git url: 'git@github.com:Me/my-repo.git', credentialsId: '###'
// Get the maven tool.
// ** NOTE: This 'M3' maven tool must be configured
// ** in the global configuration.
def mvnHome = tool 'M3'
stage 'Build'
sh "${mvnHome}/bin/mvn clean install"
}
When I commit something in this branch a build is triggered. 当我在此分支中提交某些内容时,将触发构建。 The problem is that git checkouts the master branch and not the branch that is building at the moment. 问题是git检出master分支,而不是当前正在建立的分支。
How can I checkout the branch for the current build? 如何检出当前版本的分支?
The solutions so far 到目前为止的解决方案
checkout scm
instead of git url: 'git@github.com:Me/my-repo.git', credentialsId: '###'
This way you will checkout the current branch. 使用checkout scm
代替git url: 'git@github.com:Me/my-repo.git', credentialsId: '###'
这样,您将签出当前分支。 In your Jenkinsfile
, add the name of the branch to checkout with the branch
parameter on the git
line: 在您的Jenkinsfile
,使用git
行上的branch
参数将分支的名称添加到checkout:
git {...}, branch: 'branch-name'
Some build triggers set an environment variable that contains the branch name (eg, Gerrit Trigger Plugin sets GERRIT_BRANCH
), which would allow you to set branch
to that environment variable instead of hard-coding per branch. 一些构建触发器会设置一个包含分支名称的环境变量(例如Gerrit触发器插件设置GERRIT_BRANCH
),这将允许您将branch
设置为该环境变量,而不是对每个分支进行硬编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.