Skip to content

Commit

Permalink
feat(workflows): revamp workflows with artifacts instead of a site
Browse files Browse the repository at this point in the history
…branch

Using artifacts for build output allows them to be more easily managed, compared to checking out an entire repository.
  • Loading branch information
TrebledJ committed Jun 8, 2024
1 parent 57ffda5 commit cd8a67a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ name: Dependabot Reviewer

on: pull_request_target
# on: workflow_dispatch
# on:
# workflow_run:
# workflows: [Dry Run Build]
# branches:
# - 'dependabot/**'
# types:
# - completed

env:
OLD_BUILD_FOLDER: old-build
MASTER_REF: master

permissions:
pull-requests: write
Expand All @@ -27,55 +20,58 @@ jobs:
run: |
echo "## Debug" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- ref: ${{ github.ref }}" >> $GITHUB_STEP_SUMMARY
echo "- user.login: ${{ github.event.pull_request.user.login }}" >> $GITHUB_STEP_SUMMARY
echo "- merged: ${{ github.event.pull_request.merged }}" >> $GITHUB_STEP_SUMMARY
echo "- merged_at: ${{ github.event.pull_request.merged_at }}" >> $GITHUB_STEP_SUMMARY
echo "- mergeable: ${{ github.event.pull_request.mergeable }}" >> $GITHUB_STEP_SUMMARY
echo "- mergeable_state: ${{ github.event.pull_request.mergeable_state }}" >> $GITHUB_STEP_SUMMARY
pre-diff:
name: Pre-Diff Checkout
runs-on: ubuntu-latest
steps:
- name: Checkout Base Site
uses: actions/checkout@v4
with:
ref: site
path: ${{ env.OLD_BUILD_FOLDER }}

- name: Sanity Check
run: |
echo "cwd: `pwd`" && echo "Workspace: $GITHUB_WORKSPACE" && ls -Al $GITHUB_WORKSPACE
sanity:
if: github.event.pull_request.user.login == 'dependabot[bot]'

build:
needs: sanity
uses: ./.github/workflows/build.yml

diff:
name: Diff Builds
needs: [pre-diff, build]
needs: build
runs-on: ubuntu-latest
steps:
- name: Sanity Check
run: |
echo "cwd: $(pwd)" && echo "Workspace: $GITHUB_WORKSPACE" && ls -Al $GITHUB_WORKSPACE
echo "\ndiff: $(which diff)"
echo "diff: $(which diff)"
- name: Download Master Site
uses: actions/download-artifact@v4
with:
name: site-${{ env.MASTER_REF }}
path: master/

- name: Download PR Site
uses: actions/download-artifact@v4
with:
name: ${{ needs.build.outputs.artifact }}
path: pr/

- name: Diff Folders
id: diff
continue-on-error: true
run: diff --unified --recursive ${{ env.OLD_BUILD_FOLDER }}/_site/ $GITHUB_WORKSPACE/_site/ >.diff
- name: Epic Fail
run: diff --unified --recursive master/ pr/ >.diff

- name: Diff Found
if: steps.diff.outcome != 'success'
run: |
echo "## Diff Output" >> $GITHUB_STEP_SUMMARY
echo '## Diff Output' >> $GITHUB_STEP_SUMMARY
echo '```diff' >> $GITHUB_STEP_SUMMARY
cat .diff >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
exit 1
test:
needs: sanity
uses: ./.github/workflows/test.yml

review-dependabot-pr:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ on:
schedule:
- cron: "00 11 * * 2,5" # HKT 7PM Tuesday/Friday

env:
REF: master

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
ref: site
name: site-${{ env.REF }}
path: _site/

# - name: Restore Lychee Cache
# uses: actions/cache@v3
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/site.validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Workflow to validate site files.
name: Validate

on:
workflow_dispatch:

# Run after a site build completed.
workflow_run:
workflows: [Build]
types: [completed]
branches: [master]

env:
REF: master

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: site-${{ env.REF }}
path: _site/

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- name: Restore npm Cache
id: cache-npm
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}

- name: Install Dependencies
if: steps.cache-npm.outputs.cache-hit != 'true'
run: npm ci

- name: Validate HTML
run: npm run validate-html
74 changes: 42 additions & 32 deletions .github/workflows/build.yml → .github/workflows/src.build.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

# Build (without deploying) an Eleventy site for CI testing.
# Useful for testing PRs and branches.
# See deploy.yml for a build + deploy to GitHub Pages workflow.
name: Dry Run Build
# Build an Eleventy site and upload build artifacts.
name: Build

on:
push:
Expand All @@ -21,17 +18,26 @@ on:
- '!scripts/**'
- '!ust/**'

# Regularly build (to keep the main site artifact alive).
schedule:
- cron: "00 10 1 * *" # HKT 6PM, first day of each month

# Allows this workflow to be called from other workflows.
workflow_call:
outputs:
artifact:
description: The artifact name of the uploaded site files.
value: ${{ jobs.build.outputs.artifact }}

# Allows you to run this workflow manually from the Actions tab.
workflow_dispatch:

jobs:
# Build job
build:
name: Build
runs-on: ubuntu-latest
outputs:
artifact: ${{ steps.upload.outputs.artifact }}
steps:
- name: Get Ref
id: get-ref
Expand All @@ -53,51 +59,55 @@ jobs:

- name: Sanity Check
run: |
echo '## ref' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "ref: ${{ github.ref }}" >> $GITHUB_STEP_SUMMARY
echo "ref_name: ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "ref_type: ${{ github.ref_type }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo '## `git status`' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
git status >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
- name: Detect Package Manager
id: detect-package-manager
run: |
if [ -f "${{ github.workspace }}/yarn.lock" ]; then
echo "manager=yarn" >> $GITHUB_OUTPUT
echo "command=install" >> $GITHUB_OUTPUT
exit 0
elif [ -f "${{ github.workspace }}/package.json" ]; then
echo "manager=npm" >> $GITHUB_OUTPUT
echo "command=ci" >> $GITHUB_OUTPUT
exit 0
else
echo "Unable to determine packager manager" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: ${{ steps.detect-package-manager.outputs.manager }}
cache: npm

- name: Restore cache
- name: Restore 11ty Cache
uses: actions/cache@v4
with:
path: |
.cache
.eleventy/cache
path: .cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
key: ${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
restore-keys: ${{ runner.os }}-eleventy-${{ hashFiles('**/package-lock.json') }}-

- name: Restore npm Cache
id: cache-npm
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}

- name: Install Dependencies
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
if: steps.cache-npm.outputs.cache-hit != 'true'
run: npm ci

- name: Build
run: |
echo "## Build Output" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
${{ steps.detect-package-manager.outputs.manager }} run prod >> $GITHUB_STEP_SUMMARY
npm run prod >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
- uses: actions/upload-artifact@v4
with:
name: site-${{ github.ref }}
path: _site/

- id: upload
run: echo 'site-${{ github.ref }}' >> $GITHUB_OUTPUT
File renamed without changes.
File renamed without changes.
27 changes: 0 additions & 27 deletions .github/workflows/validate-html.yml

This file was deleted.

0 comments on commit cd8a67a

Please sign in to comment.