infra: Fix and make CI faster #582
Workflow file for this run
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: Build and Deploy to Pages | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- src/** | |
- static/** | |
- package.json | |
- pnpm-lock.yaml | |
- "*.config.*s" | |
- tsconfig.json | |
- .github/workflows/* | |
pull_request: | |
paths: | |
- src/** | |
- static/** | |
- package.json | |
- pnpm-lock.yaml | |
- "*.config.*s" | |
- tsconfig.json | |
- .github/workflows/* | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
# Start the workflow | |
jobs: | |
permissions-check: | |
name: Permissions check | |
runs-on: ubuntu-latest | |
if: github.event_name == 'pull_request' | |
outputs: | |
has-permissions: ${{ steps.check-output.outputs.has-permissions }} | |
steps: | |
- name: β Has access to secrets? | |
id: secrets-check | |
continue-on-error: true | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ github.event.repository.full_name }} | |
ref: ${{ github.head_ref }} | |
token: ${{ secrets.WORKFLOW_PAT }} | |
- name: π€ Set output | |
id: check-output | |
if: always() | |
run: echo "has-permissions=${{ steps.secrets-check.outcome == 'success' && 'true' || 'false' }}" >> $GITHUB_OUTPUT | |
prechecks: | |
name: Pre-checks | |
runs-on: ubuntu-latest | |
needs: permissions-check | |
if: github.event_name == 'pull_request' && !failure() && !cancelled() | |
permissions: | |
contents: write | |
steps: | |
- name: π Checkout | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ github.event.repository.full_name }} | |
ref: ${{ github.head_ref }} | |
token: ${{ needs.permissions-check.outputs.has-permissions == 'false' && github.token || secrets.WORKFLOW_PAT }} | |
fetch-depth: ${{ github.event_name == 'push' && 2 || 1 }} | |
- name: π₯ Install pnpm | |
uses: pnpm/action-setup@v2 | |
with: | |
version: latest | |
- name: π§ Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: latest | |
cache: pnpm | |
- name: π₯ Install NPM dependencies | |
run: pnpm i --no-frozen-lockfile | |
- name: π Detect file changes | |
id: detect-changes-pnpm | |
run: | | |
if [[ $(git diff --name-only) =~ pnpm-lock.yaml ]]; then | |
echo "changes_detected=true >> $GITHUB_OUTPUT" | |
else | |
echo "changes_detected=false >> $GITHUB_OUTPUT" | |
fi | |
- name: β Exit if lock file is not updated | |
if: needs.permissions-check.outputs.has-permissions == 'false' && steps.detect-changes-pnpm.outputs.changes_detected == 'true' | |
run: exit 1 | |
- name: π€ Commit updated lock file | |
id: auto-commit-action-lock | |
if: needs.permissions-check.outputs.has-permissions == 'true' | |
uses: stefanzweifel/git-auto-commit-action@v5 | |
with: | |
commit_message: Update lock file | |
file_pattern: pnpm-lock.yaml | |
- name: β Exit if lock file has been committed | |
if: needs.permissions-check.outputs.has-permissions == 'true' && steps.auto-commit-action-lock.outputs.changes_detected == 'true' | |
run: exit 1 | |
- name: π§ Check Inlang config | |
run: pnpm ci:update-inlang | |
- name: π Detect file changes | |
id: detect-changes-inlang | |
run: | | |
if [[ $(git diff --name-only) =~ inlang.config.js ]]; then | |
echo "changes_detected=true >> $GITHUB_OUTPUT" | |
else | |
echo "changes_detected=false >> $GITHUB_OUTPUT" | |
fi | |
- name: β Exit if Inlang config is not up-to-date | |
if: needs.permissions-check.outputs.has-permissions == 'false' && steps.detect-changes-inlang.outputs.changes_detected == 'true' | |
run: exit 1 | |
- name: π€ Commit updated Inlang config | |
id: auto-commit-action-inlang | |
if: needs.permissions-check.outputs.has-permissions == 'true' | |
uses: stefanzweifel/git-auto-commit-action@v5 | |
with: | |
commit_message: Update Inlang config | |
file_pattern: inlang.config.js | |
- name: β Exit if Inlang config has been committed | |
if: needs.permissions-check.outputs.has-permissions == 'true' && steps.auto-commit-action-inlang.outputs.changes_detected == 'true' | |
run: exit 1 | |
- name: π Get modified files | |
id: modified-files | |
uses: tj-actions/changed-files@v39 | |
- name: π€ Export results | |
id: changed-files | |
run: | | |
code_changes=false | |
config_changes=false | |
for file in ${{ steps.modified-files.outputs.all_changed_files }}; do | |
if [[ $file =~ ^src/ || $file =~ ^static/ ]]; then | |
code_changes=true | |
elif [[ $file =~ ^.*.config.*s || $file =~ ^tsconfig.json ]]; then | |
config_changes=true | |
fi | |
done | |
echo "code_changes=${code_changes}" >> $GITHUB_OUTPUT | |
echo "config_changes=${config_changes}" >> $GITHUB_OUTPUT | |
- name: β¨ Check Svelte format | |
if: steps.changed-files.code_changes == 'true' | |
run: pnpm check | |
- name: β¨ Check style with Prettier & ESLint | |
if: steps.changed-files.code_changes == 'true' || steps.changed-files.config_changes == 'true' | |
run: pnpm lint | |
- name: π§ Fix lint | |
if: failure() && needs.permissions-check.outputs.has-permissions == 'true' | |
run: pnpm format | |
- name: π€ Commit lint fixes | |
if: failure() && needs.permissions-check.outputs.has-permissions == 'true' | |
uses: stefanzweifel/git-auto-commit-action@v5 | |
with: | |
commit_message: Fix lint | |
build: | |
name: Build website | |
runs-on: ubuntu-latest | |
needs: prechecks | |
if: (!failure() || github.event_name == 'push') && !cancelled() | |
outputs: | |
has-built: ${{ steps.changed-files.requires_build }} | |
steps: | |
- name: π Checkout | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ github.event.repository.full_name }} | |
ref: ${{ github.head_ref }} | |
fetch-depth: ${{ github.event_name == 'push' && 2 || 1 }} | |
- name: π Get modified files | |
id: modified-files | |
uses: tj-actions/changed-files@v39 | |
- name: π€ Export results | |
id: changed-files | |
run: | | |
requires_build=false | |
for file in ${{ steps.modified-files.outputs.all_changed_files }}; do | |
if [[ $file =~ ^src/ || $file =~ ^static/ || $file =~ ^package.json || $file =~ ^pnpm-lock.yaml || $file =~ ^.*.config.*s || $file =~ ^tsconfig.json ]]; then | |
requires_build=true | |
fi | |
done | |
echo "requires_build=${requires_build}" >> $GITHUB_OUTPUT | |
- name: π§ Configure pages | |
uses: actions/configure-pages@v3 | |
if: github.event_name != 'pull_request' && steps.changed-files.requires_build == 'true' | |
id: pages | |
with: | |
static_site_generator: sveltekit | |
- name: π₯ Install pnpm | |
if: steps.changed-files.requires_build == 'true' | |
uses: pnpm/action-setup@v2 | |
with: | |
version: latest | |
- name: π§ Setup Node | |
if: steps.changed-files.requires_build == 'true' | |
uses: actions/setup-node@v3 | |
with: | |
node-version: latest | |
cache: pnpm | |
- name: π₯ Install NPM dependencies | |
if: steps.changed-files.requires_build == 'true' | |
run: pnpm i # no need for `--no-frozen-lockfile` here, as the sync is ensured by the `prechecks` job | |
- name: π¨ Build repo | |
if: steps.changed-files.requires_build == 'true' | |
run: | | |
touch static/.nojekyll | |
pnpm build | |
- name: π€ Upload artifact | |
if: github.event_name == 'push' && steps.changed-files.requires_build == 'true' | |
uses: actions/upload-pages-artifact@v2 | |
with: | |
path: build | |
deploy: | |
name: Deploy website to GitHub Pages | |
runs-on: ubuntu-latest | |
needs: build | |
if: github.event_name == 'push' && !failure() && !cancelled() && needs.build.outputs.has-built == 'true' | |
concurrency: | |
group: pages | |
cancel-in-progress: true | |
permissions: | |
pages: write | |
id-token: write | |
environment: | |
name: github-pages | |
url: ${{ steps.deployment.outputs.page_url }} | |
outputs: | |
page_url: ${{ steps.deployment.outputs.page_url }} | |
steps: | |
- name: π Deploy Pages | |
uses: actions/deploy-pages@v2 | |
id: deployment | |
perf-check: | |
name: Performance checks | |
runs-on: ubuntu-latest | |
needs: [build, deploy] | |
if: github.event_name == 'push' && !failure() && !cancelled() && needs.build.outputs.has-built == 'true' | |
steps: | |
- name: π§ Setup Bun | |
uses: oven-sh/setup-bun@v1 | |
- name: βοΈ Unlighthouse check | |
run: | | |
npm i -g @unlighthouse/cli puppeteer | |
unlighthouse-ci --site ${{ needs.deploy.outputs.page_url }} --budget 75 | |
- name: π€ Upload artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: unlighthouse-result | |
path: .unlighthouse/ci-result.json |