Search code examples
dockerjenkins-pipeline

docker.build() in Jenkins pipeline with two tags


How can I set another tag for docker build step in the Jenkins pipeline which uses docker.build() script?

As for now I have:

docker.build("artifactory/docker/${IMAGE_NAME}:${BUILD_NO}")

and then

rtDockerPush(
  serverId: "Artifactory",
  image: "artifactory/docker/${IMAGE_NAME}:${BUILD_NO}",
  targetRepo: 'docker',
)
rtPublishBuildInfo(
  serverId: "Artifactory"
)

but I want to tag it with the :latest as well

I have tried something like:

docker.build("artifactory/docker/${IMAGE_NAME}:${BUILD_NO}","-t latest .")

but that is not working (it got published only with build number).

Any tips on that?

PS. Based on the given answer I have ended up to have two calls for the push as follow:

rtDockerPush(
  serverId: "Artifactory",
  image: "artifactory/docker/${IMAGE_NAME}:${BUILD_NO}",
  targetRepo: 'docker',
)
rtDockerPush(
  serverId: "Artifactory",
  image: "artifactory/docker/${IMAGE_NAME}:latest",
  targetRepo: 'docker',
)
rtPublishBuildInfo(
  serverId: "Artifactory"
)

Solution

  • You need to update your build command to:

    docker.build("test-alpine:123", "test-alpine:latest .")
    

    It will produce the following bash command underhood:

    docker build -t test-alpine:123 -t test-alpine:latest .
    

    So probably need to put this code in your Jenkinsfile:

    docker.build("artifactory/docker/${IMAGE_NAME}:${BUILD_NO}","-t artifactory/docker/${IMAGE_NAME}:latest .")
    

    To push additional tag you can use docker.push command

    def customImage = docker.build("my-image:${env.BUILD_ID}")
    customImage.push()
    
    customImage.push('latest')
    

    This is official docs code: https://www.jenkins.io/doc/book/pipeline/docker/