|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# get-docker-tags.sh |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./get-docker-tags.sh <build number> <git commit sha1> <git branch name> [git tag name] |
| 7 | +# |
| 8 | +# Example: |
| 9 | +# |
| 10 | +# # get tag for the master branch |
| 11 | +# ./get-docker-tags.sh $(date -u +%F) testingsha master |
| 12 | +# |
| 13 | +# # get tag for a release tag |
| 14 | +# ./get-docker-tags.sh $(date -u +%F) testingsha release v0.5.0 |
| 15 | +# |
| 16 | +# # Serving suggestion in circle ci - https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables |
| 17 | +# ./get-docker-tags.sh $(date -u +%F) "$CIRCLE_SHA1" "$CIRCLE_BRANCH" "$CIRCLE_TAG" |
| 18 | +# |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +if [[ $# -lt 1 ]] ; then |
| 22 | + echo 'At least 1 arg required.' |
| 23 | + echo 'Usage:' |
| 24 | + echo './push-docker-tags.sh <build number> [git commit sha1] [git branch name] [git tag name]' |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +BUILD_NUM=$1 |
| 29 | +GIT_SHA1=${2:-$(git rev-parse HEAD)} |
| 30 | +GIT_SHA1_SHORT=$(echo "$GIT_SHA1" | cut -c 1-7) |
| 31 | +GIT_BRANCH=${3:-$(git symbolic-ref -q --short HEAD || echo "unknown")} |
| 32 | +GIT_TAG=${4:-$(git describe --tags --exact-match || echo "")} |
| 33 | + |
| 34 | +IMAGE_NAME=${IMAGE_NAME:-ipfs/go-ipfs} |
| 35 | + |
| 36 | +echoImageName () { |
| 37 | + local IMAGE_TAG=$1 |
| 38 | + echo "$IMAGE_NAME:$IMAGE_TAG" |
| 39 | +} |
| 40 | + |
| 41 | +if [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+-rc ]]; then |
| 42 | + echoImageName "$GIT_TAG" |
| 43 | + |
| 44 | +elif [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 45 | + echoImageName "$GIT_TAG" |
| 46 | + echoImageName "latest" |
| 47 | + echoImageName "release" # see: https://github.com/ipfs/go-ipfs/issues/3999#issuecomment-742228981 |
| 48 | + |
| 49 | +elif [[ $GIT_BRANCH =~ ^bifrost-.* ]]; then |
| 50 | + # sanitize the branch name since docker tags have stricter char limits than git branch names |
| 51 | + branch=$(echo "$GIT_BRANCH" | tr '/' '-' | tr --delete --complement '[:alnum:]-') |
| 52 | + echoImageName "${branch}-${BUILD_NUM}-${GIT_SHA1_SHORT}" |
| 53 | + |
| 54 | +elif [ "$GIT_BRANCH" = "master" ]; then |
| 55 | + echoImageName "master-${BUILD_NUM}-${GIT_SHA1_SHORT}" |
| 56 | + echoImageName "master-latest" |
| 57 | + |
| 58 | +else |
| 59 | + echo "Nothing to do. No docker tag defined for branch: $GIT_BRANCH, tag: $GIT_TAG" |
| 60 | + |
| 61 | +fi |
0 commit comments