7

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?

Milad
  • 4,901
  • 5
  • 32
  • 43

1 Answers1

5

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.

Tidhar Klein Orbach
  • 2,896
  • 2
  • 30
  • 47
  • Any way I can read this `imgName` in one of the stages to do stuff with the built image? – Attila Szeremi Jan 28 '19 at 18:49
  • 2
    @yarafed Unfortunately after spending a lot of time trying to get this to work, I realized that the declarative syntax was useless for what I was trying to do. To this day, there's hardly been any activity in the relevant Jenkins JIRA tickets about this issue. I had to switch to the imperative syntax and do `docker.build()` myself in a `script` block; that was the only way. – Attila Szeremi Dec 09 '19 at 19:18
  • @AttilaSzeremi thanks a lot for your response! I also decided to switch to the imperative syntax. – Yaryna Dec 11 '19 at 11:29
  • The tag is still generated as [a hash](https://github.com/jenkinsci/docker-workflow-plugin/blob/d5d2e5c4007f7ea006152542b2bcbe0f1b2b08aa/src/main/resources/org/jenkinsci/plugins/docker/workflow/declarative/DockerPipelineFromDockerfileScript.groovy#L72). – Yuri Jun 05 '23 at 09:26