0

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'
            }
        }
    }
}
holms
  • 9,112
  • 14
  • 65
  • 95
Avi Kivity
  • 1,362
  • 9
  • 17

2 Answers2

3

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.

Avi Kivity
  • 1,362
  • 9
  • 17
0

You simply need to:

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

You can use use my_image by itself in a shell step

sh 'echo ${my_image}'

but in a pipeline step, you need to dereference it from env

Rich Duncan
  • 1,845
  • 1
  • 12
  • 13
  • It's closer, but does not work. I'm redefining my_image in a step (see the full script I posted), and that redefinition does not survive to the second step. – Avi Kivity Nov 28 '18 at 14:31
  • The final step allows env.IMAGE for "echo" but not for docker's image attribute: stage('build') { agent { docker { image env.IMAGE //image 'docker.io/scylladb/scylla-build-dependencies-docker:fedora-29' label 'packager' } } steps { echo env.IMAGE git 'https://github.com/avikivity/jenkins-docker-test' sh './build' } } – Avi Kivity Nov 28 '18 at 14:48
  • I think the problem is that the "image" clause is evaluated before any stages are run, so I don't get to use the variable as evaluated in the previous step. – Avi Kivity Nov 28 '18 at 15:09