Skip to content

Instantly share code, notes, and snippets.

@karimsa
Created January 14, 2019 20:09
Show Gist options
  • Save karimsa/c1294b4c53932c9a0d3c99120c75bc65 to your computer and use it in GitHub Desktop.
Save karimsa/c1294b4c53932c9a0d3c99120c75bc65 to your computer and use it in GitHub Desktop.
Deploy private docker image
#!/bin/sh -ex
# docker-deploy.sh
# Deploys image from Travis to ECR.
#
# Copyright (C) 2017-present Karim Alibhai. All rights reserved.
export PROJECT="$(cat .travis.yml | grep 'repo\:' | head -n 1 | cut -d\: -f2 | cut -d/ -f2 | sed 's/^dyn-//')"
if test -z "$TRAVIS_PULL_REQUEST" || test "$TRAVIS_PULL_REQUEST" != "false"; then
echo "Skipping deployment."
exit
fi
if test -z "${AWS_ACCESS_KEY_ID}" || test -z "${AWS_SECRET_ACCESS_KEY}"; then
echo "AWS credentials are missing."
exit 1
fi
# figure out version number
FULL_VERSION="$(cat package.json | grep 'version' | cut -d\: -f2 | cut -d\" -f2)"
MAJOR="$(echo ${FULL_VERSION} | cut -d. -f1)"
MINOR="$(echo ${FULL_VERSION} | cut -d. -f2)"
PATCH="$(echo ${FULL_VERSION} | cut -d. -f3)"
# create docker versions
MAJOR_VERSION="${MAJOR}"
MINOR_VERSION="${MAJOR}.${MINOR}"
# if not on travis, try to determine using local branch
if test -z "${TRAVIS_BRANCH}"; then
export TRAVIS_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
fi
# deployment for production
if test "${TRAVIS_BRANCH}" = "${TRAVIS_TAG}"; then
export DOCKER_TAG="latest"
elif test "${TRAVIS_BRANCH}" = "master"; then
export DOCKER_TAG="staging"
elif test "${TRAVIS_BRANCH}" = "develop"; then
export DOCKER_TAG="dev"
elif test -z "${DOCKER_TAG}"; then
echo "Unknown branch. Skipping deployment."
exit
fi
echo "Deploying to: ${DOCKER_TAG}"
# dynamic aws variable for when you're using pyenv
# or similar
if test -z "$AWS"; then
export AWS="aws"
fi
# log into docker via aws
# (with -x, this would print out secure credentials - so
# temporarily opt out of -x)
set +x
AWS_LOGIN="$(${AWS} ecr get-login --no-include-email --region ca-central-1)"
AWS_SECRET="$(echo ${AWS_LOGIN} | sed 's/AWS -p /|/g' | cut -d\| -f2 | sed 's/https\:/|/g' | cut -d\| -f1)"
set -x
# set docker repo
DOCKER_REGISTRY="467791464356.dkr.ecr.ca-central-1.amazonaws.com"
DOCKER_REPO="${PROJECT}"
# check if version already exists
ERR_LOG="$(mktemp)"
set +x
{
eval $(echo curl --head -u "AWS:${AWS_SECRET}" "https://${DOCKER_REGISTRY}/v2/${DOCKER_REPO}/manifests/${FULL_VERSION}" -i)
} | tee ${ERR_LOG}
set -x
check_status () {
head -n 1 ${ERR_LOG} | grep "${1}" >/dev/null
}
if check_status '404'; then
echo "Tag not found on ECR. Yay!"
elif check_status '200'; then
echo "Version already deployed. Cannot redeploy!"
exit 1
else
echo "Something went wrong."
exit 1
fi
# get a new docker login
set +x
eval $(${AWS} ecr get-login --no-include-email --region ca-central-1)
set -x
# build & tag
docker build -t dynalytics/${PROJECT}:${DOCKER_TAG} .
# create aws tag
docker tag dynalytics/${PROJECT}:${DOCKER_TAG} ${DOCKER_REGISTRY}/${DOCKER_REPO}:${DOCKER_TAG}
# create release tags
docker tag dynalytics/${PROJECT}:${DOCKER_TAG} ${DOCKER_REGISTRY}/${DOCKER_REPO}:${FULL_VERSION}
docker tag dynalytics/${PROJECT}:${DOCKER_TAG} ${DOCKER_REGISTRY}/${DOCKER_REPO}:${MINOR_VERSION}
docker tag dynalytics/${PROJECT}:${DOCKER_TAG} ${DOCKER_REGISTRY}/${DOCKER_REPO}:${MAJOR_VERSION}
# if dry run, just exit
if test "${DRY_RUN}" = "true"; then
exit 0
fi
# push to ECS
docker push ${DOCKER_REGISTRY}/${DOCKER_REPO}
# send deployment notification
curl \
-X POST \
--data "tag=${DOCKER_TAG}&version=${FULL_VERSION}&project=${PROJECT}" \
https://hooks.zapier.com/hooks/catch/${ZAP_ACCNT}/${ZAP_HOOK}/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment