-
Notifications
You must be signed in to change notification settings - Fork 61
245 lines (222 loc) · 9.91 KB
/
pr.yml
File metadata and controls
245 lines (222 loc) · 9.91 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Copyright (c) 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
name: Build HTML preview of PR
on:
# /!\ Warning: using the pull_request_target event to be able to read secrets. But using this event without the cautionary measures described below
# may allow unauthorized GitHub users to open a “pwn request” and exfiltrate secrets.
# As recommended in https://iterative.ai/blog/testing-external-contributions-using-github-actions-secrets,
# we are adding an 'authorize' job that checks if the workflow was triggered from a fork PR. In that case, the "external" environment
# will prevent the job from running until it's approved manually by human intervention.
pull_request_target:
types: [opened, synchronize, reopened, ready_for_review]
branches:
- main
- rhdh-1.**
- 1.**.x
- release-1.**
concurrency:
group: ${{ github.workflow }}-${{ github.event.number || github.event.pull_request.head.ref }}
cancel-in-progress: true
env:
GH_TEAM: rhdh
GH_ORGANIZATION: redhat-developer
jobs:
check-commit-author:
runs-on: ubuntu-latest
outputs:
is_authorized: ${{ steps.check-team-membership.outputs.is_active_member }}
steps:
- name: Generate GitHub App Token
id: app-token
uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
with:
app-id: ${{ secrets.RHDH_GITHUB_APP_ID }}
private-key: ${{ secrets.RHDH_GITHUB_APP_PRIVATE_KEY }}
- name: Check team membership
uses: redhat-developer/rhdh/.github/actions/check-author@main
id: check-team-membership
with:
team: ${{ env.GH_TEAM }}
organization: ${{ env.GH_ORGANIZATION }}
gh_token: ${{ steps.app-token.outputs.token }}
author: ${{ github.event.pull_request.user.login }}
authorize:
# The 'external' environment is configured with the rhdh-content team as required reviewers.
# All the subsequent jobs in this workflow 'need' this job, which will require manual approval for PRs coming from external forks outside of the rhdh team
needs: check-commit-author
environment:
${{ (needs.check-commit-author.outputs.is_authorized == 'true' || github.event.pull_request.head.repo.full_name == github.repository) && 'internal' || 'external' }}
runs-on: ubuntu-latest
steps:
- name: Check if internal PR
id: check
run: |
if [[ "${{ needs.check-commit-author.outputs.is_authorized }}" == "true" ]]; then
echo "✓ Commit author is in rhdh team - using internal environment"
elif [[ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]]; then
echo "✓ Internal PR (not from fork) - using internal environment"
else
echo "✓ External PR from fork from non-rhdh team member - using external environment for security"
fi
adoc_build:
name: Ccutil Build For PR branch preview
runs-on: ubuntu-latest
needs: authorize
permissions:
contents: read
packages: write
pull-requests: write
steps:
- name: Checkout main branch to get trusted build scripts
uses: actions/checkout@v4
with:
ref: main
path: trusted-scripts
- name: Checkout PR branch for content to build
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
path: pr-content
- name: Setup environment
run: |
# update
sudo apt-get update -y || true
# install
sudo apt-get -y -q install podman && podman --version
echo "GIT_BRANCH=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_ENV
- name: Install lychee
run: |
LYCHEE_VERSION="v0.23.0"
curl -sSfL "https://github.com/lycheeverse/lychee/releases/download/lychee-${LYCHEE_VERSION}/lychee-x86_64-unknown-linux-gnu.tar.gz" \
| sudo tar xz -C /usr/local/bin lychee
lychee --version
- name: Restore lychee cache
uses: actions/cache@v4
with:
path: pr-content/.lycheecache
key: lychee-${{ github.event.number }}-${{ github.sha }}
restore-keys: |
lychee-${{ github.event.number }}-
lychee-
- name: Build guides and indexes
id: build
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.RHDH_BOT_TOKEN }}
run: |
echo "Building PR ${{ github.event.pull_request.number }}"
cp trusted-scripts/build/scripts/build-ccutil.sh pr-content/build/scripts/build-ccutil.sh
cp trusted-scripts/build/scripts/build-orchestrator.js pr-content/build/scripts/build-orchestrator.js
cp trusted-scripts/build/scripts/error-patterns.json pr-content/build/scripts/error-patterns.json
cp trusted-scripts/lychee.toml pr-content/lychee.toml
cp trusted-scripts/.lycheeignore pr-content/.lycheeignore
touch pr-content/.lycheecache
cd pr-content
build/scripts/build-ccutil.sh -b "pr-${{ github.event.number }}"
- name: Pull from origin before pushing (if possible)
if: steps.build.outcome == 'success'
run: |
cd pr-content
/usr/bin/git pull origin gh-pages || true
# repo must be public for this to work
- name: Deploy
if: steps.build.outcome == 'success'
uses: peaceiris/actions-gh-pages@v4
# if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.RHDH_BOT_TOKEN }}
publish_branch: gh-pages
keep_files: true
publish_dir: ./pr-content/titles-generated
- name: Post or update PR comment with doc preview link
if: always() && steps.build.outcome != 'skipped'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.RHDH_BOT_TOKEN }}
script: |
const fs = require('fs');
const prNum = context.issue.number;
const previewUrl = `https://redhat-developer.github.io/red-hat-developers-documentation-rhdh/pr-${prNum}/`;
const now = new Date().toLocaleString('en-US', { timeZone: 'UTC' });
const buildOutcome = '${{ steps.build.outcome }}';
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
let body;
if (buildOutcome === 'success') {
body = `Updated preview: ${previewUrl} @ ${now}`;
} else {
let report;
try {
report = JSON.parse(fs.readFileSync('pr-content/build-report.json', 'utf8'));
} catch { report = null; }
if (report) {
const failed = report.results.filter(r => r.status === 'failed');
const details = failed.map(r => {
const errs = r.errors.map(e =>
` **Error:** \`${e.line}\`\n **Cause:** ${e.cause}\n **Fix:** ${e.fix}`
).join('\n\n');
return `### ${r.title}\n${errs}`;
}).join('\n\n');
let cqaDetails = '';
if (report.cqa && report.cqa.status === 'failed') {
const s = report.cqa.stats || {};
cqaDetails = `### CQA (Content Quality Assessment)\n` +
`${s.total} checks: ${s.pass} pass, ${s.fail} fail\n\n`;
if (report.cqa.output) {
cqaDetails += `<details>\n<summary>CQA checklist</summary>\n\n` +
`${report.cqa.output}\n</details>\n\n`;
}
cqaDetails += `Run \`node build/scripts/cqa/index.js --all\` locally for details.\n\n`;
}
body = `## :x: Build failed\n\n` +
`${report.titles.passed}/${report.titles.total} titles built successfully | ` +
`${report.titles.failed} failed | ${report.duration}s\n\n` +
`${details}\n\n` +
`${cqaDetails}` +
`[View full logs](${runUrl}) | ${now}`;
} else {
body = `## :x: Build failed\n\n` +
`The documentation build failed before producing a report.\n` +
`[View full logs](${runUrl}) | ${now}`;
}
}
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
});
const existing = comments.find(c =>
c.body.includes('preview: https://redhat-developer.github.io/') ||
c.body.includes('Build failed')
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
body: body
});
}
- name: Fail job if build failed
if: steps.build.outcome == 'failure'
run: exit 1