如何使用Jenkins预构建分支仅合并我想要合并的分支? [英] How to use Jenkins pre-build branch merging only for branches I want to merge?

查看:428
本文介绍了如何使用Jenkins预构建分支仅合并我想要合并的分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读 https://wiki.jenkins-ci.org/display/ JENKINS / GIT中+插件#GitPlugin-AdvancedFeatures 这允许预先生产功能分支的推提交给主支路,从而有效地实现预提交构建/测试队列之前。



它在下使用<使用Git,Jenkins和预构建分支合并
$ b


设置您的Jenkins项目,并将Git
SCM中的'分支'字段留空。这将导致Jenkins考虑对任何
分支进行构建的任何更改。

接下来,在
中选择一个特定的分支名称作为集成目标'高级'部分 - (例如'master'或'stable'),并选择'合并之前的
'。

选择'到源代码仓库'后期构建的
操作(这需要使用构建的
结果更新您的集中式git仓库)。



现在,开发人员不应直接向您的集成
分支('主'或'stable')提交。相反,他们应该使用
特性分支,或者在提交时创建新的远程分支(例如:git
push origin HEAD:refs / heads / myNewFeature)。您也可以将您的
GIT存储库设置为仅接受来自
Jenkins的整合分支的提交。



您完成了。提交现在应该与
集成分支自动合并(如果他们不干净地把他们会失败),和
内置。如果构建成功,合并的结果将被推回到远程git仓库。

现在我经常我想继续开发功能分支,并且稍后将它们合并到 master 中。



到目前为止据我了解,这个设置会在它生成后立即合并并将任何功能分支推送到 master 中。



(How)Jenkins能否支持我的用例构建所有功能分支,但只将那些合并到 master 中的我将合并的元素合并? 我有一个构建管道,我只希望开发分支合并回我的稳定分支。所有其他人不应合并。我解决这个问题的方法是为合并分配一个单独的工作。当构建作业完成时,只有环境变量GIT_BRANCH等于'develop'才会触发'merge'作业。



Jenkins没有很好的条件触发器这是我想要的方式。以下是我如何设置我的构建作业以有条件地触发合并步骤。




  • 添加构建步骤以删除名为trigger.properties的文件(我使用Execute Shell构建步骤)

  • 添加构建步骤(conditional-buildstep plugin)如果$ GIT_BRANCH等于origin / develop,则运行条件步骤(单个)。如果为true,则运行shell命令来创建trigger.properties文件。 (触摸trigger.properties)

  • 添加一个生成后的动作(参数触发插件)来触发我的合并作业时,构建稳定。在属性文件的参数部分中,填充trigger.properties字段。有一个选项不要触发,如果有任何文件丢失,所以检查。



所以这个过程就是触发器.properties文件首先被删除,以确保以前的运行没有冲突。然后,检查git分支是否开发。如果是,请重新创建trigger.properties文件。构建后步骤需要该文件存在才能触发下一个作业。



现在,您可以在为此工作而完成的工作中执行各种合并。你也可以修改这个过程,以便多个分支进入合并作业。


I am reading https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-AdvancedFeatures which allows pre-builds of feature branches before pushing commits to the master branch, effectively implementing a pre-commit build/test queue.

It says under Using Git, Jenkins and pre-build branch merging:

Set up your Jenkins project, and leave the 'branch' field in the Git SCM blank. This will cause Jenkins to consider any change on any branch for building.

Next, pick a particular branch name as the integration target in the 'Advanced' section - (e.g. 'master', or 'stable'), and select 'Merge before build'.

Select 'Push GIT tags back to origin repository' from the post-build actions (this is required to update your centralised git repo with the results of the build).

Now, developers should never commit directly to your integration branch (the 'master' or 'stable'). Instead, they should either use feature branches, or create new remote branches on commit (e.g : "git push origin HEAD:refs/heads/myNewFeature"). You could also set up your GIT repository to only accept commits onto the integration branch from Jenkins.

You're done. Commits should now be automatically merged with the integration branch (they will fail if they do not merge cleanly), and built. If the build succeeds, the result of the merge will be pushed back to the remote git repository.

Now I often have feature branches that I would like to continue developing on, and only merge them into master later.

As far as I understand, this setup will merge and push any feature branch into master as soon as it builds.

(How) Can Jenkins support my use case, building all feature branches, but merging only those into master that I indend to be merged?

解决方案

I have a build pipeline where I only want the develop branch to merge back to my stable branch. All others should not be merged. The way I've solved this is to have a separate job for the merge. When the build job is complete, the 'merge' job is only triggered if the environment variable GIT_BRANCH equals 'develop'.

Jenkins doesn't have a very good conditional trigger that works the way I want. Here is how I set up my build job to conditionally trigger the merge step.

  • Add a build step to remove a file called trigger.properties (I use the Execute Shell build step)
  • Add a build step (conditional-buildstep plugin) Conditional step (single) to run if $GIT_BRANCH equals "origin/develop". When true, it runs a shell command to create the trigger.properties file. (touch trigger.properties)
  • Add a post-build action (Parameterized Trigger plugin) to trigger my merge job when the build is stable. In the Parameters from properties file section, fill in the field with "trigger.properties". There is an option "Don't trigger if any files are missing", so check that.

So the process is that the trigger.properties file is first removed to make sure a previous run didn't conflict. Then, check to see if the git branch is develop. If it is, recreate the trigger.properties file. The post-build step requires that file to exist for the next job to get triggered.

Now you can perform the kinds of merging you like in a job made just for that. You can also modify this process so that more than one branch makes it to the merge job.

这篇关于如何使用Jenkins预构建分支仅合并我想要合并的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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