jenkinsjenkins-pipelinejenkins-docker

Parametrized docker image in jenkins declarative pipeline


I want to run my build in a container with the image determined by a file under source control, so something like

environment {
   my_image = ...
}
agent {
    docker { 
        image my_image
    }
}

But Jenkins complains that

groovy.lang.MissingPropertyException: No such property: my_image for class: groovy.lang.Binding

Is there a way to get Jenkins to use a variable for the docker image specification?

Full pipeline for reference:

pipeline {
    agent any
    environment {
        REPO = 'unknown'
    }
    stages {
        stage('toolchain') {
            steps {
                git 'https://github.com/avikivity/jenkins-docker-test'
                script {
                    REPO = readFile 'repo'
                }
                echo "repo = ${REPO}"
            }
        }
        stage('build') {
            agent {
                docker {
                    image REPO
                    //image 'docker.io/scylladb/scylla-build-dependencies-docker:fedora-29'
                    label 'packager'
                }
            }
            steps {
                git 'https://github.com/avikivity/jenkins-docker-test'
                sh './build'
            }
        }
    }
}

Solution

  • The problem turned out to be that image env.whatever was evaluated before anything had a chance to run.

    I worked around it by using the scripted version of the docker plugin:

                script {
                    docker.image(env.IMAGE).inside {
                        sh './build'
                    }
                }
    

    Now, env.IMAGE is evaluated after it is computed, and the plugin doesn't get confused by an uninitialized argument.