Skip to content

Instantly share code, notes, and snippets.

@justinsoliz
Last active April 7, 2017 00:10
Show Gist options
  • Save justinsoliz/4b92423bbf06bf8be3d28797d2977ab0 to your computer and use it in GitHub Desktop.
Save justinsoliz/4b92423bbf06bf8be3d28797d2977ab0 to your computer and use it in GitHub Desktop.
continuous integration scripts for express apps + babel + aws s3
#!/bin/bash
BUILD_DIR="./.build"
# clean build directory
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR/lib
# copy package.json
cp ./package.json $BUILD_DIR
# copy yarn.lock
cp ./yarn.lock $BUILD_DIR
# compile es6
npm run compile
# install prod dependencies
cd $BUILD_DIR
yarn install --prod
printf "* rebuild native libs against AWS Lambda AMI docker image *"
docker run -v "$PWD":/var/task lambci/lambda:build
# Use the Node.js v6.10 runtime
# docker run -v "$PWD":/var/task lambci/lambda:nodejs6.10
# verify all zipped files have correct file permissions
sudo chmod -R 777 ./node_modules
# format timestamp '20160220.074052'
VERSION=$(date "+%Y%m%d.%H%M%S")
SHA=$(git rev-parse --short HEAD)
FILE=${NAME}_${VERSION}_${SHA}.zip
printf "zip file: $FILE"
zip -r ${FILE} . -x \*.git\* -x \*.zip
cd ../
mkdir -p ./.dist
mv ./.build/$FILE ./.dist
printf "\nPackage create: $FILE\n"
mkdir -p ./.artifacts
echo "${VERSION}.${SHA}" > './.artifacts/package_version'
echo $FILE > './.artifacts/package_filename'
#!/bin/bash
echo "Initializing aws environment"
get_instance_region() {
if [ -z "$AWS_REGION" ]; then
AWS_REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document \
| grep -i region \
| awk -F\" '{print $4}')
fi
echo $AWS_REGION
}
mkdir -p ~/.aws
AWS_CONFIG=~/.aws/config
SECURITY_ROLE="instance-role"
IAM_CREDENTIALS_URL="http://169.254.169.254/latest/meta-data/iam/security-credentials/${SECURITY_ROLE}"
curl -s ${IAM_CREDENTIALS_URL} >.creds.json
cat >${AWS_CONFIG} <<_EOF_
[default]
region = $(get_instance_region)
_EOF_
echo "Results from AWS config"
cat $AWS_CONFIG
echo "configure AWS ECS"
sudo $(aws ecr get-login)
echo "** Verifying ECR integration **"
aws ecr describe-repositories
#!/bin/bash
echo "Executing push-package to s3"
echo "Reading package_filename"
PACKAGE=$(cat ./.artifacts/package_filename)
echo "Found: $PACKAGE"
# bucket value
BUCKET="tf-packages"
PACKAGE_PATH=".dist/$PACKAGE"
BUCKET_PATH="$BUCKET/$PACKAGE"
aws s3 cp $PACKAGE_PATH s3://$BUCKET_PATH
echo "\nPackage: $PACKAGE uploaded to S3 Bucket: '$BUCKET'"
#!/bin/bash
API_URL="http://health-check"
BUILD_VERSION=$(npm version \
| grep api \
| awk -F: '{ print $2 }' \
| sed "s/[',]//g" \
| sed -e 's/^[[:space:]]*//')
printf "expected build version: $BUILD_VERSION\n"
printf "Verifying service: $API_URL"
retry="0"
DEPLOY_STATUS="failed"
while [ $retry -lt 15 ]
do
echo "Retry: $retry"
retry=$((retry+1))
res=`curl -I $API_URL | grep HTTP/1.1 | awk {'print $2'}`
printf "\nAPI Response: $res\n"
if [ $res -eq 200 ]
then
printf "Successful health check, checking api version\n"
DEPLOYED_API_VERSION=$(curl -i $API_URL \
| grep -i 'api-version' \
| sed "s/X-Api-Version: //g" \
| tr -d '[:space:]')
printf "Deployed API version: $DEPLOYED_API_VERSION\n"
if [ $DEPLOYED_API_VERSION != $BUILD_VERSION ]
then
echo Deployed API version does not match build version >&2
exit 1
fi
DEPLOY_STATUS="succeeded"
break
fi
sleep 20
done
printf "\nfinished deploy in $retry retries with status: $DEPLOY_STATUS"
if [ "$DEPLOY_STATUS" = "succeeded" ]; then
printf "\nDeploy succeeded\n"
exit 0
else
printf "\nDeploy failed\n"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment