Skip to content

Instantly share code, notes, and snippets.

@vspedr
Forked from PuKoren/AWS_deploy.sh
Last active March 9, 2017 11:05
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 vspedr/cc65ad468ed505f12da68b677fdac821 to your computer and use it in GitHub Desktop.
Save vspedr/cc65ad468ed505f12da68b677fdac821 to your computer and use it in GitHub Desktop.
Travis script used to deploy on AWS ElasticBeanstalk (inspired from Codeship's one)
#!/bin/bash
# v0.0.2
# AWS Deploy Script for Travis
# Remember to add the env var in the travis configuration
# in order to work, this scripts needs:
# AWS_ACCESS_KEY_ID: user ID
# AWS_SECRET_ACCESS_KEY: user secret
# APP_NAME: EBS application name
# ENV_NAME: EBS application env
# S3_BUCKET: name of the S3 bucket to store deployment archive
# DEPLOY_BRANCH
# Check current git branch
if [ $TRAVIS_BRANCH = $DEPLOY_BRANCH ];
then
echo On $TRAVIS_BRANCH, deploying.
else
echo On $TRAVIS_BRANCH, not deploying.
exit
fi
#the AWS region, currently only 1 is supported in this script
AWS_DEFAULT_REGION="us-west-2"
mkdir ~/.aws
echo -e "[default]\naws_access_key_id=$AWS_ACCESS_KEY_ID\naws_secret_access_key=$AWS_SECRET_ACCESS_KEY" > ~/.aws/credentials
echo -e "[default]\nregion=$AWS_DEFAULT_REGION\noutput=json" > ~/.aws/config
export APP_VERSION=`git rev-parse --short HEAD`
export APP_DESCRIPTION=`git log -1 --pretty=%B`
#installing aws command line interface
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
./awscli-bundle/install -b ~/bin/aws
export PATH=~/bin:$PATH
# clean build artifacts and create the application archive (also ignore any files named .git* in any folder)
#git clean -fd
#removed because we don't need to clean, we create an archive using git archive
# zip the application
git archive --format=zip HEAD -o "${APP_NAME}-${APP_VERSION}.zip"
#zip -q -x *.git* -r "${APP_NAME}-${APP_VERSION}.zip" .
# delete any version with the same name (based on the short revision)
aws elasticbeanstalk delete-application-version --application-name "${APP_NAME}" --version-label "${APP_VERSION}" --delete-source-bundle
# upload to S3
aws s3 cp ${APP_NAME}-${APP_VERSION}.zip s3://${S3_BUCKET}/${APP_NAME}-${APP_VERSION}.zip
# create a new version and update the environment to use this version
aws elasticbeanstalk create-application-version --application-name "${APP_NAME}" --description "${APP_DESCRIPTION}" --version-label "${APP_VERSION}" --source-bundle S3Bucket="${S3_BUCKET}",S3Key="${APP_NAME}-${APP_VERSION}.zip"
aws elasticbeanstalk update-environment --environment-name "${ENV_NAME}" --version-label "${APP_VERSION}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment