Created
May 19, 2016 13:37
-
-
Save asikkema/0e17dccdf052ba6c8e21858aed364e6d to your computer and use it in GitHub Desktop.
Deploy feature branch to AWS S3 in newly created bucket with website hosting permissions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# codeship exposes the current git branch in $CI_BRANCH. If not available use git to find it. | |
# codeship specific, not required. | |
branch=$CI_BRANCH | |
if [ -z "$CI_BRANCH" ]; then | |
echo "NO CI_BRANCH env, getting from git." | |
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p') | |
fi | |
echo "Current branch: $branch" | |
# We extract the feature number (company policy) to get the feature name we want to deploy. | |
feature=$(echo $branch | sed -n 's/feature\/\(.*\)\/.*/\1/p') | |
echo "Feature: $feature" | |
S3_BUCKET="sd-$feature.yourdomain.com" | |
echo "Bucket name: $S3_BUCKET" | |
# Note: Choose the right AWS Zone | |
S3_BUCKET_FULL="$S3_BUCKET.s3-website-eu-west-1.amazonaws.com" | |
echo "Full bucket name: $S3_BUCKET_FULL" | |
function deployToS3Bucket { | |
echo "Going to upload dist files to: $S3_BUCKET" | |
aws s3 rm "s3://$S3_BUCKET" --recursive | |
aws s3 sync dist "s3://$S3_BUCKET" | |
echo "Done." | |
} | |
function createNewBucket { | |
echo "Going to create new bucket $S3_BUCKET" | |
aws s3 mb "s3://$S3_BUCKET" --region eu-west-1 | |
echo "Sleep a while to make sure the bucket is done creating." | |
sleep 10 | |
# set policy | |
echo "Going to set bucket permissions" | |
sed "s/bucketname/$S3_BUCKET/g" ./bin/policy_template.json > policy.json | |
aws s3api put-bucket-policy --bucket "$S3_BUCKET" --policy file://policy.json | |
aws s3api put-bucket-acl --bucket "$S3_BUCKET" --grant-read 'uri="http://acs.amazonaws.com/groups/global/AllUsers"' --grant-read-acp 'uri="http://acs.amazonaws.com/groups/global/AllUsers"' | |
echo "Add website hosting to bucket." | |
aws s3 website "s3://$S3_BUCKET" --index-document index.html --error-document index.html | |
echo "Going to create route53 alias for $S3_BUCKET" | |
sed "s/bucketname/$S3_BUCKET/g" ./bin/route53-changeset-template.json > route53-changeset.json | |
aws route53 change-resource-record-sets --hosted-zone-id Z1AYYBTWL1ONA6 --change-batch file://route53-changeset.json | |
echo "Sleep a while to make sure permissions have been added." | |
sleep 10 | |
echo "Finished setup of bucket for feature deployment." | |
} | |
if aws s3 ls "s3://$S3_BUCKET" 2>&1 | grep -q 'NoSuchBucket' | |
then | |
echo "Bucket: $S3_BUCKET doesn't exist." | |
createNewBucket | |
deployToS3Bucket | |
else | |
echo "Bucket already exists. Deploying..." | |
deployToS3Bucket | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment