Skip to content

Commit

Permalink
ci: improve bootstrap and caching (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown authored Sep 20, 2024
1 parent 350296d commit 8708b9a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 35 deletions.
35 changes: 35 additions & 0 deletions .github/actions/pnpm-install/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
# Based on https://gist.github.com/belgattitude/838b2eba30c324f1f0033a797bab2e31
name: Install pnpm
description: Setup pnpm and install dependencies

runs:
using: 'composite'
steps:
- id: pnpm-config
name: Expose pnpm config(s) through "$GITHUB_OUTPUT"
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
shell: 'bash -Eeuo pipefail {0}'

- id: cache-rotation
name: Cache rotation keys
run: |
echo "YEAR_MONTH=$(/bin/date -u "+%Y%m")" >> $GITHUB_OUTPUT
shell: 'bash -Eeuo pipefail {0}'

- name: Setup pnpm cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: ${{ steps.pnpm-config.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-cache-${{ steps.cache-rotation.outputs.YEAR_MONTH }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-cache-${{ steps.cache-rotation.outputs.YEAR_MONTH }}-
- name: Bootstrap monorepo
env:
HUSKY: '0'
run: |
# Use the bootstrap script to install dependencies and build packages
pnpm bootstrap
shell: 'bash -Eeuo pipefail {0}'
26 changes: 5 additions & 21 deletions .github/actions/prepare/action.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
---
name: Prepare Job
description: Prepare the job by installing required tools and dependencies

runs:
using: 'composite'
steps:
- name: Enable Corepack
run: corepack enable
shell: 'bash -Eeuo pipefail {0}'

- id: setup-node
name: Setup Node.js
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
cache: 'pnpm'
node-version-file: '.node-version'

- id: cache-node-modules
name: Cache node_modules
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
- uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2
with:
key: node-${{ steps.setup-node.outputs.node-version }}-modules-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
path: '**/node_modules'
restore-keys: node-${{ steps.setup-node.outputs.node-version }}-modules-${{ runner.os }}-

- if: steps.cache-node-modules.outputs.cache-hit != 'true' || steps.setup-node.outputs.cache-hit != 'true'
env:
HUSKY: '0'
name: Install dependencies
run: pnpm install
shell: 'bash -Eeuo pipefail {0}'
run_install: false

- name: Build packages
run: pnpm run build
shell: 'bash -Eeuo pipefail {0}'
- name: Install dependencies and build packages
uses: ./.github/actions/pnpm-install
2 changes: 1 addition & 1 deletion .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ branches:
protection:
required_status_checks:
strict: false
contexts: ['Test', 'Release', 'Renovate / Renovate']
contexts: ['CI', 'Release', 'Renovate / Renovate']
enforce_admins: true
required_pull_request_reviews: null
restrictions: null
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/cache-cleanup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
name: Cache Cleanup

'on':
pull_request:
types: [closed]
schedule:
- cron: '0 0 * * *'
workflow_dispatch:

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

- id: cache-info
name: Get branch name and cache keys
env:
BRANCH: >-
${{
github.event_name == 'pull_request' && github.event.pull_request.merged && format('refs/pull/{0}/merge', github.event.pull_request.number)
|| github.event_name != 'pull_request' && github.ref
|| ''
}}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh extension install actions/gh-actions-cache
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
if [ -n "$BRANCH" ]; then
# Get cache keys that don't include the branch's SHA (if not a PR)
gh actions-cache list -R ${{ github.repository }} -B $BRANCH --order asc --sort last-use | cut -f 1 > cache-keys.txt
[ -z "${{ github.event.pull_request }}" ] && grep -v ${{ github.sha }} cache-keys.txt > cache-keys.txt.tmp && mv cache-keys.txt.tmp cache-keys.txt
echo cache-keys="$(cat cache-keys.txt | tr '\n' ' ')" >> $GITHUB_OUTPUT
fi
- env:
CACHE_KEYS: ${{ steps.cache-info.outputs.cache-keys }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: ${{ steps.cache-info.outputs.branch != '' }}
name: Delete stale cache entries from ${{ steps.cache-info.outputs.branch }}
run: |
set +e
for key in $CACHE_KEYS; do
gh actions-cache delete $key -R ${{ github.repository }} -B ${{ steps.cache-info.outputs.branch }} --confirm
done
10 changes: 5 additions & 5 deletions .github/workflows/ci.yaml → .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Continuous Integration jobs for this repository.
# Continuous Integration jobs for changes pointed to main.
---
name: CI
name: Main

'on':
merge_group:
Expand All @@ -19,8 +19,8 @@ permissions:
contents: read

jobs:
test:
name: Test
ci:
name: CI
runs-on: ubuntu-latest

steps:
Expand All @@ -43,7 +43,7 @@ jobs:
name: Release
if: github.repository == 'bfra-me/works' && github.ref == 'refs/heads/main'
needs:
- test
- ci
permissions:
contents: read
id-token: write
Expand Down
3 changes: 2 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
auto-install-peers = false
resolution-mode = time-based
save-prefix =
save-prefix = ''
shamefully-hoist = true
shell-emulator = true
strict-peer-dependencies = true
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"author": "Marcus R. Brown <contact@bfra.me>",
"type": "module",
"scripts": {
"bootstrap": "pnpm install && pnpm run clean && pnpm -r build",
"bootstrap": "pnpm install --frozen-lockfile --prefer-offline && pnpm run clean && pnpm -r build",
"build": "pnpm -r run build",
"check-format": "prettier --check .",
"clean": "rimraf --glob \"packages/**/lib\" \"**/*.tsbuildinfo\"",
"clean": "rimraf --glob \"packages/*/lib\" \"**/*.tsbuildinfo\"",
"format": "prettier --write .",
"lint": "manypkg check && pnpm run lint-packages && pnpm -r run --parallel lint",
"lint-packages": "pnpm -r --include-workspace-root --no-bail --filter=!@bfra.me/prettier-config exec publint",
"publish-changesets": "changeset publish",
"test": "CI=1 pnpm -r run test",
"test": "pnpm -r run test",
"version-changesets": "changeset version",
"watch": "pnpm run build --watch"
},
Expand Down
7 changes: 3 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8708b9a

Please sign in to comment.