How to extract the value of a Docker image's tag

4/20/2017

Given a registry with several tags for an image, e.g.:

myimage  0.0.3              
myimage  0.0.2              

Can I somehow extract 0.0.3 (or rather the most recent tag) into a variable in bash?
I'd like to find out this value so that I could then tagValue++ it and use it in a Jenkins pipeline for actions such as:

  • docker build to build the next image with an updated tag
  • docker push to push this new image tag to the registry
  • kubectl set image to update a Kubernetes cluster with pods, using the updated image tag

Of course, if anyone has a better strategy I am all for hearing it!

Alternatives:

  • Get the value from the Kubernetes Deployment's YAML file, then run the actions above, update the file with the updated tag and push the updated file back to the repository?

  • Same as the alternative above, but use kubectl replace instead of kubectl set image? (example here from the docs)

-- Idan Adar
bash
jenkins
kubernetes

2 Answers

4/20/2017

we use this with an inhouse v2 docker registry. it depends on jq, so it may not be the right fit for you. the registry seems to return them in the order in which they were added, so we just grab the latest, which is quite possible with jq, if you have a new enough version:

tags_url="https://hot.registry.example.com/v2/somerepoprefix/${repo}/tags/list"
newest_tag=$(curl $tags_url | jq -r '(.tags | reverse)[0]')
-- burnettk
Source: StackOverflow

4/21/2017

Different approach, but I use the jenkins ${BUILD_NUMBER} environment variable for tagging the images. Additionally I move forward a latest tag, so I have both options to use. This results in the same image tagged multiple times with different build numbers (if there are no changes), but my setup is fine with that. Because I'm using different Jenkins jobs for building the image and updating k8s I use the jenkins api to get the latest build number across jobs:

containerTag=$(curl -u $user:$cred 'http://localhost:8080/job/$jobname/lastStableBuild/buildNumber')
-- Oswin Noetzelmann
Source: StackOverflow