Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Pull over deploy script changes from react.london #32

Merged
merged 1 commit into from
Jul 8, 2016
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ node_modules
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
Dockerrun.aws.json
*.zip


# Built assets
dist

Expand Down
File renamed without changes.
55 changes: 30 additions & 25 deletions bin/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#!/bin/bash

set -e
#!/usr/bin/env bash

ENV=$1
RELEASE_TAG=$2
RELEASE_TAG=$(git rev-parse HEAD)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removes the need for manual setting of a version. The AWS EB version GUI has dates and what env a version is deployed to so I've found that and a SHA to identify commits is sufficient.

This is a matter of taste though!

APP_NAME=badger-brain
AWS_ACCOUNT=578418881509
AWS_REGION=eu-west-1
ECR_REPO=$AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/$APP_NAME
EB_BUCKET=elasticbeanstalk-$AWS_REGION-$AWS_ACCOUNT

if [ -z "$ENV" ] || [ -z "$RELEASE_TAG" ]
then
echo Usage:
echo " deploy.sh ENVIRONMENT"
exit 1
fi

set -eu
set -o pipefail
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safer bash options. Set after the variable checking as otherwise we get an ugly error message when the user forgets to pass the argument rather than the friendlier one above.


# Authenticate
eval $(aws ecr get-login --region=$AWS_REGION)

# Build Docker image
if [ "$ENV" == "live" ]
then
VERSION=$RELEASE_TAG
docker build -t $APP_NAME:$RELEASE_TAG .
docker tag $APP_NAME:$RELEASE_TAG $ECR_REPO:$RELEASE_TAG
docker push $ECR_REPO:$RELEASE_TAG
else
VERSION=$ENV
docker build -t $APP_NAME:$ENV .
docker tag $APP_NAME:$ENV $ECR_REPO:$ENV
docker push $ECR_REPO:$ENV
fi
VERSION=$RELEASE_TAG
docker build -t $APP_NAME:$RELEASE_TAG .
docker tag $APP_NAME:$RELEASE_TAG $ECR_REPO:$RELEASE_TAG
docker push $ECR_REPO:$RELEASE_TAG
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the different path for production and staging for a few reasons.

  • Avoids errors from trying to overwrite a tag. There's a -f arg but Chris said it's deprecated.
  • It allows us to move a version directly from staging to production, which seems like a very desirable feature as it means we can ensure that the version tested on staging is the same as what we put live.

Copy link
Contributor Author

@lpil lpil Jul 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I feel like the production deploy should always be moving an existing image from staging (or storage) to production rather than compiling a fresh version. Could build on CI.


# Apply docker image path to Dockerrun.aws.json template
cp Dockerrun.aws.json.template Dockerrun.aws.json
Copy link
Contributor Author

@lpil lpil Jul 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the compiled Dockerrun.aws.json out of the repo so the deployer doesn't need to undo the changes to the file after each deploy. Removes risk of accidentally committing the compiled version.

perl -pi -e "s,<ECR_REPO>,$ECR_REPO,g" "Dockerrun.aws.json"
perl -pi -e "s,<TAG>,$VERSION,g" "Dockerrun.aws.json"

Expand All @@ -36,14 +37,18 @@ ZIP_FILE=$VERSION.zip
zip -r $ZIP_FILE Dockerrun.aws.json
aws s3 cp $ZIP_FILE s3://$EB_BUCKET/$APP_NAME/$ZIP_FILE

# In production, create a new app version
if [ "$ENV" == "live" ]
then
# Create a new application version with the zipped up Dockerrun file
aws elasticbeanstalk create-application-version --application-name $APP_NAME \
--version-label $VERSION --source-bundle S3Bucket=$EB_BUCKET,S3Key=$APP_NAME/$ZIP_FILE --region $AWS_REGION
fi
# Create a new application version with the zipped up Dockerrun file
aws elasticbeanstalk create-application-version \
--application-name $APP_NAME \
--version-label $VERSION \
--source-bundle S3Bucket=$EB_BUCKET,S3Key=$APP_NAME/$ZIP_FILE \
--region $AWS_REGION

# Update the environment to use the new application version
aws elasticbeanstalk update-environment --environment-name $APP_NAME-$ENV \
--version-label $VERSION --region $AWS_REGION
aws elasticbeanstalk update-environment \
--environment-name $APP_NAME-$ENV \
--version-label $VERSION \
--region $AWS_REGION

echo
echo Done! AWS EB deployment rollout in progress.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just formatting for readability and a friendly success message since the previous version just dumped a load of json to the terminal which is not obviously a success.