Skip to content

Instantly share code, notes, and snippets.

@hbar1st
Forked from plamoni/release.yml
Created November 6, 2024 17:43
Show Gist options
  • Save hbar1st/29c0ea24879fff3ee0a28c9db5436805 to your computer and use it in GitHub Desktop.
Save hbar1st/29c0ea24879fff3ee0a28c9db5436805 to your computer and use it in GitHub Desktop.
Pete's S3+CloudFront GitHub Action Deploy Script
# Heavily borrowed from https://gist.github.com/neftaly/6390a8b3bfc278113d1a42303532d9b2
name: Build and Deploy
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
steps:
# An action to check out code from GitHub
- uses: actions/checkout@v1
# Setting up node to build (you probably want to use a more recent version of Node)
- uses: actions/setup-node@v1
with:
node-version: '14.x'
# Not sure this helps much, but maybe it makes things faster sometimes.
- uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
# Install the Node dependencies
- run: npm install
# Run tests (all code needs tests!)
- run: npm test
# Build the distribution package
- run: npm run dist
# This is optional. But it can be nice to track test coverage.
- name: Upload Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
# The following is pinned to a known-good commit for safety purposes
# https://github.com/jakejarvis/s3-sync-action
- uses: jakejarvis/s3-sync-action@c33d061a8f4bddd78d8b5f9f4e5d2fd40a556980
with:
args: --acl public-read --follow-symlinks --delete
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_S3_BUCKET: 'SET TO YOUR S3 BUCKET NAME'
AWS_REGION: 'us-east-1'
SOURCE_DIR: 'dist' # This is the directory that my your code builds to. No preceeding slashes needed.
DEST_DIR: '' # The directory in the S3 bucket to put the code in (empty is probably fine in most cases)
# Invalidate Cloudfront
# Because of how my code is built, JS and CSS files will get new names when their content changes.
# But the index.html page keeps the same name which means that I need to tell CloudFront to invalidate
# the old version. If you're not using CloudFront, then this step can be removed.
- name: invalidate
uses: chetan/invalidate-cloudfront-action@819473e1ccf40ea3178b75c8f31ae375fb45d388
env:
DISTRIBUTION: ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION }}
PATHS: '/index.html'
AWS_REGION: 'us-east-1'
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment