Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dockerhub post push hook #6956

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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