preview #14602
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 | |
on: | |
workflow_run: | |
workflows: | |
- deploy | |
types: | |
- completed | |
jobs: | |
preview-failed: | |
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion != 'success' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Download metadata artifact | |
run: gh run download '${{ github.event.workflow_run.id }}' -n pr | |
env: | |
GH_TOKEN: ${{ github.token }} | |
- name: Read PR data | |
run: echo "PR_DATA=$(cat ./pr.json)" >> $GITHUB_ENV | |
- name: Comment on PR | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
number: ${{ fromJSON(env.PR_DATA).pr_number }} | |
message: "The PR preview for ${{ fromJSON(env.PR_DATA).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: | |
- uses: actions/checkout@v4 | |
- name: Download metadata artifact | |
run: gh run download '${{ github.event.workflow_run.id }}' -n pr | |
env: | |
GH_TOKEN: ${{ github.token }} | |
- name: Read PR data | |
run: echo "PR_DATA=$(cat ./pr.json)" >> $GITHUB_ENV | |
- name: Set preview domain | |
run: echo "PREVIEW_DOMAIN=$(echo ${{ github.repository }} | tr / - )-${{ github.job }}-pr-${{ fromJSON(env.PR_DATA).pr_number }}.surge.sh" >> $GITHUB_ENV | |
- name: Install surge | |
run: npm install surge | |
- name: Install diffstat and pygments | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y diffstat python3-pygments | |
- name: Create preview layout | |
run: | | |
gh run download '${{ github.event.worfklow_run.id }}' --name 'foreman-docs-web-master' --dir preview | |
gh run download '${{ github.event.worfklow_run.id }}' --name 'foreman-docs-html-${{ fromJSON(env.PR_DATA).branch_name }}' --dir 'preview/${{ fromJSON(env.PR_DATA).target_name }}' | |
env: | |
GH_TOKEN: ${{ github.token }} | |
- name: Create old base | |
run: gh run download '${{ github.event.worfklow_run.id }}' --name foreman-docs-html-base --dir old | |
env: | |
GH_TOKEN: ${{ github.token }} | |
- name: Create diff to old base | |
run: | | |
diff -Nrwu old/ preview/${{ fromJSON(env.PR_DATA).target_name }}/ | cat > preview/diff.patch | |
pygmentize -o preview/diff.html -l diff -f html -O full preview/diff.patch | |
diffstat -l -p2 preview/diff.patch > diff.txt | |
- name: Deploy to surge.sh | |
run: ./node_modules/.bin/surge ./preview $PREVIEW_DOMAIN --token ${{ secrets.SURGE_TOKEN }} | |
- name: Generate summary | |
run: | | |
echo "The PR preview for ${{ fromJSON(env.PR_DATA).head_sha }} is available at [${{ env.PREVIEW_DOMAIN }}](https://${{ env.PREVIEW_DOMAIN }})" >> pr.md | |
echo "" >> pr.md | |
echo "The following output files are affected by this PR:" >> pr.md | |
sed -E "s#(.*)#- [\1](https://${{ env.PREVIEW_DOMAIN }}/${{ fromJSON(env.PR_DATA).target_name }}/\1)#" diff.txt >> pr.md | |
echo "" >> pr.md | |
echo '[show diff](https://${{ env.PREVIEW_DOMAIN }}/diff.patch)' >> pr.md | |
echo "" >> pr.md | |
echo '[show diff as HTML](https://${{ env.PREVIEW_DOMAIN }}/diff.html)' >> pr.md | |
- name: Comment on PR | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
number: ${{ fromJSON(env.PR_DATA).pr_number }} | |
path: pr.md |