Last active
November 24, 2020 13:54
-
-
Save santiq/dee9d7707cf98d4bd0cfa80093495e25 to your computer and use it in GitHub Desktop.
CircleCI file to deploy to AWS S3 and invalidate AWS CloudFront cache. Read the blogpost about it https://softwareontheroad.com/s3-cloudfront-circleci-continuous-integration #continuousintegration #aws #s3 #CDN #deploy
This file contains hidden or 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
version: 2 | |
jobs: | |
"testing": | |
docker: | |
- image: circleci/node:10-stretch | |
working_directory: ~/repo | |
steps: | |
- checkout | |
- restore_cache: | |
keys: | |
- app-{{ checksum "package.json" }} | |
# fallback to using the latest cache if no exact match is found | |
- app- | |
- run: npm install | |
- save_cache: # Save node_modules into cache with a checksum of the package.json | |
paths: | |
- node_modules | |
key: app-{{ checksum "package.json" }} | |
- run: npm run test # Run your tests | |
"deploy": | |
docker: | |
- image: circleci/node:10-stretch | |
working_directory: ~/repo | |
steps: | |
- checkout | |
- run: | |
name: Installing AWS CLI | |
working_directory: / | |
command: | | |
sudo apt-get -y -qq update | |
sudo apt-get install -y awscli | |
sudo apt-get install -y python-pip python-dev build-essential | |
sudo pip install awsebcli --upgrade | |
- run: | |
name: Preparing Artifact | |
command: | | |
npm install | |
npm run build # Here goes your build command. | |
cd dist # My angular app generate a Dist folder | |
zip ../build.zip -r * .[^.]* # Just zip your files | |
echo "Sucessfull building" | |
- run: | |
name: Deploying Client to S3 and Cloudfront | |
command: | | |
aws configure set preview.cloudfront true # Turn on cloudfront in AWS CLI | |
if [ "${CIRCLE_BRANCH}" == "production" ] # Check current branch to decide to which S3 bucket deploy | |
then | |
aws s3 sync ~/repo/dist s3://yoursite.com --delete | |
aws cloudfront create-invalidation --distribution-id DISTRIBUTION_ID_YOUR_SITE_PRODUCTION --paths /\* | |
elif [ "${CIRCLE_BRANCH}" == "staging" ] | |
then | |
aws s3 sync ~/repo/dist s3://staging.yoursite.com --delete | |
aws cloudfront create-invalidation --distribution-id DISTRIBUTION_ID_YOUR_SITE_STAGING --paths /\* | |
else | |
aws s3 sync ~/repo/dist s3://dev.yoursite.com --delete | |
aws cloudfront create-invalidation --distribution-id DISTRIBUTION_ID_YOUR_SITE_DEV --paths /\* | |
fi | |
workflows: | |
version: 2 | |
build_and_deploy: | |
jobs: | |
- testing | |
- deploy: | |
requires: | |
- testing # Deploy require testing to be successful | |
filters: | |
branches: | |
only: # Only deploy for production, staging, and master branchs | |
- production | |
- staging | |
- master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment