dockerjenkinsgroovy

How can I execute a bash command as part of a variable substitution in a Jenkinsfile


I am executing my Jenkins build in a docker agent. I need to provide additional arguments to the docker run command, one of which is --group-add. The agent section of my Jenkinsfile looks like:

agent {
    docker {
        image "maven"
        args "-v /var/run/docker.sock:/var/run/docker.sock --group-add 999"
    }
}

The problem is that the group id is hard-coded in the Jenkinsfile and I would much rather this be extracted at runtime. i.e. I would like to do something like this:

agent {
    docker {
        image "maven"
        args "-v /var/run/docker.sock:/var/run/docker.sock --group-add $(getent group docker | cut --delimiter ':' --field 3)"
    }
}

But this expansion doesn't work. I don't think setting an environment variable would help because it would occur too late.

Am I stuck with hard coding the group id value?


Solution

  • I've resolved this issue by defining my agent with a dockerfile. This allows for execution-time expansion of variables.