Skip to content

Commit

Permalink
feat: add docker tags by post_push hook
Browse files Browse the repository at this point in the history
Dockerhub allows us to add scripts to a hooks directory in our repo, that allow us to alter the steps of an autobuild!

This PR adds a post_push dockerhub hook that adds additional tags for the stabilize-dht and master branch in the form `<branch>-<short date>-<short commit sha>`, e.g `bifrost-2020-02-26-a44c610` or `master-2020-03-01-18ca5ab`

This is an alternative to ipfs#6949 that requires no credentials in CI, but does not ensure our published docker tags are tested.

See ipfs#6949 for details

License: MIT
Signed-off-by: Oli Evans <[email protected]>
  • Loading branch information
olizilla committed Mar 5, 2020
1 parent 18ca5ab commit 951debb
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions hooks/post_push
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
set -euo pipefail

# post_push - a dockerhub autobuild hook
#
# A hook, run on dockerhub after an autobuild completes and the configured tag has been pushed.
# Use me to publish additional tag names.
#
# see: https://docs.docker.com/docker-hub/builds/advanced
#
# Usage:
# DOCKER_REPO="ipfs/go-ipfs" \
# DOCKER_TAG="bifrost-latest" \
# SOURCE_BRANCH="feat/stabilize-dh" \
# SOURCE_COMMIT="a44c610c6cf4d2a878b1b16f5add8280bed423c0" \
# ./post_push
#
# # for testing you can pass the env vars as params, and flag it as a dry run, so nothing gets dockered.
# ./post_push "ipfs/go-ipfs" "bifrost-latest" "feat/stabilize-dh" "a44c610c6cf4d2a878b1b16f5add8280bed423c0" "dryrun"

# env vars set by docker hub https://docs.docker.com/docker-hub/builds/advanced/#environment-variables-for-building-and-testing
DOCKER_REPO=${1:-$DOCKER_REPO} # e.g. "ipfs/go-ipfs"
DOCKER_TAG=${2:-$DOCKER_TAG} # e.g. "bifrost-latest"
SOURCE_BRANCH=${3:-$SOURCE_BRANCH} # e.g. "feat/stabilize-dh"
SOURCE_COMMIT=${4:-$SOURCE_COMMIT} # e.g. "a44c610c6cf4d2a878b1b16f5add8280bed423c0"

# custom vars
DRY_RUN=${5:-false} # e.g. "dryrun".
BUILD_DATE=$(date -u +%F) # e.g. "2020-03-05"
GIT_SHA_SHORT=$(echo "$SOURCE_COMMIT" | cut -c 1-7)

# Use me to create and push a new tag.
pushTag () {
local NEW_TAG=$1
if [ "$DRY_RUN" != false ]; then
echo "hooks/post_push - DRY RUN! would push tag: '$DOCKER_REPO:$NEW_TAG' for branch: '$SOURCE_BRANCH'"
echo docker tag "$DOCKER_REPO:$DOCKER_TAG" "$DOCKER_REPO:$NEW_TAG"
echo docker push "$DOCKER_REPO:$NEW_TAG"
else
echo "hooks/post_push - pushing tag: '$DOCKER_REPO:$NEW_TAG' for branch: '$SOURCE_BRANCH'"
docker tag "$DOCKER_REPO:$DOCKER_TAG" "$DOCKER_REPO:$NEW_TAG"
# docker push "$DOCKER_REPO:$NEW_TAG"
fi
}

# Add conditions where you'd like to publish an additional docker tag here
if [ "$SOURCE_BRANCH" = "feat/stabilize-dht" ]; then
pushTag "bifrost-${BUILD_DATE}-${GIT_SHA_SHORT}"

elif [ "$SOURCE_BRANCH" = "master" ]; then
pushTag "master-${BUILD_DATE}-${GIT_SHA_SHORT}"

else
echo "hooks/post_push - nothing to do for image: '$DOCKER_REPO:$DOCKER_TAG', branch: '$SOURCE_BRANCH', commit: '$SOURCE_COMMIT'"
fi

0 comments on commit 951debb

Please sign in to comment.