| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # TODO: this script is legacy, use get-docker-tags.sh instead. |
| 4 | # |
| 5 | # push-docker-tags.sh |
| 6 | # |
| 7 | # Run from ci to tag images based on the current branch or tag name. |
| 8 | # A bit like dockerhub autobuild config, but somewhere we can version control it. |
| 9 | # |
| 10 | # The `docker-build` job builds the current commit in docker and tags it as ipfs/kubo:wip |
| 11 | # |
| 12 | # Then the `docker-publish` job runs this script to decide what tag, if any, |
| 13 | # to publish to dockerhub. |
| 14 | # |
| 15 | # Usage: |
| 16 | # ./push-docker-tags.sh <build number> <git commit sha1> <git branch name> [git tag name] [dry run] |
| 17 | # |
| 18 | # Example: |
| 19 | # # dry run. pass a 5th arg to have it print what it would do rather than do it. |
| 20 | # ./push-docker-tags.sh $(date -u +%F) testingsha master "" dryrun |
| 21 | # |
| 22 | # # push tag for the master branch |
| 23 | # ./push-docker-tags.sh $(date -u +%F) testingsha master |
| 24 | # |
| 25 | # # push tag for a release tag |
| 26 | # ./push-docker-tags.sh $(date -u +%F) testingsha release v0.5.0 |
| 27 | # |
| 28 | set -euo pipefail |
| 29 | |
| 30 | if [[ $# -lt 1 ]] ; then |
| 31 | echo 'At least 1 arg required. Pass 5 args for a dry run.' |
| 32 | echo 'Usage:' |
| 33 | echo './push-docker-tags.sh <build number> [git commit sha1] [git branch name] [git tag name] [dry run]' |
| 34 | exit 1 |
| 35 | fi |
| 36 | |
| 37 | BUILD_NUM=$1 |
| 38 | GIT_SHA1=${2:-$(git rev-parse HEAD)} |
| 39 | GIT_SHA1_SHORT=$(echo "$GIT_SHA1" | cut -c 1-7) |
| 40 | GIT_BRANCH=${3:-$(git symbolic-ref -q --short HEAD || echo "unknown")} |
| 41 | GIT_TAG=${4:-$(git describe --tags --exact-match || echo "")} |
| 42 | DRY_RUN=${5:-false} |
| 43 | |
| 44 | WIP_IMAGE_TAG=${WIP_IMAGE_TAG:-wip} |
| 45 | IMAGE_NAME=${IMAGE_NAME:-ipfs/kubo} |
| 46 | |
| 47 | pushTag () { |
| 48 | local IMAGE_TAG=$1 |
| 49 | if [ "$DRY_RUN" != false ]; then |
| 50 | echo "DRY RUN! I would have tagged and pushed the following..." |
| 51 | echo docker tag "$IMAGE_NAME:$WIP_IMAGE_TAG" "$IMAGE_NAME:$IMAGE_TAG" |
| 52 | echo docker push "$IMAGE_NAME:$IMAGE_TAG" |
| 53 | else |
| 54 | echo "Tagging $IMAGE_NAME:$IMAGE_TAG and pushing to dockerhub" |
| 55 | docker tag "$IMAGE_NAME:$WIP_IMAGE_TAG" "$IMAGE_NAME:$IMAGE_TAG" |
| 56 | docker push "$IMAGE_NAME:$IMAGE_TAG" |
| 57 | fi |
| 58 | } |
| 59 | |
| 60 | if [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+-rc ]]; then |
| 61 | pushTag "$GIT_TAG" |
| 62 | |
| 63 | elif [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 64 | pushTag "$GIT_TAG" |
| 65 | pushTag "latest" |
| 66 | pushTag "release" # see: https://github.com/ipfs/kubo/issues/3999#issuecomment-742228981 |
| 67 | |
| 68 | elif [[ $GIT_BRANCH =~ ^bifrost-.* ]]; then |
| 69 | # sanitize the branch name since docker tags have stricter char limits than git branch names |
| 70 | branch=$(echo "$GIT_BRANCH" | tr '/' '-' | tr --delete --complement '[:alnum:]-') |
| 71 | pushTag "${branch}-${BUILD_NUM}-${GIT_SHA1_SHORT}" |
| 72 | |
| 73 | elif [ "$GIT_BRANCH" = "master" ] || [ "$GIT_BRANCH" = "staging" ]; then |
| 74 | pushTag "${GIT_BRANCH}-${BUILD_NUM}-${GIT_SHA1_SHORT}" |
| 75 | pushTag "${GIT_BRANCH}-latest" |
| 76 | |
| 77 | else |
| 78 | echo "Nothing to do. No docker tag defined for branch: $GIT_BRANCH, tag: $GIT_TAG" |
| 79 | |
| 80 | fi |