Skip to content

Instantly share code, notes, and snippets.

@antonioned
Last active October 4, 2018 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonioned/49387010183760fa6b275ca3297c0b46 to your computer and use it in GitHub Desktop.
Save antonioned/49387010183760fa6b275ca3297c0b46 to your computer and use it in GitHub Desktop.
Custom Codeship script for deployment to Elastic Beanstalk
#!/bin/sh
#for app label I am using the last commit on github, but because spaces pose a problem in S3, replace SPACE with an underscore _
export APP_VERSION=`git log --oneline -n 1 | cut -c 1-90 | sed 's/ /_/g'`
pip install awscli
# clean build artifacts and create the application archive (also ignore any files named .git* in any folder)
git clean -fd
# precompile assets, ...
# zip the application
zip -x *.git* -r "${AWS_APP_NAME}-${APP_VERSION}.zip" .
# delete any version with the same name (based on the short revision) - optional
aws elasticbeanstalk delete-application-version --application-name "${AWS_APP_NAME}" --version-label "${APP_VERSION}" --delete-source-bundle
# upload to S3
aws s3 cp ${AWS_APP_NAME}-${APP_VERSION}.zip s3://${AWS_S3_BUCKET}/${AWS_APP_NAME}-${APP_VERSION}.zip
# create a new version and update the environment to use this version
aws elasticbeanstalk create-application-version --application-name "${AWS_APP_NAME}" --version-label "${APP_VERSION}" --source-bundle S3Bucket="${AWS_S3_BUCKET}",S3Key="${AWS_APP_NAME}-${APP_VERSION}.zip"
aws elasticbeanstalk update-environment --environment-name "${AWS_APP_ENVIRONMENT}" --version-label "${APP_VERSION}"
Don't forget to put AWS_APP_NAME, AWS_S3_BUCKET and AWS_APP_ENVIRONMENT variables in your Codeship environment.
@antonioned
Copy link
Author

An addition to this, would be an automated process of a blue/green environment deployment method. A small modification of the last command and adding 3 additional lines at the end will do the job:

aws elasticbeanstalk update-environment --environment-name BLUE_ENV --version-label "${APP_VERSION}" && sleep 500
aws elasticbeanstalk swap-environment-cnames --source-environment-name BLUE_ENV --destination-environment-name GREEN_ENV && sleep 12
aws elasticbeanstalk update-environment --environment-name GREEN_ENV --version-label "${APP_VERSION}" && sleep 450
aws elasticbeanstalk swap-environment-cnames --source-environment-name BLUE_ENV --destination-environment-name GREEN_ENV

Those sleep commands аre needed as the aws elasticbeanstalk update-environment command does not wait for the deploy to finish, it finishes in the CLI almost immediately. Because of that we do not want for the swap-environment-cnames command to fail because of that, as the deployment needs at least 3-4 minutes to finish (depending of the deployment method used in Beanstalk: all at once, immutable or rolling).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment