Skip to content

Commit

Permalink
refactor: split GitHub workflows (#528)
Browse files Browse the repository at this point in the history
* Split build and deploy workflow for prod and staging

* Created actions for repeated steps

* Fixed concurrency groups

* Added condition to not deploy staging on fork
  • Loading branch information
js0mmer authored Dec 9, 2024
1 parent 68f5e96 commit b966cb5
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 160 deletions.
27 changes: 27 additions & 0 deletions .github/actions/setup-pnpm/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Setup pnpm
runs:
using: composite
steps:
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- uses: pnpm/action-setup@v3
name: Install pnpm
with:
version: 9
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
74 changes: 0 additions & 74 deletions .github/workflows/build-and-deploy.yml

This file was deleted.

61 changes: 0 additions & 61 deletions .github/workflows/clean-up-pr.yml

This file was deleted.

46 changes: 46 additions & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Deploy production

on:
push:
branches:
- main

# do not cancel in progress, SST will be stuck in a "locked" state if cancelled mid-deployment
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

jobs:
build_and_deploy:
name: Build and deploy
runs-on: ubuntu-latest
environment:
name: production
url: https://peterportal.org
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup pnpm
uses: ./.github/actions/setup-pnpm

- name: Install dependencies
run: pnpm install
env:
HUSKY: 0

- name: Build and deploy
run: pnpm sst deploy --stage prod
env:
DATABASE_URL: ${{ secrets.PROD_DATABASE_URL }}
NODE_ENV: production

PUBLIC_API_URL: ${{ secrets.PUBLIC_API_URL }}
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
GOOGLE_CLIENT: ${{ secrets.GOOGLE_CLIENT }}
GOOGLE_SECRET: ${{ secrets.GOOGLE_SECRET }}
GRECAPTCHA_SECRET: ${{ secrets.GRECAPTCHA_SECRET }}
ADMIN_EMAILS: ${{ secrets.ADMIN_EMAILS }}
PRODUCTION_DOMAIN: ${{ secrets.PRODUCTION_DOMAIN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
ANTEATER_API_KEY: ${{ secrets.ANTEATER_API_KEY }}
50 changes: 50 additions & 0 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Deploy staging

on:
pull_request:
types:
- opened
- reopened
- synchronize

# do not cancel in progress, SST will be stuck in a "locked" state if cancelled mid-deployment
concurrency:
group: staging-${{ github.event.pull_request.number }}

jobs:
build_and_deploy:
name: Build and deploy
runs-on: ubuntu-latest
# don't run if labeled "no deploy" && don't run on PRs from forks
if: (!contains(github.event.pull_request.labels.*.name, 'no deploy')) && github.event.pull_request.head.repo.full_name == github.repository
environment:
name: staging-${{ github.event.pull_request.number }}
url: https://staging-${{ github.event.pull_request.number }}.peterportal.org
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup pnpm
uses: ./.github/actions/setup-pnpm

- name: Install dependencies
run: pnpm install
env:
HUSKY: 0

- name: Build and deploy
run: pnpm sst deploy --stage staging-${{ github.event.pull_request.number }}
env:
DATABASE_URL: ${{ secrets.DEV_DATABASE_URL }}
NODE_ENV: staging

PUBLIC_API_URL: ${{ secrets.PUBLIC_API_URL }}
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
GOOGLE_CLIENT: ${{ secrets.GOOGLE_CLIENT }}
GOOGLE_SECRET: ${{ secrets.GOOGLE_SECRET }}
GRECAPTCHA_SECRET: ${{ secrets.GRECAPTCHA_SECRET }}
ADMIN_EMAILS: ${{ secrets.ADMIN_EMAILS }}
PRODUCTION_DOMAIN: ${{ secrets.PRODUCTION_DOMAIN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
ANTEATER_API_KEY: ${{ secrets.ANTEATER_API_KEY }}
28 changes: 3 additions & 25 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,12 @@ jobs:
lint:
name: Lint and check formatting
runs-on: ubuntu-latest

steps:
- name: Check Out Repo
- name: Checkout repo
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- uses: pnpm/action-setup@v3
name: Install pnpm
with:
version: 9
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Setup pnpm
uses: ./.github/actions/setup-pnpm

- name: Install Dependencies
run: pnpm install
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/remove-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Remove staging

on:
pull_request:
types:
- closed

# use pr number for group instead of github.ref because ref will be main branch when the PR closes which is not a unique group for the PR
# group should match with deploy-staging workflow so those don't run concurrently (if someone closes/reopens a PR)
concurrency:
group: staging-${{ github.event.pull_request.number }}

jobs:
clean-up-pr:
runs-on: ubuntu-latest
# don't run on PRs from forks
if: github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup pnpm
uses: ./.github/actions/setup-pnpm

- name: Install Dependencies
run: pnpm install
env:
HUSKY: 0

- name: Remove staging
run: pnpm sst remove --stage staging-${{ github.event.pull_request.number }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

- name: Deactivate deployment
uses: strumwolf/[email protected]
with:
environment: staging-${{ github.event.pull_request.number }}
token: ${{ secrets.GITHUB_TOKEN }}
onlyDeactivateDeployments: true

0 comments on commit b966cb5

Please sign in to comment.