Adding tags to docker image from jenkins

2019-06-28 05:55发布

I have a jenkins instance (which actually runs inside docker) for my Continous Integration.

The jenkins server builds docker images on an external docker host, tests them and then pushes them to tagged with my-app:tested.

Now, when I build a release, I want to re-tag the docker image from tested to vX.X. I do not want rebuild the image with a new tag, I want to re-tag the existing image.

How can this be done with jenkins? I am looking through the jenkins plugins and cannot find any with this capability.

2条回答
萌系小妹纸
2楼-- · 2019-06-28 06:11

As you do not link to any of the plugins you use I cannot easily say if they might be able to re-tag an existing image, it is however possible via a shell based job.

If you use a shell (bash or something similar) script in Jenkins you can easily do this with a standard docker command for tagging existing images. If your my-app:test image is already cached locally for your jenkins build job you can run:

docker login -u $USER -p $PASSWORD <myregistry.example.org>
docker pull my-app:tested 
docker tag my-app:tested my-app:vX.X
docker push my-app:vX.X

If my-app:tested is cached locally for the jenkins job you can omit the docker pull command. See the docker tag documentation for more information. If this shell commands-based workflow does not fit into your plugins-based build workflow I'm not sure how you would do it.

I do not personally use Jenkins or Jenkins plugins for building docker images, so I am not familiar with how plugins for building Docker images work in Jenkins. Someone else might be able to help you with a plugin based build job.

查看更多
该账号已被封号
3楼-- · 2019-06-28 06:12

As shown in this blog post you can add two tags with the push command like so:

stage('Push image') {
    docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
        app.push("${env.BUILD_NUMBER}")
        app.push("latest")
    }
}

PS Mind you Jenkins folks if you're reading this a 3rd party blog post does NOT count as documentation. This could have been resolved MUCH quicker had this been properly documented.

查看更多
登录 后发表回答