Skip to content

Instantly share code, notes, and snippets.

@eramuk
Created March 23, 2020 06:16
Show Gist options
  • Save eramuk/256e3f03913d3acedb499e06229f3585 to your computer and use it in GitHub Desktop.
Save eramuk/256e3f03913d3acedb499e06229f3585 to your computer and use it in GitHub Desktop.
docker build push to ecr from github
#!/bin/bash
set -eu -o pipefail
if [[ $# -ne 1 ]]; then
echo "invalid argument"
exit 1
fi
REPO=$1
repo_user=$(echo $REPO | cut -d '/' -f 1)
repo_name=$(echo $REPO | cut -d '/' -f 2)
LOCAL_REPO=${LOCAL_REPO:-${HOME}/src/${repo_name}}
account_id=$(aws sts get-caller-identity --query 'Account' --output text)
region=$(aws configure get region)
aws ecr get-login-password | docker login -u AWS --password-stdin https://${account_id}.dkr.ecr.${region}.amazonaws.com > /dev/null
repository_uri=$(aws ecr describe-repositories --repository-names $repo_name --query 'repositories[].repositoryUri' --output text)
git clone git@github.com:${REPO} ${LOCAL_REPO} 2>/dev/null || (cd ${LOCAL_REPO} && git pull)
cd ${LOCAL_REPO}
image_tag=$(git rev-parse --short HEAD)
docker build -t ${repository_uri}:${image_tag} .
docker push $repository_uri:${image_tag}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment