Created
February 18, 2024 17:00
-
-
Save diegofcornejo/4e563c39680e5e4944bbda9a53002fbf to your computer and use it in GitHub Desktop.
Github Action - Deploy to S3 and Invalidate CloudFront Cache
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
name: Deploy to S3 and Invalidate CloudFront Cache | |
on: | |
push: | |
branches: | |
- main | |
- develop | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '18' | |
- name: Install Dependencies | |
run: npm install | |
- name: Build | |
run: npm run build | |
- name: Deploy | |
run: | | |
if [[ $GITHUB_REF == 'refs/heads/main' ]]; then | |
echo "Deploying to Production" | |
aws s3 cp ./build s3://$PRODUCTION_S3_BUCKET/ --recursive | |
aws cloudfront create-invalidation --distribution-id $PRODUCTION_CLOUDFRONT_DISTRIBUTION_ID --paths "/*" | |
elif [[ $GITHUB_REF == 'refs/heads/develop' ]]; then | |
echo "Deploying to Staging" | |
aws s3 cp ./build s3://$STAGING_S3_BUCKET/ --recursive | |
aws cloudfront create-invalidation --distribution-id $STAGING_CLOUDFRONT_DISTRIBUTION_ID --paths "/*" | |
fi | |
env: | |
PRODUCTION_S3_BUCKET: ${{ secrets.PRODUCTION_S3_BUCKET }} | |
PRODUCTION_CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.PRODUCTION_CLOUDFRONT_DISTRIBUTION_ID }} | |
STAGING_S3_BUCKET: ${{ secrets.STAGING_S3_BUCKET }} | |
STAGING_CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.STAGING_CLOUDFRONT_DISTRIBUTION_ID }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment