Jenkins Dockerfile declarative pipelines: getting the built image tag

5,058

Solution 1

docker build does output the image ID:

Successfully built 6fd2b9d56348
Successfully tagged foo:latest

Unfortunately, it doesn't seem like the Docker Build Step plugin exposes it to the pipeline (it does write it to the log!). You may want to open an issue with that project.

That said, docker tag itself accepts name:tag:

$ docker help tag
Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Since you (can) choose image name and first tag for docker build, try using those to identify the built image instead of its ID. If that doesn't work with the plugin, you may need a feature request after all.

As a workaround, you can always use sh script: 'docker ...' directly.

Solution 2

As I understand you want to add a variable to the docker image tag,

in this case, you have to get an environment variable from Jenkins and use it in your image label.

    pipeline {
  environment {
    registry = "docker_hub_account/repository_name"
    registryCredential = 'dockerhub'
  }
  agent any
  stages {
    stage('Building image') {
      steps{
        script {
          docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
  }
}

pleae use this URL for more info

Share:
5,058

Related videos on Youtube

Attila Szeremi
Author by

Attila Szeremi

Updated on September 18, 2022

Comments

  • Attila Szeremi
    Attila Szeremi over 1 year

    I'm trying to get the image tag generated with the help of the dockerfile agent in Jenkins declarative pipelines.

    This Jenkinsfile is incomplete though, because I still need the deployment steps.

    Normally, people would then want a step to push to Dockerhub. Although I don't even know how to do that, I want something different: I want to re-tag the built image locally to a name of my choice. This is because this is a hobby project, and my Jenkins and production servers are the same. The problem is that I don't know how to get the dynamically generated image ID for the docker tag command.

    (Then what would come next is to swap out the old running image with the new one, but I could probably take care of that myself.)

    Here is my Jenkinsfile:

    pipeline {
        agent {
            dockerfile {
                args '--network szeremi -e DB_HOST=mysql -e APP_ENV=testing'
            }
        }
    
        stages {
            stage('Run tests') {
                steps {
                    sh 'cd /var/www && php artisan migrate:fresh && vendor/bin/phpunit'
                }
            }
        }
    }
    

    Please help me use Jenkins declarative pipelines to re-tag my built-by-dockerfile image.

  • Attila Szeremi
    Attila Szeremi over 5 years
    No. I don't want to use a pre-existing Docker image from the registry (docker agent), I want to build a new Docker image from the Dockerfile in my project (dockerfile agent). I know I can use variables, but the problem is that I don't know how to dynamically get the image tag of the newly built image.
  • Ahmed Badawy
    Ahmed Badawy over 5 years
    OK, I will edit my answer
  • Attila Szeremi
    Attila Szeremi over 5 years
    Still not OK. I want to make use of the dockerfile agent. You are using the any agent now.
  • Ahmed Badawy
    Ahmed Badawy over 5 years
    I think you can't use the agent which you still need to add a tag to it from build number, The Idea from the agent that you have already up and running container connected to Jenkins as a node and it used to build your code ( for your case you need to run docker command so the user agent should have docker command on this agent )
  • Ijaz Ahmad
    Ijaz Ahmad over 3 years
    @AhmedBadawy how to pass build argument to docker.build?