3

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.

3 Answers 3

1

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

5
  • 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. Commented Jan 28, 2019 at 17:12
  • OK, I will edit my answer Commented Jan 29, 2019 at 8:35
  • Still not OK. I want to make use of the dockerfile agent. You are using the any agent now. Commented Jan 30, 2019 at 15:04
  • 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 ) Commented Jan 31, 2019 at 9:37
  • @AhmedBadawy how to pass build argument to docker.build?
    – Ijaz Ahmad
    Commented Jan 18, 2021 at 11:17
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.

0

Probably you need additionalBuildArgs to tag your image as mytag:

    agent {
        dockerfile {
            additionalBuildArgs '-t mytag'
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.