Skip to content

Instantly share code, notes, and snippets.

@sangdongvan
Created September 4, 2019 04:22
Show Gist options
  • Save sangdongvan/77429081205ff5f8429a8c389b623ab7 to your computer and use it in GitHub Desktop.
Save sangdongvan/77429081205ff5f8429a8c389b623ab7 to your computer and use it in GitHub Desktop.
Sample docker commands to build image on CI machine
# ----------------------------------------------------------------------------
# Git Utilities to get the current git branch and last commit
# ----------------------------------------------------------------------------
# checks if branch has something pending
function _git_dirty() {
git diff --quiet --ignore-submodules HEAD 2>/dev/null; [ $? -eq 1 ] && echo "*"
}
# gets the current git branch
function _git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(_git_dirty)/"
}
# get last commit hash (i.e. c999e1864e65a29a8f9ad165221c67ce540f646a)
function _git_hash() {
if [[ -z "${CODEBUILD_RESOLVED_SOURCE_VERSION}" ]]; then
# Get git hash from developer machine
git rev-parse HEAD 2> /dev/null
else
# Get git hash from AWS CodeBuild
echo $CODEBUILD_RESOLVED_SOURCE_VERSION
fi
}
# ----------------------------------------------------------------------------
# Docker Utilities
# ----------------------------------------------------------------------------
function docker_login() {
# Get login cmd from aws ecr
login_cmd=$(aws ecr get-login --no-include-email --profile $AWS_PROFILE)
eval $login_cmd
}
function _awscli_get_login_cmd() {
# Get login cmd from aws ecr
login_cmd=$(aws ecr get-login --no-include-email --profile $AWS_PROFILE)
echo $login_cmd
}
# Get a Docker login token and copy into the clipboard to share
function docker_login_token() {
if ! which aws >/dev/null; then
echo "=========================================================="
echo "Missing aws-cli !!! Please reach your devops man for help."
echo "=========================================================="
return 1
fi
if [ "$(uname)" = "Darwin" ]; then # MACOS
_awscli_get_login_cmd | pbcopy
echo "Docker login token was copied into the clipboard."
elif [ "$(expr substr $(uname -s) 1 5)" = "Linux" ]; then # Linux platform
if ! which apt-get >/dev/null; then # Ubuntu only
echo "======================================================="
echo "Unsupport OS !!! Please reach your devops man for help."
echo "======================================================="
return 1
fi
if ! which xclip >/dev/null; then
sudo apt-get install xclip
fi
_awscli_get_login_cmd | xclip -selection clipboard
echo "Docker login token was copied into the clipboard."
else
echo "======================================================="
echo "Unsupport OS !!! Please reach your devops man for help."
echo "======================================================="
return 1
fi
}
function _docker_verify_git() {
BRANCH=$(_git_branch)
# If the source is dirty
if [[ $BRANCH == *"*" ]] ; then
echo "Please commit the changes before doing docker build/push"
exit 1
fi
}
function docker_build() {
# Verify if there is pending code in git
_docker_verify_git
COMMIT_ID=$(_git_hash)
docker build \
-t ${GROUP}/${REPO}:$COMMIT_ID \
-f $SRC_DIR/docker/Dockerfile \
$SRC_DIR
}
function _docker_push_and_retry() {
local DOCKER_PUSH=1;
local MAX_RETRIES=3
local retries=0
while [ $DOCKER_PUSH -gt 0 ] && [ $retries -lt $MAX_RETRIES ]; do
echo "Pushing $1"
docker push $1
DOCKER_PUSH=$(echo $?)
if [[ "$DOCKER_PUSH" -gt 0 ]] ; then
echo "Docker push failed with exit code $DOCKER_PUSH"
fi
retries=$[$retries+1]
sleep 1
done
}
function _docker_tag_and_push_all() {
COMMIT_ID=$(_git_hash)
if [[ -z "$1" ]] ; then
echo "Please pass the tag"
exit 1
else
TAG=$1
fi
DOCKER_REPO=${GROUP}/${REPO}
if [[ "$COMMIT_ID" != "$TAG" ]]; then
docker tag ${DOCKER_REPO}:${COMMIT_ID} ${DOCKER_REPO}:${TAG}
fi
_docker_push_and_retry "$DOCKER_REPO:$TAG"
}
function docker_push() {
BRANCH=$(_git_branch)
COMMIT_ID=$(_git_hash)
local tag=$1
# Verify if there is pending code in git
_docker_verify_git
# Push snapshot when in master
if [ "$BRANCH" == "master" ]; then
_docker_tag_and_push_all master-${COMMIT_ID:0:8}
_docker_tag_and_push_all master
fi
# Push snapshot when in develop
if [ "$BRANCH" == "develop" ]; then
_docker_tag_and_push_all develop
fi
# Push snapshot when in beta
if [ "$BRANCH" == "beta" ]; then
_docker_tag_and_push_all beta
fi
# Verify if the tag has `release/` prefix
# Then push `release` tag as well
IFS='/' read -r -a array <<< "$BRANCH"
BRANCH_PREFIX="${array[0]}"
if [ "$BRANCH_PREFIX" == "release" ]; then
_docker_tag_and_push_all ${BRANCH_PREFIX}
fi
# Push tag when tagged
if [ -n "$tag" ]; then
_docker_tag_and_push_all ${tag}
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment