Keep It Simple Stupid

Jenkins: matrix job with conditions

| comments

Let’s say I have an iOS project, and I’d like to build it in a number of configurations: “Debug”/“Release”, for device/simulator, and with/without debug logs. Jenkins has the multi-configuration (aka matrix) project build plugin for that. Not all of the steps are required/possible everywhere (e.g., testing is allowed in the simulator only, or checking the license is enough one time only).

My first idea was to create different jobs for different steps and combine them using the “Parameterized Launch” step. However, I’d end up with a very complex configuration and a number of duplicate jobs (because I also want to build the project from the master and develop branches as well).

Then it hit me I could use the “Conditional BuildStep” plugin to run build steps conditionally, and the “Flexible Publish” plugin to publish the results conditionally.

Here’s how to set that up:

  1. In the matrix job, define the axis. For example, “user-defined axis” based on the requirements above may start with configuration = Debug Release, and sdk = iphonesimulator iphoneos.
  2. Add a “Conditional steps (multiple)” step with the following parameters: for “Run?” select “And”, then in the “&&” conditions you select “Strings match”, and put $axis0 and axis0value0, then another Strings match, put $axis1 and axis1value0. For example, use these conditions: Run?: And; &&: {Strings match: {$configuration: Debug}}, {Strings match: {$sdk: iphonesimulator}}. Add the build steps for the specific condition. Take a look:

My next idea was to do exactly the same setup in the “Post-build Actions” section using the “Flexible publish” step. However, I stumbled upon a bug that prevented that from working:

Started by user anonymous
Building in workspace /Users/Shared/Jenkins/Home/jobs/matrix-demo/workspace
Exception caught evaluating condition: [org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Unrecognized macro 'configuration' in '$configuration'], action = [Fail the build]
Triggering matrix-demo » Release,iphonesimulator
Triggering matrix-demo » Release,iphoneos
Triggering matrix-demo » Debug,iphoneos
Triggering matrix-demo » Debug,iphonesimulator
matrix-demo » Release,iphonesimulator completed with result SUCCESS
matrix-demo » Release,iphoneos completed with result SUCCESS
matrix-demo » Debug,iphoneos completed with result SUCCESS
matrix-demo » Debug,iphonesimulator completed with result SUCCESS
Finished: FAILURE

As you can see, all the configuration jobs have finished successfully, but the main job has failed due to the Unrecognized macro 'configuration' in '$configuration' error. For some reason, that condition is evaluated BEFORE actually running the matrix jobs and on the main job, which is wrong.

The workaround is to use the “Any Build Step plugin”. After installation go to the Jenkins settings, and in the “Flexible publish” section select “Allowed build steps”: “Any build step”. For the consistency sake, you can also do the same in the “Conditional buildstep” section. Screenshot:

Now you can use post-build actions, such as “Archive the artifacts” and “Scan for compiler warnings”, right in the “Conditional steps (multiple)” build step. An added benefit is that you don’t need to duplicate the conditions in build and post-build steps. Here is what the setup looks like: