git 如何仅对我想合并的分支使用 Jenkins 预构建分支合并?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22732145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:54:54  来源:igfitidea点击:

How to use Jenkins pre-build branch merging only for branches I want to merge?

gitjenkins

提问by nh2

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

我正在阅读https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-AdvancedFeatures,它允许在将提交推送到主分支之前预构建功能分支,从而有效地实现预提交构建/测试队列。

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

它在下面说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.

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

接下来,在“高级”部分选择一个特定的分支名称作为集成目标(例如“主”或“稳定”),然后选择“构建前合并”。

从构建后操作中选择“将 GIT 标签推回原始存储库”(这是使用构建结果更新集中式 git 存储库所必需的)。

现在,开发人员不应该直接提交到您的集成分支('master' 或 'stable')。相反,他们应该使用功能分支,或者在提交时创建新的远程分支(例如:“git push origin HEAD:refs/heads/myNewFeature”)。您还可以将 GIT 存储库设置为仅接受 Jenkins 对集成分支的提交。

你完成了。提交现在应该自动与集成分支合并(如果它们不干净地合并,它们将失败)并构建。如果构建成功,合并的结果将被推送回远程 git 存储库。

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

现在我经常有一些我想继续开发的功能分支,只有在master以后才能将它们合并。

As far as I understand, this setup will merge and push anyfeature branch into masteras soon as it builds.

据我了解,此设置将在构建后立即合并并推送任何功能分支master

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

(如何)Jenkins 能否支持我的用例,构建所有功能分支,但只合并那些master我打算合并的分支?

回答by Tim Rupe

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'.

我有一个构建管道,我只希望开发分支合并回我的稳定分支。所有其他人不应合并。我解决这个问题的方法是为合并做一个单独的工作。构建作业完成后,仅当环境变量 GIT_BRANCH 等于 '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.
  • 添加构建步骤以删除名为 trigger.properties 的文件(我使用 Execute Shell 构建步骤)
  • 添加一个构建步骤(conditional-buildstep 插件) 如果 $GIT_BRANCH 等于“origin/develop”,则要运行的条件步骤(单个)。当为真时,它运行一个 shell 命令来创建 trigger.properties 文件。(触摸trigger.properties)
  • 添加构建后操作(参数化触发器插件)以在构建稳定时触发我的合并作业。在“来自属性文件的参数”部分中,使用“trigger.properties”填写该字段。有一个选项“如果缺少任何文件则不触发”,因此请检查。

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.

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

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.

现在,您可以在专为此而设计的工作中执行您喜欢的各种合并。您还可以修改此过程,以便有多个分支进入合并作业。

回答by mikelong

Have you had a look at the pretested commit plugin for Jenkins? https://wiki.jenkins-ci.org/display/JENKINS/Pretested+Integration+Plugin

你有没有看过 Jenkins 的预测试提交插件? https://wiki.jenkins-ci.org/display/JENKINS/Pretested+Integration+Plugin

It was designed for exactly the purpose you described.

它正是为您描述的目的而设计的。