DEV Community

Richard Spindler
Richard Spindler

Posted on

Howto use Docker Agent in a Jenkinsfile Matrix Build

There is one simple tutorial that I was looking for on the web, and I could not find it.

So I wrote it myself.

Jenkins has matrix builds, and Jenkins has agent docker for builds inside a docker image.

Combined, these features make it easy to test a project with multiple versions of PHP for example.

So here is the simple example I came up with: Put this in your Jenkinsfile and you are done.

pipeline {
    agent none
    stages {
        stage('Test') {
            matrix {
                agent {
                    docker { image "${DOCKER_IMAGE}" }
                }
                axes {
                    axis {
                        name 'DOCKER_IMAGE'
                        values 'php:5.3', 'php:5.6'
                    }
                }
                stages {
                    stage('Test') {
                        steps {
                            sh 'php --version'
                        }
                    }
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Please let me know if it helps you 😊

Learning more

Testing your software in different environments is a best practice for software engineering.

If you like this article: I am writing a book about best practices for legacy code projects:

Leading a Legacy Software Team

Top comments (0)