From 55da451f71f99798270195bf857734d6affa40c6 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:10:23 +0100 Subject: [PATCH 01/14] Add GitHub Actions workflow for deploying pull requests to GitHub Pages --- .github/workflows/pr.yml | 127 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 .github/workflows/pr.yml diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..86e59a8 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,127 @@ +name: Deploy to GitHub Pages + +on: + pull_request: + types: + - opened + - reopened + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + +jobs: + configure: + runs-on: ubuntu-latest + permissions: + pages: read + steps: + - name: Check secrets + id: check_secrets + run: | + echo "GH_APP_ID_EXISTS=${{ secrets.GH_APP_ID && 'true' || 'false' }}" >> $GITHUB_OUTPUT + echo "GH_APP_PRIVATE_KEY_EXISTS=${{ secrets.GH_APP_PRIVATE_KEY && 'true' || 'false' }}" >> $GITHUB_OUTPUT + echo "GH_APP_EXISTS=${{ secrets.GH_APP_ID && secrets.GH_APP_PRIVATE_KEY && 'true' || 'false' }}" >> $GITHUB_OUTPUT + + - name: Generate a token + if: steps.check_secrets.outputs.GH_APP_EXISTS == 'true' + continue-on-error: true + id: generate_token + uses: actions/create-github-app-token@v1 + with: + app-id: ${{ secrets.GH_APP_ID }} + private-key: ${{ secrets.GH_APP_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + repositories: | + ${{ github.event.repository.name }} + + - name: Setup Pages + id: pages + uses: actions/configure-pages@v5 + with: + # enablement: true # requires administration:write and pages:write + enablement: ${{ steps.generate_token.outputs.token && 'true' || 'false' }} + token: ${{ steps.generate_token.outputs.token || github.token }} + outputs: + base_url: ${{ steps.pages.outputs.base_url }} + origin: ${{ steps.pages.outputs.origin }} + host: ${{ steps.pages.outputs.host }} + base_path: ${{ steps.pages.outputs.base_path }} + + build: + runs-on: ubuntu-latest + needs: + - configure + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + + - name: Install dependencies + run: yarn + + - name: Typecheck + run: yarn tsc -b + continue-on-error: true + + - name: Lint + run: yarn lint + continue-on-error: true + + - name: Build project + run: yarn build --base ${{ needs.configure.outputs.base_path }}/pr/${{ github.event.number }} + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: dist + path: dist + + # Deploy job + deploy: + # Add a dependency to the build job + needs: + - configure + - build + + # Grant GITHUB_TOKEN the permissions required to make a Pages deployment + permissions: + pages: write # to deploy to Pages + id-token: write # to verify the deployment originates from an appropriate source + contents: write + # Deploy to the github-pages environment + environment: + name: github-pages + url: ${{ needs.configure.outputs.base_url }}/pr/${{ github.event.number }} + + runs-on: ubuntu-latest + steps: + - name: Checkout pages branch + uses: actions/checkout@v4 + with: + ref: gh-pages + + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: dist + path: dist + + - name: Move artifact to pr folder + run: mv dist pr/${{ github.event.number }} + + - name: Configure Git + run: | + git config user.name "${{ github.actor }}" + git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" + - name: Stage changes + run: | + git add pr/${{ github.event.number }} + - name: Commit changes + run: | + git commit -m "Deploy PR #${{ github.event.number }}" + - name: Push changes + run: | + git push From f66327cc1e1228149b0f0a83e512827ad4983543 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:12:47 +0100 Subject: [PATCH 02/14] Update GitHub Actions workflow to use dynamic environment name for pull requests --- .github/workflows/pr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 86e59a8..fc713bd 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -93,7 +93,7 @@ jobs: contents: write # Deploy to the github-pages environment environment: - name: github-pages + name: pr/${{ github.event.number }} url: ${{ needs.configure.outputs.base_url }}/pr/${{ github.event.number }} runs-on: ubuntu-latest From a7b626a76f05e4e08804248ae97e4d9aae6978c5 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:18:36 +0100 Subject: [PATCH 03/14] Rename workflow for clarity and add push event trigger --- .github/workflows/pr.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index fc713bd..045ae86 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -1,6 +1,7 @@ -name: Deploy to GitHub Pages +name: Deploy to Environment GitHub Pages on: + push: pull_request: types: - opened From 96aeebf6ce1d60c7cd25de739d52e93c6ccfa09e Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:26:31 +0100 Subject: [PATCH 04/14] Update GitHub Actions workflow to pull or create detached pages branch --- .github/workflows/pr.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 045ae86..1d6e8d9 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -101,8 +101,11 @@ jobs: steps: - name: Checkout pages branch uses: actions/checkout@v4 - with: - ref: gh-pages + + - name: Pull or create detached pages branch + run: | + git fetch origin + git checkout -b gh-pages origin/gh-pages || git checkout --orphan gh-pages - name: Download artifact uses: actions/download-artifact@v4 From 2e7156bbfee8dce26339ef1b98d78c7436ac6667 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:27:54 +0100 Subject: [PATCH 05/14] Update GitHub Actions workflow to push changes to gh-pages branch --- .github/workflows/pr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 1d6e8d9..9c6f7a4 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -128,4 +128,4 @@ jobs: git commit -m "Deploy PR #${{ github.event.number }}" - name: Push changes run: | - git push + git push --set-upstream origin gh-pages From 2f5aa117fa7632b7092b49c459de6f3dcef6aaab Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:31:27 +0100 Subject: [PATCH 06/14] Update GitHub Actions workflow to use ref name for build and deployment paths --- .github/workflows/pr.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 9c6f7a4..ae2bf39 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -71,8 +71,8 @@ jobs: run: yarn lint continue-on-error: true - - name: Build project - run: yarn build --base ${{ needs.configure.outputs.base_path }}/pr/${{ github.event.number }} + - name: Build project using ref as base path + run: yarn build --base ${{ needs.configure.outputs.base_path }}/${{ github.ref_name }} - name: Upload artifact uses: actions/upload-artifact@v4 @@ -95,7 +95,7 @@ jobs: # Deploy to the github-pages environment environment: name: pr/${{ github.event.number }} - url: ${{ needs.configure.outputs.base_url }}/pr/${{ github.event.number }} + url: ${{ needs.configure.outputs.base_url }}/${{ github.ref_name }} runs-on: ubuntu-latest steps: @@ -114,7 +114,7 @@ jobs: path: dist - name: Move artifact to pr folder - run: mv dist pr/${{ github.event.number }} + run: mv dist ${{ github.ref_name }} - name: Configure Git run: | @@ -122,10 +122,10 @@ jobs: git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" - name: Stage changes run: | - git add pr/${{ github.event.number }} + git add ${{ github.ref_name }} - name: Commit changes run: | - git commit -m "Deploy PR #${{ github.event.number }}" + git commit -m "Deploy ${{ github.ref_name }} to GitHub Pages" - name: Push changes run: | git push --set-upstream origin gh-pages From 0a14fbb99f4adf43376ed76c4eed63faac084435 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:42:20 +0100 Subject: [PATCH 07/14] Reset git state before downloading artifacts in PR workflow --- .github/workflows/pr.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index ae2bf39..b107b64 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -106,6 +106,8 @@ jobs: run: | git fetch origin git checkout -b gh-pages origin/gh-pages || git checkout --orphan gh-pages + git reset --hard + git status - name: Download artifact uses: actions/download-artifact@v4 From 35e830d26b301e7960fa589d6913f15c29c9fee6 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:50:10 +0100 Subject: [PATCH 08/14] Update GitHub Actions workflow to include 'ref' prefix in build and deployment paths --- .github/workflows/pr.yml | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index b107b64..e2240f1 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -72,7 +72,7 @@ jobs: continue-on-error: true - name: Build project using ref as base path - run: yarn build --base ${{ needs.configure.outputs.base_path }}/${{ github.ref_name }} + run: yarn build --base ${{ needs.configure.outputs.base_path }}/ref/${{ github.ref_name }} - name: Upload artifact uses: actions/upload-artifact@v4 @@ -94,18 +94,20 @@ jobs: contents: write # Deploy to the github-pages environment environment: - name: pr/${{ github.event.number }} - url: ${{ needs.configure.outputs.base_url }}/${{ github.ref_name }} + name: ref/${{ github.ref_name }} + url: ${{ needs.configure.outputs.base_url }}/ref/${{ github.ref_name }} runs-on: ubuntu-latest steps: - name: Checkout pages branch uses: actions/checkout@v4 + with: + sparse-checkout: | - - name: Pull or create detached pages branch + - name: Prepare pages branch run: | git fetch origin - git checkout -b gh-pages origin/gh-pages || git checkout --orphan gh-pages + git checkout gh-pages || git checkout --orphan gh-pages git reset --hard git status @@ -116,7 +118,7 @@ jobs: path: dist - name: Move artifact to pr folder - run: mv dist ${{ github.ref_name }} + run: mv dist ref/${{ github.ref_name }} - name: Configure Git run: | From 61ca884e45ab01e2effcde177c1074e965f6e8f3 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:54:45 +0100 Subject: [PATCH 09/14] Enhance PR workflow to create 'ref' directory before moving artifacts --- .github/workflows/pr.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index e2240f1..986a441 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -118,7 +118,10 @@ jobs: path: dist - name: Move artifact to pr folder - run: mv dist ref/${{ github.ref_name }} + run: | + mkdir -p ref + mv dist ref/${{ github.ref_name }} + ls -alRh - name: Configure Git run: | From 4f71fc1636129b729e5095f28c6c5be84286b04d Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 10:56:20 +0100 Subject: [PATCH 10/14] Update PR workflow to stage changes in 'ref' directory before committing --- .github/workflows/pr.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 986a441..5691a09 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -127,12 +127,15 @@ jobs: run: | git config user.name "${{ github.actor }}" git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" + - name: Stage changes run: | - git add ${{ github.ref_name }} + git add ref/${{ github.ref_name }} + - name: Commit changes run: | git commit -m "Deploy ${{ github.ref_name }} to GitHub Pages" + - name: Push changes run: | git push --set-upstream origin gh-pages From 155fe25c73be9cc084fbaf001d52b2a236ae6e69 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 11:01:46 +0100 Subject: [PATCH 11/14] Refactor GitHub Actions workflow to simplify environment name configuration --- .github/workflows/pr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 5691a09..e4d825c 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -94,7 +94,7 @@ jobs: contents: write # Deploy to the github-pages environment environment: - name: ref/${{ github.ref_name }} + name: ${{ github.ref_name }} url: ${{ needs.configure.outputs.base_url }}/ref/${{ github.ref_name }} runs-on: ubuntu-latest From 9521cd52dbd0fc7df7be6b69c6ede99b4877a7f2 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 11:08:16 +0100 Subject: [PATCH 12/14] Enhance PR workflow to build and move artifacts based on branch context --- .github/workflows/pr.yml | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index e4d825c..19e3abb 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -71,7 +71,12 @@ jobs: run: yarn lint continue-on-error: true + - name: Build project using root as base path + if: github.ref_name == 'main' + run: yarn build --base ${{ needs.configure.outputs.base_path }} + - name: Build project using ref as base path + if: github.ref_name != 'main' run: yarn build --base ${{ needs.configure.outputs.base_path }}/ref/${{ github.ref_name }} - name: Upload artifact @@ -94,8 +99,8 @@ jobs: contents: write # Deploy to the github-pages environment environment: - name: ${{ github.ref_name }} - url: ${{ needs.configure.outputs.base_url }}/ref/${{ github.ref_name }} + name: ${{ github.ref_name == 'main' && 'production' || 'ref/' }}${{ github.ref_name }} + url: ${{ github.ref_name == 'main' && needs.configure.outputs.base_url || format('{0}/ref/{1}', needs.configure.outputs.base_url, github.ref_name) }} runs-on: ubuntu-latest steps: @@ -117,18 +122,32 @@ jobs: name: dist path: dist - - name: Move artifact to pr folder + - name: Move artifact to root folder + if: github.ref_name == 'main' + run: | + mv dist/* . + + + - name: Move artifact to ref folder + if: github.ref_name != 'main' run: | mkdir -p ref mv dist ref/${{ github.ref_name }} - ls -alRh + + - run: ls -alRh - name: Configure Git run: | git config user.name "${{ github.actor }}" git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" - - name: Stage changes + - name: Stage changes for main branch + if: github.ref_name == 'main' + run: | + git add . + + - name: Stage changes for other branches + if: github.ref_name != 'main' run: | git add ref/${{ github.ref_name }} From 5d296cf6524a17bc446d5217c6e12d724a985680 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 11:42:25 +0100 Subject: [PATCH 13/14] Update PR workflow to move all artifacts to 'ref' directory based on branch context --- .github/workflows/pr.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 19e3abb..fd7972e 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -127,12 +127,11 @@ jobs: run: | mv dist/* . - - name: Move artifact to ref folder if: github.ref_name != 'main' run: | mkdir -p ref - mv dist ref/${{ github.ref_name }} + mv dist/* ref/${{ github.ref_name }} - run: ls -alRh From 1c803776c6b388b3c5db4fc8efb8ccf839036da5 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Fri, 11 Oct 2024 11:44:31 +0100 Subject: [PATCH 14/14] Update PR workflow to remove previous deployment artifacts before moving new ones --- .github/workflows/pr.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index fd7972e..adcc446 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -131,6 +131,7 @@ jobs: if: github.ref_name != 'main' run: | mkdir -p ref + rm -r ref/${{ github.ref_name }} || echo "No previous deployment found" mv dist/* ref/${{ github.ref_name }} - run: ls -alRh