niklodeon
niklodeon

Reputation: 1380

Docker image retag for build promotion

I am trying to promote a docker image in my Jenkins pipeline using Jenkins docker plugin but I am able to do so as I am getting following error.

"docker tag" requires exactly 2 arguments.
See 'docker tag --help'.

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

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

I can see the following in the logs

docker tag artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/dev:artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/test:latest

Pipeline code:

testImage = docker.image("artifactory.mycompany.com/docker-dev/appname/dev:latest")

testImage.pull()

testImage.push("artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/test:latest" )

Any idea what's wrong here...

Edit#1: If I do following the I get different error.

testImage = docker.image("artifactory.mycompany.com/docker-dev/appname/dev:latest")

testImage.pull()

testImage.tag("artifactory.mycompany.com/docker-dev/appname/test:latest")

testImage.push( )

Error:

Error parsing reference: "artifactory.mycompany.com/docker-dev/appnamee/dev:artifactory.mycompany.com/docker-dev/appname/test:latest" is not a valid repository/tag: invalid reference format

Upvotes: 2

Views: 4568

Answers (1)

matus
matus

Reputation: 724

It seems that you are running into some confusion on what each docker command does and how to add new tags to an existing docker on your workspace.

On Jenkins world, docker commands behave like this

docker.image takes a single argument, composing IMAGE_NAME:TAG

docker.tag with a single argument, will assume TAG (this command will not change 'IMAGE_NAME', it will only change the TAG part)

docker.push takes a single optional argument, TAG, meant to push an already existing Image with a different tag only (not with a different IMAGE_NAME)

On your pipeline, you are trying to change the IMAGE_NAME part of the docker identifier, since, none of the above commands help you.

NEW SOLUTION

Another way of approaching this issue is to make the IMAGE_NAME change via shell, and then use the Jenkins plugins to map and push the images

sh("docker tag ORIGINAL_IMAGE_NAME:ORIGINAL_TAG NEW_IMAGE_NAME:NEW_TAG")
newImage = docker.image("NEW_IMAGE_NAME:NEW_TAG")
# docker plugin should find the image on the localhost, so there is no need to pull it form the registry
newImage.push

on your code, something like

sh ('docker tag artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/test:latest')
testImage2 = docker.image('artifactory.mycompany.com/docker-dev/appname/test:latest')

and then, push each image independently from the other

testImage.push()
testImage2.push()

DID NOT WORK

You could try supplying 2 arguments to docker.tag, such as

docker.tag (ORIGINAL_IMAGE_NAME:ORIGINAL_TAG, NEW_IMAGE_NAME:NEW_TAG)

in your case, something like

testImage.tag ("artifactory.mycompany.com/docker-dev/appname/dev:latest" "artifactory.mycompany.com/docker-dev/appname/test:latest")

and then, push each image independently from the other

testImage.push (ORIGINAL_IMAGE_NAME:ORIGINAL_TAG)
testImage.push (NEW_IMAGE_NAME:NEW_TAG)

Upvotes: 3

Related Questions