jenkins-pipelinejenkins-docker

How to override Jenkins' random image tag for a Dockerfile agent in a pipeline


I have a step in my Jenkinsfile that runs using a Dockerfile agent. When jenkins creates the docker image it gives it a random long tag and I'd like to replace that with my own tag. I tried passing the tag using additionalBuildArgs but that gives the docker image an additional tag.

agent {
        dockerfile {
            additionalBuildArgs '-t my-image:latest'
        }
}

Is there a way to stop Jenkins from passing a tag?


Solution

  • The plugin that controls this action is pipeline-model-definition-plugin.

    you can see at the plugin's code that the image name is a hash of the project name and the dockerfile path:

    def hash = Utils.stringToSHA1("${runWrapper.fullProjectName}\n${script.readFile("${dockerfilePath}")}")
    def imgName = "${hash}"
    

    then it takes the additional args and adds them to the image name:

    def additionalBuildArgs = describable.getAdditionalBuildArgs() ? " ${describable.additionalBuildArgs}" : ""
    script.sh "docker build -t ${imgName}${additionalBuildArgs} -f \"${dockerfilePath}\" \"${describable.getActualDir()}\""
    

    so by using the dockerfile step, it seems that the name would always be a hash.