Jirong Hu
Jirong Hu

Reputation: 2375

How to do this Jenkins conditional build?

We are a .net shop so use Jenkins MSBuild plugin to do the build. For each project we have a development and release build, but I notice their only differences are:

  1. /p:Configuration=Debug or /p:Configuration=Release in MSBuild
  2. Release build has an extra copy step at the end

So I am thinking if I can merge them together into one build, and change the above configurations in the build based on the git branch names. I can get the git branch name like this %GIT_BRANCH%= origin/development, but I can't figure out:

  1. How to construct a conditional step to use the above %GIT_BRANCH%.
  2. How to use the above %GIT_BRANCH% to change the /p:Configuration=?

Can anyone shed me some lights?

Upvotes: 2

Views: 7462

Answers (1)

Nick Jones
Nick Jones

Reputation: 4465

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
)

Upvotes: 4

Related Questions