Are there any tools that convert SemVer to suitable Docker tags?


I’m wondering if anyone knows of a simple tool that would convert SemVer tags into something more suitable for Docker tags. For example, giving the input v1.2.3 would output 1.2.3,1.2,1.


You’ll have to more specific about where you want to use it. And how you’re getting that tag.

On the command line this may help, or not, we cannot tell as the options are endless:

echo ${TAG#v*}
echo ${TAG#v*} | cut -d. -f1-2
echo ${TAG#v*} | cut -d. -f1
1 Like

Thanks for the reply.

The tag comes from a CI build, so I was just looking for an easy way to convert any SemVer compliant tags into those one and two part (major, major.minor) style Docker tags. The main reason I was hoping for something pre-existing is that I want to set up some automated Docker builds for a project that uses SemVer, but isn’t mine. I was hoping to cover all potential options such as the - and + suffixes and thought maybe there was already a tool that gracefully handles things like + where they can’t be passed straight in as a Docker tag.

I decided to do pretty much what you show; strip the v and split everything else into the parts I want. I don’t think I need to support the + build metadata in SemVer, so I decided to ignore it since trying to use it in a Docker tag will fail the build anyway allowing me to defer dealing with it until it’s actually a problem.

This is what I eventually used for now.

if [ -n "${CI_TAG}" ]; then
  readonly build_tag="${CI_TAG#v*}"
  readonly patch_tag=$(echo "${build_tag}" | cut -d '-' -f 1)
  readonly minor_tag=$(echo "${patch_tag}" | cut -d '.' -f 1,2)
  readonly major_tag=$(echo "${patch_tag}" | cut -d '.' -f 1)