简体   繁体   English

Jenkins / Git:从任何提交构建最新的分支+手动构建

[英]Jenkins/Git: Build latest from branches + manual build from any commit

I am trying to set up Jenkins with a Git project so that: 我正在尝试使用Git项目设置Jenkins,以便:

  1. It will build from branches matching a pattern ( origin/master , origin/feature/* , origin/hotfix/ *, etc.) whenever changes are pushed to the central repository 每当更改被推送到中央存储库时,它将从匹配模式( origin / masterorigin / feature / *origin / hotfix / *等)的分支构建

  2. Developers and testers can trigger a build for any revision they want, specified as a build parameter that is a tag name, branch name or commit hash. 开发人员和测试人员可以为他们想要的任何修订触发构建,指定为构建参数,即标记名称,分支名称或提交哈希。 The job has other parameters and we will occasionally want to create builds with something other than the default values. 作业有其他参数,我们偶尔会想要使用默认值以外的其他东西创建构建。

I have got 1. working correctly by setting up a post-receive script on the Git server and adding multiple branch specifiers in Jenkins. 通过在Git服务器上设置post-receive脚本并在Jenkins中添加多个分支说明符,我得到了正确的工作。

In order to also do 2., I added an extra build parameter GitRef and then added an extra branch specifier with $GitRef . 为了也做2.,我添加了一个额外的构建参数GitRef ,然后用$GitRef添加了一个额外的分支说明$GitRef Manually starting a build would then just keep building from the same commit/branch every time, whatever the parameter was set to. 无论参数设置如何,手动启动构建只会每次都从同一个提交/分支继续构建。 If I removed all the other branch specifiers, the manual builds would work as expected. 如果我删除了所有其他分支说明符,则手动构建将按预期工作。 But then the hook-triggered builds would only build from origin/master (the default value of $GitRef ). 但是钩子触发的构建只能从origin / master$GitRef的默认值) $GitRef

Is what I am trying to achieve even possible without creating a two jobs for every project? 如果没有为每个项目创造两个工作岗位,我试图实现的目标是什么? If so, what do I need to do to get it working? 如果是这样,我需要做些什么才能使其正常工作?

If you install the Git Parameters Plugin you can allow users to start a parameterized build using a specific commit ID, branch or tag. 如果安装Git Parameters Plugin ,则可以允许用户使用特定的提交ID,分支或标记启动参数化构建。

You can then set the default value for your parameters as ** and by default, Jenkins will build the latest commit on branches. 然后,您可以将参数的默认值设置为** ,默认情况下,Jenkins将在分支上构建最新的提交。

Instead of using $GitRef as another branch specifier, just use it as a String variable with no default value. 而不是使用$GitRef作为另一个分支说明符,只需将其用作没有默认值的String变量。 Then, as the first step of your build phase in Jenkins, have a script that checks if that value is set: 然后,作为Jenkins构建阶段的第一步,有一个脚本来检查是否设置了该值:

#!/bin/bash
if [ -n $GitRef ]
then
  echo "Manually specified reference found. Building $GitRef"
  git checkout $GitRef
else  
  echo "No explicit branch specified, building $GIT_BRANCH" 
fi

At this point, run your build as per normal (eg Maven). 此时,按照正常情况运行构建(例如Maven)。 The build will run whatever branch/tag/commit you wish to compile, and if it doesn't exist Jenkins should take care of the git checkout <bad_ref> by failing the build. 构建将运行您希望编译的任何分支/标记/提交,如果它不存在,Jenkins应该通过使构建失败来处理git checkout <bad_ref>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM