From 42fa9e5d0114aa412dfdd34e4c758218ea2acc61 Mon Sep 17 00:00:00 2001 From: Mark Youssef Date: Thu, 19 Dec 2024 15:20:40 -0800 Subject: [PATCH] Add github actions to deploy preview pages --- .github/workflows/cleanup-preview.yml | 25 ++++++++++++ .github/workflows/deploy-preview.yml | 59 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 .github/workflows/cleanup-preview.yml create mode 100644 .github/workflows/deploy-preview.yml diff --git a/.github/workflows/cleanup-preview.yml b/.github/workflows/cleanup-preview.yml new file mode 100644 index 0000000..1fafb2f --- /dev/null +++ b/.github/workflows/cleanup-preview.yml @@ -0,0 +1,25 @@ +name: Cleanup PR Preview +on: + pull_request: + types: [closed] + +permissions: + contents: write + +jobs: + cleanup: + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: gh-pages + + - name: Delete PR Preview + run: | + git config user.name "GitHub Actions Bot" + git config user.email "<>" + rm -rf pr-preview/pr-${{ github.event.pull_request.number }} + git add . + git commit -m "Remove PR preview for #${{ github.event.pull_request.number }}" + git push origin gh-pages diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml new file mode 100644 index 0000000..7c1d251 --- /dev/null +++ b/.github/workflows/deploy-preview.yml @@ -0,0 +1,59 @@ +name: Deploy PR Preview +on: + pull_request: + types: [opened, synchronize, reopened] + +permissions: + contents: read + pages: write + id-token: write + pull-requests: write + +jobs: + build: + runs-on: ubuntu-22.04 + steps: + - name: Checkout your repository using git + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + # Add PR number to Astro config for base path + - name: Update base path + run: | + echo "export default { base: '/pr-preview/pr-${{ github.event.pull_request.number }}' };" > astro.config.mjs + + - name: Install, build, and upload your site output + uses: withastro/action@44cbafd43567733e3b007918c6e0711480560516 # v3.0.0 + with: + path: dist/pr-${{ github.event.pull_request.number }} + + deploy: + needs: build + runs-on: ubuntu-22.04 + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }}/pr-preview/pr-${{ github.event.pull_request.number }} + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5 + + - name: Find Comment + uses: peter-evans/find-comment@a54c31d9fa01c18cba0a010a6a14bfa61fd9c44c # v3.0.0 + id: find-comment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: "github-actions[bot]" + body-includes: Preview deployment is ready! + + - name: Create or Update Preview Comment + uses: peter-evans/create-or-update-comment@23ff15bd0ed82dd24f88c2d417cd6e8d5f2c203d # v4.0.0 + with: + comment-id: ${{ steps.find-comment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + Preview deployment is ready! + + 🚀 Preview URL: ${{ steps.deployment.outputs.page_url }}/pr-preview/pr-${{ github.event.pull_request.number }} + + _Updated at: ${{ github.event.pull_request.updated_at }}_ + edit-mode: replace