Skip to content

Instantly share code, notes, and snippets.

@PuKoren
Created January 14, 2015 16:28
Show Gist options
  • Save PuKoren/74623c41df9391e51453 to your computer and use it in GitHub Desktop.
Save PuKoren/74623c41df9391e51453 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
# APP_ENV: 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="eu-west-1"
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=eu-west-1\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