Skip to content

Commit

Permalink
docs preview
Browse files Browse the repository at this point in the history
Signed-off-by: zirain <[email protected]>
  • Loading branch information
zirain committed Jan 12, 2025
1 parent 00ed0b8 commit e2d4f6a
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/docs-preview-build.yml
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions .github/workflows/docs-preview-clean.yml
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions .github/workflows/docs-preview-deploy.yml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e2d4f6a

Please sign in to comment.