简体   繁体   English

使用Jenkins工作流多分支插件拉当前分支

[英]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 到目前为止的解决方案

  1. Use 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: '###'这样,您将签出当前分支。
  2. Use the branch variable - check this for more info: Jenkins Multibranch pipeline: What is the branch name variable? 使用分支变量-有关更多信息,请检查此变量: Jenkins Multibranch管道:分支名称变量是什么?

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.

相关问题 如何在 Jenkins Multibranch Pipeline 插件中从 pom 获取 artifactId 和 version - How to get artifactId and version from pom in a Jenkins Multibranch Pipeline plugin Jenkins pull request plugin抛出一个ArrayIndexOutOfBoundsException异常 - Jenkins pull request plugin throws an ArrayIndexOutOfBoundsException exception 如何通过插件获取当前分支名称 - how to get current branch name through plugin Maven版本插件Jenkins保持将中继线复制到标签而不是分支中 - Maven Release Plugin Jenkins Keeps Copying Trunk into Tag instead of Branch Jenkins + Maven发布插件:release:branch + Git-无法提交文件 - Jenkins + Maven release plugin: release:branch + Git - Unable to commit files 生成SNAPSHOT时不会触发Jenkins Multibranch项目 - Jenkins Multibranch project are not triggered when a SNAPSHOT is built 如何为Jenkins插件打印当前的类路径? - How do I print the current classpath for a Jenkins plugin? 为什么 jenkins 没有在多分支构建中签出源代码 - why the jenkins did not checkout the source code in multibranch build 仍然缺少 jenkins bitbucket 分支源插件的自签名证书 - Still missing self-signed cert for jenkins bitbucket branch source plugin Eclipse git插件和获取当前分支的方法-将ID提交到Java代码版本信息字符串 - Eclipse git plugin and a method to fetch current branch - commit id to Java code version info string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM