From e2d4f6afde553fa4566ec229a509cafcf2882f18 Mon Sep 17 00:00:00 2001 From: zirain Date: Sat, 11 Jan 2025 20:20:37 +0800 Subject: [PATCH] docs preview Signed-off-by: zirain --- .github/workflows/docs-preview-build.yml | 67 +++++++++++++++++++++++ .github/workflows/docs-preview-clean.yml | 37 +++++++++++++ .github/workflows/docs-preview-deploy.yml | 66 ++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 .github/workflows/docs-preview-build.yml create mode 100644 .github/workflows/docs-preview-clean.yml create mode 100644 .github/workflows/docs-preview-deploy.yml diff --git a/.github/workflows/docs-preview-build.yml b/.github/workflows/docs-preview-build.yml new file mode 100644 index 00000000000..a19f4198946 --- /dev/null +++ b/.github/workflows/docs-preview-build.yml @@ -0,0 +1,67 @@ +# This action builds and deploys egui_demo_app on each pull request created +# Security notes: +# The preview deployment is split in two workflows, preview_build and preview_deploy. +# `preview_build` runs on pull_request, so it won't have any access to the repositories secrets, so it is safe to +# build / execute untrusted code. +# `preview_deploy` has access to the repositories secrets (so it can push to the pr preview repo) but won't run +# any untrusted code (it will just extract the build artifact and push it to the pages branch where it will +# automatically be deployed). + +name: Preview Build + +on: + pull_request: + branches: + - main + paths: + - "site/**" + - "tools/make/docs.mk" + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Git checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + submodules: true + ref: ${{ github.event.pull_request.head.sha }} + + # - uses: ./tools/github-actions/setup-deps + + - name: Setup Hugo + uses: peaceiris/actions-hugo@75d2e84710de30f6ff7268e08f310b60ef14033f # v3.0.0 + with: + hugo-version: "latest" + extended: true + + - name: Setup Node + uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0 + with: + node-version: "18" + + - name: Install Site Dependencies and Build Site + run: make docs + + # Upload docs for GitHub Pages + - name: Upload GitHub Pages artifact + uses: actions/upload-artifact@v4 + with: + name: gateway_docs + path: site/public + + - name: Generate meta.json + env: + PR_NUMBER: ${{ github.event.number }} + URL_SLUG: ${{ github.event.number }}-${{ github.head_ref }} + run: | + # Sanitize the URL_SLUG to only contain alphanumeric characters and dashes + URL_SLUG=$(echo $URL_SLUG | tr -cd '[:alnum:]-') + echo "{\"pr_number\": \"$PR_NUMBER\", \"url_slug\": \"$URL_SLUG\"}" > meta.json + + - uses: actions/upload-artifact@v4 + with: + name: meta.json + path: meta.json diff --git a/.github/workflows/docs-preview-clean.yml b/.github/workflows/docs-preview-clean.yml new file mode 100644 index 00000000000..2dc5a177539 --- /dev/null +++ b/.github/workflows/docs-preview-clean.yml @@ -0,0 +1,37 @@ +name: Preview Cleanup + +permissions: + contents: write + +on: + pull_request_target: + types: + - closed + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - run: mkdir -p empty_dir + - name: Generate URL_SLUG + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + URL_SLUG: ${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.ref }} + run: | + # Sanitize the URL_SLUG to only contain alphanumeric characters and dashes + URL_SLUG=$(echo $URL_SLUG | tr -cd '[:alnum:]-') + echo "URL_SLUG=$URL_SLUG" >> $GITHUB_ENV + + - name: Deploy + uses: JamesIves/github-pages-deploy-action@v4 + with: + folder: empty_dir + repository-name: zirain/eg-pr-preview + branch: "main" + clean: true + target-folder: ${{ env.URL_SLUG }} + ssh-key: ${{ secrets.DEPLOY_KEY }} + commit-message: "Remove preview for PR ${{ env.URL_SLUG }}" + single-commit: true diff --git a/.github/workflows/docs-preview-deploy.yml b/.github/workflows/docs-preview-deploy.yml new file mode 100644 index 00000000000..ece8240cb61 --- /dev/null +++ b/.github/workflows/docs-preview-deploy.yml @@ -0,0 +1,66 @@ +name: Preview Deploy + +permissions: + contents: write + pull-requests: write + +on: + workflow_run: + workflows: + - "Preview Build" + types: + - completed + +# Since we use single_commit and force on the deploy action, only one deploy action can run at a time. +# Should this create a bottleneck we might have to set single_commit and force to false which should allow +# for the deployments to run in parallel. +concurrency: + group: preview_deploy + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: "Download build artifact" + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 + with: + name: gateway_docs + path: gateway_docs_artifact + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + + - name: "Download build meta" + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 + with: + name: meta.json + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + + - name: Parse meta.json + run: | + echo "PR_NUMBER=$(jq -r .pr_number meta.json)" >> $GITHUB_ENV + echo "URL_SLUG=$(jq -r .url_slug meta.json)" >> $GITHUB_ENV + + - name: Deploy + uses: JamesIves/github-pages-deploy-action@v4 + with: + folder: gateway_docs_artifact + repository-name: zirain/eg-pr-preview + branch: "main" + clean: true + target-folder: ${{ env.URL_SLUG }} + ssh-key: ${{ secrets.DEPLOY_KEY }} + commit-message: "Update preview for PR ${{ env.URL_SLUG }}" + single-commit: true + + - name: Comment PR + uses: thollander/actions-comment-pull-request@v2 + with: + message: | + Preview available at https://zirain.github.io/eg-pr-preview/${{ env.URL_SLUG }} + Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed. + pr_number: ${{ env.PR_NUMBER }} + comment_tag: "eg-preview"