Interview Prep/CI/CD/Jenkins Pipelines and Agents

A pipeline needs to run the same test stage across Java 17, 21 and two OSes. How do you avoid copy-paste?

IntermediateScenarioJenkins Pipelines and Agents
Answer

Situation read → A matrix.

What I'd do →

stage('Test') {
  matrix {
    axes {
      axis { name 'JDK'; values '17', '21' }
      axis { name 'PLATFORM'; values 'linux', 'windows' }
    }
    excludes { exclude { axis { name 'JDK'; values '17' } axis { name 'PLATFORM'; values 'windows' } } }
    stages { stage('run') { steps { echo "JDK ${JDK} on ${PLATFORM}" } } }
  }
}

Why → Declarative matrix generates the parallel cells and supports excludes.

Interview tip → Compare to GitHub Actions strategy.matrix - same idea.

All CI/CD questions

More on Jenkins Pipelines and Agents