如何做这个詹金斯条件构建?

时间:2016-03-04 16:17:17

标签: jenkins parameters conditional

我们是.net商店,所以使用Jenkins MSBuild插件进行构建。对于每个项目,我们都有一个开发和发布版本,但我注意到他们唯一的区别是:

  1. / p:Configuration = Debug或/ p:Configuration =在MSBuild中发布
  2. 发布版本在最后有一个额外的复制步骤
  3. 所以我想我是否可以将它们合并到一个构建中,并根据git分支名称更改构建中的上述配置。我可以得到像这个%GIT_BRANCH%= origin / development这样的git分支名称,但我无法弄明白:

    1. 如何构建使用上述%GIT_BRANCH%的条件步骤。
    2. 如何使用上述%GIT_BRANCH%更改/ p:Configuration =?
    3. 有人可以给我一些灯吗?

1 个答案:

答案 0 :(得分:3)

I assume that, in merging these into a single job, you're expecting to parameterize the build (e.g., defining a Choice parameter named "Type" with values of "Development" and "Release"). If so, you could check "Prepare an environment for the run", then add something like the following to the "Evaluated Groovy script" field:

if ("Release".equals(Type)) {
    def map = [Configuration: "Release"]
    return map
}

if ("Development".equals(Type)) {
    def map = [Configuration: "Debug"]
    return map
}

This will define a Configuration environment variable with the specified value, which can then be passed to MSBuild as a ${Configuration} or %Configuration% environment variable (e.g., "/p:Configuration=${Configuration}" as the Command Line Argument).

If you have an additional build step that should only execute conditionally (if the Type is "Release"), you can use the Conditional BuildStep Plugin, in which case you can use a Regular Expression Match, with the Expression "^Release$" and the Label ${ENV,var="Type"} to access the value of the "Type" environment variable and compare it to the "^Release$" regex pattern. You would then do your copy step as the step to execute if the condition is met.

Alternatively, and more simply, you can use IF logic in a Windows batch file, such as this:

IF "%Type%" EQU "Release"
(
    rem Copy your files wherever
)
相关问题