Skip to content

Preview

Preview #3

Workflow file for this run

name: Preview
on:
workflow_run:
workflows:
- Build
- test
types:
- completed
jobs:
preview-failed:
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion != 'success'
runs-on: ubuntu-latest
steps:
- name: Download metadata artifact
uses: actions/github-script@v7
with:
script: |
const script = require('./.github/download_workflow_run_artifact.js');
let fs = require('fs');
await script({github, context, core, fs, artifact_name: 'pr'});
- name: Unzip artifact
run: unzip pr.zip
- name: Read PR data
run: echo "PR_NUMBER=$(cat ./pr)" >> $GITHUB_ENV
- name: Comment on PR
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ env.PR_NUMBER }}
message: "The PR preview for ${{ github.event.workflow_run.head_sha }} could not be generated"
preview:
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
# TODO: can this download from the existing pages or a cache?
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Build current pages
run: |
npm install
npx honkit build
mv _book current
- name: Download metadata artifact
uses: actions/github-script@v7
with:
script: |
const script = require('./.github/download_workflow_run_artifact.js');
let fs = require('fs');
await script({github, context, core, fs, artifact_name: 'github-pages'});
- name: Unpack preview
run: unzip github-pages.zip -d preview
- name: Remove indeterminism
run: |
find current/ preview/ -type f -exec sed -i '/gitbook.page.hasChanged/d' {} +
- name: Create diff to current
run: |
diff -Nrwu --exclude search_index.json current/ preview/ | cat > preview/diff.patch
if [[ -s preview/diff.patch ]] ; then
sudo apt-get update
sudo apt-get install -y diffstat python3-pygments
pygmentize -o preview/diff.html -l diff -f html -O full preview/diff.patch
diffstat -l -p2 preview/diff.patch > diff.txt
fi
- name: Download metadata artifact
uses: actions/github-script@v7
with:
script: |
const script = require('./.github/download_workflow_run_artifact.js');
let fs = require('fs');
await script({github, context, core, fs, artifact_name: 'pr'});
- name: Unzip artifact
run: unzip pr.zip
- name: Read PR data
run: echo "PR_NUMBER=$(cat ./pr)" >> $GITHUB_ENV
- name: Set preview domain
run: echo "PREVIEW_DOMAIN=$(echo ${{ github.repository }} | tr / - )-${{ github.job }}-pr-${{ env.PR_NUMBER }}.surge.sh" >> $GITHUB_ENV
- name: Install surge
run: npm install surge
- name: Deploy to surge.sh
run: npx surge ./preview $PREVIEW_DOMAIN --token ${{ secrets.SURGE_TOKEN }}
- name: Generate summary
run: |
echo "The PR preview for ${{ github.event.workflow_run.head_sha }} is available at [${{ env.PREVIEW_DOMAIN }}](https://${{ env.PREVIEW_DOMAIN }})" >> pr.md
echo "" >> pr.md
if [[ -f preview/diff.txt ]] ; then
echo "The following output files are affected by this PR:" >> pr.md
sed -E "s#(.*)#- [\1](https://${{ env.PREVIEW_DOMAIN }}/\1)#" preview/diff.txt >> pr.md
else
echo "No diff compared to the current website" >> pr.md
fi
if [[ -s preview/diff.patch ]] ; then
echo "" >> pr.md
echo "[show diff](https://${{ env.PREVIEW_DOMAIN }}/diff.patch)" >> pr.md
fi
if [[ -f preview/diff.html ]] ; then
echo "" >> pr.md
echo "[show diff as HTML](https://${{ env.PREVIEW_DOMAIN }}/diff.html)" >> pr.md
fi
- name: Comment on PR
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ env.PR_NUMBER }}
path: pr.md