Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 724
Milad Avatar asked Sep 08 '18 11:09

Milad


People also ask

Can we use Docker in Jenkins pipeline?

Pipeline is designed to easily use Docker images as the execution environment for a single Stage or the entire Pipeline. Meaning that a user can define the tools required for their Pipeline, without having to manually configure agents. Practically any tool which can be packaged in a Docker container.

How do I push an image to Docker Hub from Jenkins?

Manage Jenkins → Manage Plugins In Jenkins you have to add a new credential with your Docker Hub account details. Go to Credentials → Global → Add credentials and fill out the form with your username and password. Create your Jenkins pipeline.


1 Answers

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.

like image 166
Tidhar Klein Orbach Avatar answered Oct 15 '22 08:10

Tidhar Klein Orbach