forked from theforeman/foreman-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
110 lines (91 loc) · 4.18 KB
/
preview.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
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_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
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_DATA=$(cat ./pr.json)" >> $GITHUB_ENV
- name: Download other artifacts
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: 'foreman-docs-html-${{ fromJSON(env.PR_DATA).branch_name }}'});
await script({github, context, core, fs, artifact_name: 'foreman-docs-html-base'});
await script({github, context, core, fs, artifact_name: 'foreman-docs-web-master'});
- 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: |
mkdir -p preview/${{ fromJSON(env.PR_DATA).target_name }}
unzip -d preview foreman-docs-web-master.zip
unzip -d preview/${{ fromJSON(env.PR_DATA).target_name }} foreman-docs-html-${{ fromJSON(env.PR_DATA).branch_name }}.zip
- name: Create diff to old base
run: |
mkdir old
unzip -d old foreman-docs-html-base.zip
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