Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix configuration with Jenkins pipelines

The Jenkins Pipeline plugin (aka Workflow) can be extended with other Multibranch plugins to build branches and pull requests automatically.

What would be the preferred way to run multiple configurations? For example, building with Java 7 and Java 8. This is often called matrix configuration (because of the multiple combinations such as language version, framework version, ...) or build variants.

I tried:

  • executing them serially as separate stage steps. Good, but takes more time than necessary.
  • executing them inside a parallel step, with or without nodes allocated inside them. Works but I cannot use the stage step inside parallel for known limitations on how it would be visualized.

Is there a recommended way to do this?

like image 473
giorgiosironi Avatar asked Jun 20 '16 10:06

giorgiosironi


People also ask

What is matrix in Jenkins pipeline?

The new matrix directive lets me specify a set of axes . Each axis has a name and a list of one or more values . When the pipeline is run, Jenkins will take those and run my stages on all possible combinations of values from each axis.

What is configuration matrix in Jenkins?

The Configuration Matrix allows you to specify what steps to duplicate, and create a multiple-axis graph of the type of builds to create. For example, let us say that we have a build that we want to create for several different targets alpha, beta, and we want to produce both debug and release outputs.

What is a pipeline Matrix?

The Declarative Pipeline Matrix section allows users to specify a list stages once and then run that same list in parallel on multiple configurations. The new matrix section executes a set of one or more Pipeline stages multiple times - once for every combination defined in the matrix.

What are the 3 types of pipelines in Jenkins?

Declarative versus Scripted Pipeline syntax Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which: provides richer syntactical features over Scripted Pipeline syntax, and.


2 Answers

TLDR: Jenkins.io wants you to use nodes for each build.

Jenkins.io: In pipeline coding contexts, a "node" is a step that does two things, typically by enlisting help from available executors on agents:

  1. Schedules the steps contained within it to run by adding them to the Jenkins build queue (so that as soon as an executor slot is free on a node, the appropriate steps run)

  2. It is a best practice to do all material work, such as building or running shell scripts, within nodes, because node blocks in a stage tell Jenkins that the steps within them are resource-intensive enough to be scheduled, request help from the agent pool, and lock a workspace only as long as they need it.

Vanilla Jenkins Node blocks within a stage would look like:

stage 'build' {     node('java7-build'){ ... }     node('java8-build'){ ... } } 

Further extending this notion Cloudbees writes about parallelism and distributed builds with Jenkins. Cloudbees workflow for you might look like:

stage 'build' {     parallel 'java7-build':{       node('mvn-java7'){ ... }     }, 'java8-build':{       node('mvn-java8'){ ... }     } } 

Your requirements of visualizing the different builds in the pipeline would could be satisfied with either workflow, but I trust the Jenkins documentation for best practice.


EDIT

To address the visualization @Stephen would like to see, He's right - it doesn't work! The issue has been raised with Jenkins and is documented here, the resolution of involving the use of 'labelled blocks' is still in progress :-(

Q: Is there documentation letting pipeline users not to put stages inside of parallel steps?

A: No, and this is considered to be an incorrect usage if it is done; stages are only valid as top-level constructs in the pipeline, which is why the notion of labelled blocks as a separate construct has come to be ... And by that, I mean remove stages from parallel steps within my pipeline.

If you try to use a stage in a parallel job, you're going to have a bad time.

ERROR: The ‘stage’ step must not be used inside a ‘parallel’ block. 
like image 96
Stefan Crain Avatar answered Oct 02 '22 16:10

Stefan Crain


I would suggest Declarative Matrix as a preferred way to run multiple configurations in Jenkins. It allows you to execute the defined stages for every configuration without code duplication.

Example:

pipeline {     agent none     stages {         stage('Test') {             matrix {                 agent {                     label "${NODENAME}"                 }                 axes {                     axis {                         name 'NODENAME'                         values 'java7node', 'java8node'                     }                 }                 stages {                     stage('Test') {                         steps {                             echo "Do Test for ${NODENAME}"                         }                     }                 }             }         }     } } 

Note that declarative Matrix is a native declarative Pipeline feature, so no additional Plugin installation needed.

Jenkins blog post about the matrix directive.

like image 25
Viktor Be Avatar answered Oct 02 '22 15:10

Viktor Be