preview-pull-request #10
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: preview-pull-request | |
on: | |
workflow_run: | |
workflows: ["build-validate-publish"] | |
types: | |
- completed | |
jobs: | |
# This workflow contains a single job called "build" | |
build: | |
# The type of runner that the job will run on | |
runs-on: ubuntu-22.04 | |
defaults: | |
run: | |
shell: bash | |
# Steps represent a sequence of tasks that will be executed as part of the job | |
steps: | |
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | |
- uses: actions/checkout@v3 | |
# Extracts PR from Head SHA | |
- name: Find PR | |
if: ${{ github.event.workflow_run.event == 'pull_request' }} | |
run: | | |
PR=$(curl https://api.github.com/search/issues?q=${{ github.event.workflow_run.head_sha }} | | |
grep -Po "(?<=${{ github.event.workflow_run.repository.full_name }}\/pulls\/)\d*" | head -1) | |
echo "This PR is #$PR of ${{ github.event.workflow_run.repository.name }} from ${{ github.event.workflow_run.repository.owner.login }}. Therefore, https://github.com/${{ github.event.workflow_run.repository.owner.login }}/${{ github.event.workflow_run.repository.name }}/pull/$PR should be the location of the PR. If no longer valid, please provide feedback." | |
echo "PR=$PR" >> $GITHUB_ENV | |
# Replaces base specs with head specs | |
- name: Pull PR | |
if: ${{ github.event.workflow_run.event == 'pull_request' && env.PR }} | |
# Note, do not add python scripts here due to easy private key access. | |
run: | | |
git fetch https://github.com/${{ github.event.workflow_run.head_repository.full_name }} ${{ github.event.workflow_run.head_sha }} | |
git ls-tree ${{ github.event.workflow_run.head_sha }} --full-tree --name-only -r spec/ wgsl/ explainer/ correspondence/ | grep "\.\(bs\|include\|svg\|png\|jpg\)$" | xargs git checkout ${{ github.event.workflow_run.head_sha }} -- | |
# Extracts preview repository | |
# This step exists as preparation for GitHub Actions non-secret environment variables per repository. | |
# Until then, creating a repository with this exact name is necessary. Secret environment variables are | |
# well-protected and tracked by GitHub, so it is not possible to publicize them easily and reliably | |
# as a source of configuration. | |
- name: Find PR Preview Repo | |
if: ${{ github.event.workflow_run.event == 'pull_request' && env.PR }} | |
env: | |
PR_PREVIEW_REPO: "gpuweb-pr-previews" | |
run: | | |
if curl -f -s -o /dev/null https://github.com/${{ github.event.workflow_run.repository.owner.login }}/$PR_PREVIEW_REPO ; then echo "Previews will live in https://${{ github.event.workflow_run.repository.owner.login }}.github.io/$PR_PREVIEW_REPO/gpuweb/gpuweb/$PR/" ; echo "PR_PREVIEW_REPO=$PR_PREVIEW_REPO" >> $GITHUB_ENV ; else exit 0 ; fi | |
- name: Build and validate Bikeshed with custom action | |
uses: ./tools/custom-action/ | |
with: | |
check-repo-clean: 'OFF' | |
# Adjusts Bikeshed specs | |
- name: Adjust Bikeshed | |
if: ${{ github.event.workflow_run.event == 'pull_request' && env.PR }} | |
run: | | |
export PATH="$(python3 -m site --user-base)/bin:${PATH}" | |
export GHC=https://github.com/gpuweb/gpuweb/blob | |
export GHCU=https://github.com/${{ github.event.workflow_run.head_repository.full_name }}/blob | |
sed -e "s,gpuweb/wgsl/</a><br><a href=\"${GHC}/$(git rev-parse HEAD)/wgsl/index.bs\">${GHC}/$(git rev-parse HEAD)/wgsl/index.bs</a>,gpuweb/wgsl/</a><br><a href=\"${GHCU}/${{ github.event.workflow_run.head_sha }}/wgsl/index.bs\">${GHCU}/${{ github.event.workflow_run.head_sha }}/wgsl/index.bs</a>," out/wgsl/index.html > out/wgsl/index.sed.tmp.html | |
mv out/wgsl/index.sed.tmp.html out/wgsl/index.html | |
sed -e "s,gpuweb.github.io/gpuweb/</a><br><a href=\"${GHC}/$(git rev-parse HEAD)/spec/index.bs\">${GHC}/$(git rev-parse HEAD)/spec/index.bs</a>,gpuweb.github.io/gpuweb/</a><br><a href=\"${GHCU}/${{ github.event.workflow_run.head_sha }}/spec/index.bs\">${GHCU}/${{ github.event.workflow_run.head_sha }}/spec/index.bs</a>," out/index.html > out/index.sed.tmp.html | |
mv out/index.sed.tmp.html out/index.html | |
# Deploys PR to preview | |
- name: Deploy PR | |
id: deployment | |
uses: peaceiris/actions-gh-pages@v3 | |
env: | |
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }} | |
if: ${{ github.event.workflow_run.event == 'pull_request' && env.PR && env.PR_PREVIEW_REPO && env.DEPLOY_KEY != '' }} | |
with: | |
deploy_key: ${{ secrets.DEPLOY_KEY }} | |
publish_dir: out | |
destination_dir: gpuweb/gpuweb/${{ env.PR }} | |
external_repository: ${{ github.event.workflow_run.repository.owner.login }}/${{ env.PR_PREVIEW_REPO }} | |
publish_branch: main | |
keep_files: true | |
- name: Find Previous Comment | |
uses: peter-evans/find-comment@v2 | |
env: | |
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }} | |
if: ${{ github.event.workflow_run.event == 'pull_request' && env.PR && env.PR_PREVIEW_REPO && env.DEPLOY_KEY != '' }} | |
id: previous-comment | |
with: | |
issue-number: ${{ env.PR }} | |
comment-author: 'github-actions[bot]' | |
body-includes: pr;head;sha;base | |
- name: Fetch Preview Commit | |
env: | |
CDN_BASE: "https://raw.githack.com" | |
RAW_BASE: "https://raw.githubusercontent.com" | |
if: ${{ github.event.workflow_run.event == 'pull_request' && env.PR && env.PR_PREVIEW_REPO }} | |
run: | | |
sleep 1 # Wait | |
HASH=$(git ls-remote https://github.com/${{ github.event.workflow_run.repository.owner.login }}/${{ env.PR_PREVIEW_REPO }} HEAD | cut -f1) | |
SUFFIX=${{ github.event.workflow_run.repository.owner.login }}/${{ env.PR_PREVIEW_REPO }}/$HASH/gpuweb/gpuweb/${{ env.PR }} | |
echo "CDN_URL=$CDN_BASE/$SUFFIX" >> $GITHUB_ENV | |
echo "RAW_URL=$RAW_BASE/$SUFFIX" >> $GITHUB_ENV | |
# Comments on PR | |
- name: Comment PR | |
uses: peter-evans/create-or-update-comment@v2 | |
env: | |
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }} | |
if: ${{ github.event.workflow_run.event == 'pull_request' && env.PR && env.PR_PREVIEW_REPO && env.DEPLOY_KEY != '' }} | |
with: | |
comment-id: ${{ steps.previous-comment.outputs.comment-id }} | |
issue-number: ${{ env.PR }} | |
edit-mode: replace | |
body: | | |
Previews, as seen when this [build job](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}) started (${{ github.event.workflow_run.head_sha }}): | |
[**WebGPU**](${{ env.CDN_URL }}/) <sub>[**`webgpu.idl`**](${{ env.RAW_URL }}/webgpu.idl.txt) | [**Explainer**](${{ env.CDN_URL }}/explainer/) | [**Correspondence Reference**](${{ env.CDN_URL }}/correspondence/)</sub> | |
[**WGSL**](${{ env.CDN_URL }}/wgsl/) <sub>[**`grammar.js`**](${{ env.RAW_URL }}/wgsl/grammar/grammar.js) | [**`wgsl.lalr.txt`**](${{ env.RAW_URL }}/wgsl/wgsl.lalr.txt)</sub> | |
<!-- | |
pr;head;sha;base | |
${{ env.PR }};${{ github.event.workflow_run.head_repository.full_name }};${{ github.event.workflow_run.head_sha }};${{ github.event.workflow_run.repository.full_name }} | |
--> |