Interview Prep/Jenkins/Pipeline design & Jenkinsfile decisions

A library must be tested on Node 18, 20 and 22, on Linux and Windows, but Node 18 on Windows is not supported. Design the stage.

IntermediateScenarioPipeline design & Jenkinsfile decisions
Answer

Situation read → Classic matrix with an exclusion.

What I'd do →

stage('Test') {
  matrix {
    axes {
      axis { name 'NODE_VERSION'; values '18', '20', '22' }
      axis { name 'PLATFORM'; values 'linux', 'windows' }
    }
    excludes {
      exclude {
        axis { name 'NODE_VERSION'; values '18' }
        axis { name 'PLATFORM'; values 'windows' }
      }
    }
    agent { label "${PLATFORM}" }
    stages { stage('npm test') { steps { script {
      if (isUnix()) { sh 'npm ci && npm test' } else { bat 'npm ci && npm test' }
    } } } }
  }
}
  1. Select Node per cell with nvm/fnm or a container image tag per version on Linux.
  2. Publish JUnit per cell so failures show which combination broke.

Why → matrix generates five parallel cells with readable names and no copy-pasted stages.

Interview tip → Mention isUnix() for cross-platform steps.

All Jenkins questions Roadmap: Jenkins

More on Pipeline design & Jenkinsfile decisions