A simple script for finding your next version number
Here's a small script that can suggest a version number for an application running on docker-compose.
At Zero-One we have quite a few applications that run on docker-compose and use Semantic Versioning when we deploy. Our build script takes a version number as an argument, but it was always a hassle to find the latest version number so that we could figure out what the next one should be. We would run this command, and then sift through the output, like an animal:
docker images | grep myfancyapp | sort -V
This command would yield the following output:
myfancyapp 3.09.4 367523bdb502 12 days ago 543 MB
myfancyapp 3.10.0 91f58d9175f7 12 days ago 543 MB
myfancyapp 3.10.1 4a748ae36e7a 12 days ago 543 MB
myfancyapp 3.10.2 6d20954468b6 12 days ago 543 MB
myfancyapp 3.10.3 7f01a6357085 5 days ago 543 MB
myfancyapp 3.11.0 964e5388f5c1 2 days ago 543 MB
myfancyapp 3.11.1 a77d659e4f95 44 hours ago 543 MB
myfancyapp latest a77d659e4f95 44 hours ago 543 MB
From there, we'd decide:
4.0.0
if it's a Major Version release, or...3.12.0
if it's a Minor Version release, or...3.11.2
if it's a Patch/Bug-fix Version release.
We now run this simple script, and it gives us a list potential version numbers:
./suggest_version.sh
Versions for myfancyapp
Current image version is: 3.11.1
Current release version is: 3.11.1
---------------------------------
Next major version is: 4.0.0
Next minor version is: 3.12.0
Next patch version is: 3.11.2
We plan to extend our build script to take major
, minor
, or patch
as arguments instead of actual version numbers, and incorporate this script's logic in to save us a step. In the meantime, here's the script, have fun with it, and forgive my poor shell-fu.
#!/usr/bin/bash
PROJECT=`basename "$PWD"`
LATEST_VER=`/usr/bin/docker images --filter="reference=$PROJECT" --format="{{.Tag}}" | sort -V | grep "^[0-9]" | tail -1`
REVISION=`cat REVISION`
MAJOR_VER=`echo $LATEST_VER | cut --delimiter="." --fields=1`
MINOR_VER=`echo $LATEST_VER | cut --delimiter="." --fields=2`
PATCH_VER=`echo $LATEST_VER | cut --delimiter="." --fields=3`
NEXT_MAJOR_VER=$((MAJOR_VER+1))
NEXT_MINOR_VER=$((MINOR_VER+1))
NEXT_PATCH_VER=$((PATCH_VER+1))
echo "Versions for $PROJECT"
echo ""
echo "Current image version is: $LATEST_VER"
echo "Current release version is: $REVISION"
echo "---------------------------------"
echo "Next major version is: $NEXT_MAJOR_VER.0.0"
echo "Next minor version is: $MAJOR_VER.$NEXT_MINOR_VER.0"
echo "Next patch version is: $MAJOR_VER.$MINOR_VER.$NEXT_PATCH_VER"
echo ""