Interview Prep/Jenkins/Declarative pipeline syntax

How does the `matrix` directive work?

IntermediateConceptDeclarative pipeline syntax
Answer

matrix generates one parallel cell per combination of axes, with optional excludes. Each cell runs the inner stages, and axis values are available as environment variables.

stage('Test matrix') {
  matrix {
    axes {
      axis { name 'NODE'; values '20', '22' }
      axis { name 'OS';   values 'linux', 'windows' }
    }
    excludes { exclude { axis { name 'OS'; values 'windows' }; axis { name 'NODE'; values '20' } } }
    agent { label "${OS}" }
    stages { stage('Test') { steps { echo "Node ${NODE} on ${OS}" } } }
  }
}

Watch the cell count: axes multiply, and large matrices can hit the JVM Method code too large limit.

All Jenkins questions Roadmap: Jenkins

More on Declarative pipeline syntax