Repository navigation
264 lines (256 loc) · 11.7 KB
/
Copy pathtest-runner.yml
File metadata and controls
264 lines (256 loc) · 11.7 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
permissions:
contents: read
pull-requests: read
issues: read
statuses: write
name: "gha: macOS & Windows"
# Build on pull requests and pushes to `main`. The PR builds will be
# non-blocking for now, but that is configured elsewhere.
on: # zizmor: ignore[dangerous-triggers]
# Start these builds on pushes (think "after the merge") too. Normally there
# are no `ci-gha**` branches in our repository. The contributors to the repo
# can create such branches when testing or troubleshooting builds. In such
# branches we can disable builds (to speed up the testing) or add new ones,
# without impacting the rest of the team.
push:
branches: [ 'ci-gha**', 'main' ]
# Start the build in the context of the target branch. This is considered
# "safe", as the workflow files are already committed. These types of builds
# have access to the secrets in the build, which we need to use the remote
# caches (Bazel and sccache).
pull_request_target:
types:
- opened
- synchronize
- reopened
# Allow trusted maintainers to trigger this workflow on external PRs by
# commenting `/gharun`.
issue_comment:
types: [ created ]
schedule:
- cron: '0 5 * * 1,2,3,4,5'
# Cancel in-progress runs of the workflow if somebody adds a new commit to the
# PR or branch, or if a maintainer posts a new `/gharun` command.
# Concurrency is defined at the job level so that issue comments without `/gharun`
# (which are skipped in pre-flight) do not cancel in-progress builds.
jobs:
pre-flight:
# Save the `ref` of the pull request, so downstream jobs know what to checkout.
if: >-
github.event_name != 'issue_comment' ||
(github.event.issue.pull_request != null && contains(github.event.comment.body, '/gharun'))
concurrency:
group: ${{ github.workflow }}-pre-flight-${{ github.event.pull_request.number || github.event.issue.number || github.head_ref || github.ref }}
cancel-in-progress: true
environment: >-
${{
(github.event_name != 'pull_request_target' && 'internal') ||
(github.event.pull_request.head.repo.full_name == github.repository && 'internal')
}}
name: Save PR ref
runs-on: ubuntu-latest
outputs:
checkout-sha: ${{ steps.save-pull-request.outputs.sha }}
is-trusted: ${{ steps.verify-permissions.outputs.is_trusted }}
steps:
- name: Verify permissions
id: verify-permissions
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
EVENT_NAME: ${{ github.event_name }}
REF: ${{ github.ref }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
USER_TO_CHECK: >-
${{
(github.event_name == 'pull_request_target' && github.event.pull_request.user.login) ||
(github.event_name == 'issue_comment' && (github.event.comment.user.login || github.actor)) ||
github.actor
}}
run: |
set -euo pipefail
if [[ "${EVENT_NAME}" == "issue_comment" ]]; then
permission=$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${USER_TO_CHECK}/permission" --jq '.permission')
echo "Commenter: ${USER_TO_CHECK}, permission: ${permission}"
case "${permission}" in
admin|write|maintain)
echo "Commenter '${USER_TO_CHECK}' is trusted."
echo "is_trusted=true" >> "${GITHUB_OUTPUT}"
;;
*)
echo "::error::User '${USER_TO_CHECK}' is not trusted (permission: '${permission}'). Only collaborators can trigger runs with /gharun."
exit 1
;;
esac
elif [[ "${EVENT_NAME}" == "pull_request_target" ]]; then
permission=$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${USER_TO_CHECK}/permission" --jq '.permission')
echo "Author: ${USER_TO_CHECK}, permission: ${permission}"
case "${permission}" in
admin|write|maintain)
echo "Author '${USER_TO_CHECK}' is trusted."
echo "is_trusted=true" >> "${GITHUB_OUTPUT}"
;;
*)
echo "Author '${USER_TO_CHECK}' is untrusted (permission: '${permission}'). A maintainer must comment '/gharun' on the PR to trigger builds for this commit."
echo "is_trusted=false" >> "${GITHUB_OUTPUT}"
;;
esac
elif [[ "${EVENT_NAME}" == "push" && "${REF}" == refs/heads/ci-gha* ]]; then
permission=$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${USER_TO_CHECK}/permission" --jq '.permission')
echo "Pusher: ${USER_TO_CHECK}, permission: ${permission}"
case "${permission}" in
admin|write|maintain)
echo "User '${USER_TO_CHECK}' is trusted."
echo "is_trusted=true" >> "${GITHUB_OUTPUT}"
;;
*)
echo "::error::User '${USER_TO_CHECK}' is not trusted (permission: '${permission}')."
exit 1
;;
esac
else
echo "is_trusted=true" >> "${GITHUB_OUTPUT}"
fi
- name: Save Pull Request
id: save-pull-request
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
EVENT_NAME: ${{ github.event_name }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PR_NUMBER: ${{ github.event.issue.number }}
FALLBACK_SHA: ${{ github.ref }}
IS_TRUSTED: ${{ steps.verify-permissions.outputs.is_trusted }}
run: |
if [[ "${EVENT_NAME}" == "issue_comment" ]]; then
sha=$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}" --jq '.head.sha')
elif [[ "${EVENT_NAME}" == "pull_request_target" ]]; then
sha="${PR_HEAD_SHA}"
else
sha="${FALLBACK_SHA}"
fi
echo "Resolved checkout SHA: ${sha}"
echo "sha=${sha}" >> "${GITHUB_OUTPUT}"
if [[ "${EVENT_NAME}" == "issue_comment" && "${IS_TRUSTED}" == "true" && -n "${sha}" ]]; then
echo "Setting pending commit status for ${sha}..."
gh api "repos/${GITHUB_REPOSITORY}/statuses/${sha}" \
-f state="pending" \
-f context="gha: macOS & Windows" \
-f description="macOS and Windows builds in progress via /gharun..." \
-f target_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" || true
fi
# Run other jobs once the `pre-flight` job passes. When the `pre-flight`
# job requires approval, these blocks all the other jobs. The jobs are defined
# in separate files to keep the size of this file under control. Note how
# the additional jobs inherit any secrets needed to use the remote caches and
# receive what version to checkout as an input.
macos-bazel:
if: needs.pre-flight.outputs.is-trusted == 'true'
name: macOS-Bazel
needs: [pre-flight]
concurrency:
group: ${{ github.workflow }}-macos-bazel-${{ github.event.pull_request.number || github.event.issue.number || github.head_ref || github.ref }}
cancel-in-progress: true
uses: ./.github/workflows/macos-bazel.yml # zizmor: ignore[secrets-inherit]
with:
checkout-ref: ${{ needs.pre-flight.outputs.checkout-sha }}
bazel-cache-mode: 'READ_WRITE'
execute-integration-tests: true
secrets: inherit # zizmor: ignore[secrets-inherit]
windows-bazel:
# Disabled
if: false
name: Windows-Bazel
needs: [pre-flight]
concurrency:
group: ${{ github.workflow }}-windows-bazel-${{ github.event.pull_request.number || github.event.issue.number || github.head_ref || github.ref }}
cancel-in-progress: true
uses: ./.github/workflows/windows-bazel.yml # zizmor: ignore[secrets-inherit]
with:
checkout-ref: ${{ needs.pre-flight.outputs.checkout-sha }}
bazel-cache-mode: 'READ_WRITE'
execute-integration-tests: true
secrets: inherit # zizmor: ignore[secrets-inherit]
macos-cmake:
# Disabled
if: false
name: macOS-CMake
needs: [pre-flight]
concurrency:
group: ${{ github.workflow }}-macos-cmake-${{ github.event.pull_request.number || github.event.issue.number || github.head_ref || github.ref }}
cancel-in-progress: true
uses: ./.github/workflows/macos-cmake.yml # zizmor: ignore[secrets-inherit]
with:
checkout-ref: ${{ needs.pre-flight.outputs.checkout-sha }}
# Build the full matrix only on push events to the default branch, or
# when PR gets the has a `gha:full-build` label, or when it had the
# label already and it gets a new commit.
full-matrix: |-
${{
github.event_name == 'schedule' ||
github.event_name == 'push' ||
contains(github.event.pull_request.labels.*.name, 'gha:full-build') ||
contains(github.event.issue.labels.*.name, 'gha:full-build')
}}
sccache-mode: 'READ_WRITE'
vcpkg-cache-mode: 'readwrite'
execute-integration-tests: true
secrets: inherit # zizmor: ignore[secrets-inherit]
windows-cmake:
if: needs.pre-flight.outputs.is-trusted == 'true'
name: Windows-CMake
needs: [pre-flight]
concurrency:
group: ${{ github.workflow }}-windows-cmake-${{ github.event.pull_request.number || github.event.issue.number || github.head_ref || github.ref }}
cancel-in-progress: true
uses: ./.github/workflows/windows-cmake.yml # zizmor: ignore[secrets-inherit]
with:
checkout-ref: ${{ needs.pre-flight.outputs.checkout-sha }}
# Build the full matrix only on push events to the default branch, or
# when PR gets the has a `gha:full-build` label, or when it had the
# label already and it gets a new commit.
full-matrix: |-
${{
github.event_name == 'schedule' ||
github.event_name == 'push' ||
contains(github.event.pull_request.labels.*.name, 'gha:full-build') ||
contains(github.event.issue.labels.*.name, 'gha:full-build')
}}
sccache-mode: 'READ_WRITE'
vcpkg-cache-mode: 'readwrite'
execute-integration-tests: true
secrets: inherit # zizmor: ignore[secrets-inherit]
report-status:
name: Report GHA Status
needs: [pre-flight, macos-bazel, windows-cmake]
if: >-
always() &&
needs.pre-flight.result == 'success' &&
needs.pre-flight.outputs.is-trusted == 'true' &&
(github.event_name == 'issue_comment' || github.event_name == 'pull_request_target')
runs-on: ubuntu-latest
steps:
- name: Publish commit status
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_SHA: ${{ needs.pre-flight.outputs.checkout-sha }}
MACOS_BAZEL_RESULT: ${{ needs.macos-bazel.result }}
WINDOWS_CMAKE_RESULT: ${{ needs.windows-cmake.result }}
run: |
if [[ "${MACOS_BAZEL_RESULT}" == "success" && "${WINDOWS_CMAKE_RESULT}" == "success" ]]; then
state="success"
desc="macOS and Windows builds passed"
elif [[ "${MACOS_BAZEL_RESULT}" == "failure" || "${WINDOWS_CMAKE_RESULT}" == "failure" ]]; then
state="failure"
desc="macOS and Windows builds failed"
elif [[ "${MACOS_BAZEL_RESULT}" == "cancelled" || "${WINDOWS_CMAKE_RESULT}" == "cancelled" ]]; then
state="error"
desc="macOS and Windows builds cancelled"
else
state="success"
desc="macOS and Windows builds completed"
fi
echo "Setting commit status on ${PR_SHA} to ${state}: ${desc}"
gh api "repos/${GITHUB_REPOSITORY}/statuses/${PR_SHA}" \
-f state="${state}" \
-f context="gha: macOS & Windows" \
-f description="${desc}" \
-f target_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"