Skip to content

Instantly share code, notes, and snippets.

@StevenACoffman
Forked from yefim/Dockerrun.aws.json
Last active November 5, 2017 17:11
Show Gist options
  • Save StevenACoffman/b8dfb2508968b2408120570d3051b1b8 to your computer and use it in GitHub Desktop.
Save StevenACoffman/b8dfb2508968b2408120570d3051b1b8 to your computer and use it in GitHub Desktop.
Build a Docker image, push it to AWS EC2 Container Registry, then deploy it to AWS Elastic Beanstalk
#!/bin/bash -e
# usage: ./deploy.sh <branch> <sha1>
# example usage: ./deploy.sh staging f0478bd7c2f584b41a49405c91a439ce9d944657
# ./deploy.sh "$(git rev-parse --abbrev-ref HEAD)" "$(git rev-parse HEAD)"
BRANCH=$1
SHA1=$2
if [ -n "$AWS_ACCOUNT_ID" ]; then
AWS_ACCOUNT_ID=12345678900
echo "You must export AWS_ACCOUNT_ID"
exit 1
fi
NAME=name-of-service-to-deploy
EB_BUCKET=aws-s3-bucket-to-hold-application-versions
VERSION=$BRANCH-$SHA1
ZIP=$VERSION.zip
aws configure set default.region us-east-1
# Authenticate against our Docker registry
eval $(aws ecr get-login)
# Build and push the image
docker build -t $NAME:$VERSION .
docker tag $NAME:$VERSION $AWS_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/$NAME:$VERSION
docker push $AWS_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/$NAME:$VERSION
# Replace the <AWS_ACCOUNT_ID> with the real ID
sed -i='' "s/<AWS_ACCOUNT_ID>/$AWS_ACCOUNT_ID/" Dockerrun.aws.json
# Replace the <NAME> with the real name
sed -i='' "s/<NAME>/$NAME/" Dockerrun.aws.json
# Replace the <TAG> with the real version number
sed -i='' "s/<TAG>/$VERSION/" Dockerrun.aws.json
# Zip up the Dockerrun file (feel free to zip up an .ebextensions directory with it)
zip -r $ZIP Dockerrun.aws.json
aws s3 cp $ZIP s3://$EB_BUCKET/$ZIP
# Create a new application version with the zipped up Dockerrun file
aws elasticbeanstalk create-application-version \
--application-name $NAME-application \
--version-label $VERSION \
--source-bundle S3Bucket=$EB_BUCKET,S3Key=$ZIP
# Note: AWS limits the number of application versions. if you hit an error here, we just manually clean up.
# Ideally, you would maybe delete old versions.
# Update the environment to use the new application version
# Deploy to stage
aws elasticbeanstalk update-environment \
--environment-name $NAME \
--version-label $VERSION
# from staffjoy deploy https://gist.github.com/StevenACoffman/4e89a299bd7bebb55799035227c090d5
# polling to see if the deploy succeeded.
# look for the deployed tag to match the anticipated one, it looks for EB to return to a "Ready" state
# (where another deploy can be initiated), and it times out if these conditions are not met (returning exit code 1).
deploystart=$(date +%s)
timeout=3000 # Seconds to wait before error. If it's taking awhile - your boxes probably are too small.
threshhold=$((deploystart + timeout))
while true; do
# Check for timeout
timenow=$(date +%s)
if [[ "$timenow" > "$threshhold" ]]; then
echo "Timeout - $timeout seconds elapsed"
exit 1
fi
# See what's deployed
current_version=`aws elasticbeanstalk describe-environments --application-name "$NAME-application" --environment-name "$NAME" --query "Environments[*].VersionLabel" --output text`
status=`aws elasticbeanstalk describe-environments --application-name "$NAME-application" --environment-name "$NAME" --query "Environments[*].Status" --output text`
if [ "$current_version" != "$VERSION" ]; then
echo "Tag not updated (currently $version). Waiting."
sleep 10
continue
fi
if [ "$status" != "Ready" ]; then
echo "System not Ready -it's $status. Waiting."
sleep 10
continue
fi
break
done
# Example Dockerfile
FROM hello-world
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "<AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/<NAME>:<TAG>",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "443"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment