Skip to content

Instantly share code, notes, and snippets.

@alexanderilyin
Created November 15, 2018 00:58
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alexanderilyin/8cf68f85b922a7f1757ae3a74640d48a to your computer and use it in GitHub Desktop.
Save alexanderilyin/8cf68f85b922a7f1757ae3a74640d48a to your computer and use it in GitHub Desktop.
Auto-increment Minor Semantic Version using Docker Hub API v2

For example your latest image with semantic versioning looks like this:

${DOCKER_HUB_ORG}/${DOCKER_HUB_REPO}:v1.20.0

Example provided above will do the folowing:

  1. Generate token using your github username and password.
  2. Get list of all tags from repository.
  3. Increment MINOR version by 1 and set PATCH version to 0.

This example could be useful if you are looking for:

  • Docker Hub v2 authorization example for private repos.
#!/usr/bin/env bash
set -euxo pipefail
DOCKER_HUB_ORG="***"
DOCKER_HUB_REPO="***"
DOCKER_HUB_USER="***"
DOCKER_HUB_PASSWORD="***"
AUTH_DOMAIN="auth.docker.io"
AUTH_SERVICE="registry.docker.io"
AUTH_SCOPE="repository:${DOCKER_HUB_ORG}/${DOCKER_HUB_REPO}:pull"
AUTH_OFFLINE_TOKEN="1"
AUTH_CLIENT_ID="shell"
API_DOMAIN="registry-1.docker.io"
TOKEN=$(curl -v -X GET -u ${DOCKER_HUB_USER}:${DOCKER_HUB_PASSWORD} "https://${AUTH_DOMAIN}/token?service=${AUTH_SERVICE}&scope=${AUTH_SCOPE}&offline_token=${AUTH_OFFLINE_TOKEN}&client_id=${AUTH_CLIENT_ID}" | jq -r '.token')
VERSIONS=$(curl -v -H "Authorization: Bearer ${TOKEN}" https://${API_DOMAIN}/v2/${DOCKER_HUB_ORG}/${DOCKER_HUB_REPO}/tags/list | jq -r '.tags[]' | grep -E 'v[0-9]+\.[0-9]+\.[0-9]+' | sort --version-sort)
VERSION_OLD_FULL_RAW=$(echo "${VERSIONS}" | tail -n 1)
echo ${VERSION_OLD_FULL_RAW}
VERSION_OLD_FULL_CLEAN=$(echo ${VERSION_OLD_FULL_RAW} | tr -d v)
VERSION_OLD_MAJOR=$(echo ${VERSION_OLD_FULL_CLEAN} | awk -F . '{print $1}' )
VERSION_OLD_MINOR=$(echo ${VERSION_OLD_FULL_CLEAN} | awk -F . '{print $2}' )
VERSION_OLD_PATCH=$(echo ${VERSION_OLD_FULL_CLEAN} | awk -F . '{print $3}' )
VERSION_NEW_MAJOR=${VERSION_OLD_MAJOR}
VERSION_NEW_MINOR=$((${VERSION_OLD_MINOR}+1))
VERSION_NEW_PATCH=0
VERSION_NEW_FULL_RAW="v${VERSION_NEW_MAJOR}.${VERSION_NEW_MINOR}.${VERSION_NEW_PATCH}"
echo ${VERSION_NEW_FULL_RAW}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment