0
0
Jenkinsdevops~10 mins

Matrix builds for multi-platform in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building software for different platforms can be slow and repetitive. Matrix builds let Jenkins run the same steps for many platforms automatically, saving time and avoiding mistakes.
When you want to test your app on Windows, Linux, and macOS without writing separate jobs.
When you need to build Docker images for multiple CPU architectures like amd64 and arm64.
When you want to run the same tests on different Java versions to ensure compatibility.
When you want to compile your code for different operating systems from one Jenkins job.
When you want to save time by running builds in parallel for multiple environments.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent none
  stages {
    stage('Build and Test') {
      matrix {
        axes {
          axis {
            name 'OS'
            values 'linux', 'windows', 'mac'
          }
          axis {
            name 'ARCH'
            values 'amd64', 'arm64'
          }
        }
        agent {
          label "${OS} && ${ARCH}"
        }
        stages {
          stage('Checkout') {
            steps {
              checkout scm
            }
          }
          stage('Build') {
            steps {
              echo "Building on ${OS} for ${ARCH}"
              sh 'make build'
            }
          }
          stage('Test') {
            steps {
              echo "Testing on ${OS} for ${ARCH}"
              sh 'make test'
            }
          }
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with a matrix stage.

The axes section lists the platforms: OS (linux, windows, mac) and ARCH (amd64, arm64).

Jenkins runs all combinations in parallel, using agents labeled by OS and ARCH.

Inside each matrix cell, it checks out code, builds, and tests.

Commands
This command updates the Jenkins job configuration with the matrix build pipeline defined in the Jenkinsfile.
Terminal
jenkins-jobs --conf jenkins.ini update my-matrix-job
Expected OutputExpected
Job 'my-matrix-job' updated successfully
--conf - Specifies the Jenkins configuration file to connect to the server
This command triggers the matrix build job on Jenkins and waits for it to finish, showing the build status.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-matrix-job -s
Expected OutputExpected
Started build #42 Finished: SUCCESS
-s - Waits for the build to complete and shows the result
This command fetches the console output of build number 42 to see logs from all matrix runs.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 console my-matrix-job 42
Expected OutputExpected
[linux amd64] Building on linux for amd64 [linux amd64] Build successful [windows arm64] Building on windows for arm64 [windows arm64] Build successful [mac amd64] Building on mac for amd64 [mac amd64] Build successful Finished: SUCCESS
Key Concept

Matrix builds let you run the same pipeline steps automatically across many platform combinations in parallel.

Common Mistakes
Not labeling Jenkins agents correctly for each OS and architecture.
Jenkins cannot find an agent to run the job for that platform, causing build failures.
Ensure agents have labels matching the matrix axes values, like 'linux' and 'amd64'.
Using 'agent any' inside the matrix instead of specifying labels.
The build may run on the wrong platform, invalidating the test results.
Use 'agent { label "${OS} && ${ARCH}" }' to run on the correct platform.
Not defining all axes needed for the platforms you want to test.
Some platform combinations won't run, missing important tests.
List all OS and architecture values explicitly in the matrix axes.
Summary
Define a Jenkins pipeline with a matrix stage listing OS and architecture axes.
Jenkins runs all combinations in parallel on agents labeled for each platform.
Use CLI commands to update the job, trigger builds, and view logs for all matrix runs.