Repository navigation
291 lines (270 loc) · 12.7 KB
/
Copy pathe2e.yaml
File metadata and controls
291 lines (270 loc) · 12.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
name: End to End Testing
# Managed centrally in upbound/sa-up (gitops/shared-files/functions/workflows) and
# synced to the function-* repos. Edit it there, not in the individual repos.
#
# Runs the repo's end-to-end harness (the up project under e2e/) against a real
# Spaces control plane. The function is built from the PR head, pushed to a dev
# tag, and installed as a pinned dependency of the e2e project. Cloud credentials
# are in-cluster: the function signs in with its pod's Upbound OIDC token (no cloud
# secrets here). See the repo's e2e/README.md.
#
# This job builds and runs code from the pull request with secrets, so it is kept
# narrow: a read-only GITHUB_TOKEN that is not left in the checkout, only the e2e
# robot's Upbound credentials (never the deploy robot's), and re-runs on new
# commits only for branches in this repo. A fork PR needs the label again after
# every push.
#
# Repos without an e2e/ harness yet pass this check trivially, so the workflow can
# be rolled out fleet-wide before every repo has tests.
on:
# https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/
# pull_request_target is potentially dangerous target so we keep it strict
# under the label and benefit from secret propagation
pull_request_target:
types:
- synchronize
- labeled
permissions:
contents: read
# One run per pull request: adding run-e2e-tests again, or a new commit on a
# branch in this repo, replaces an older run of the same PR. Every other event
# (another label, a push to a fork) gets a group of its own, so it can never
# cancel a run in progress.
concurrency:
group: >-
e2e-pr-${{ github.event.pull_request.number }}-${{
(github.event.label.name == 'run-e2e-tests' ||
(github.event.action == 'synchronize' &&
github.event.pull_request.head.repo.full_name == github.repository))
&& 'run' || github.run_id }}
cancel-in-progress: true
env:
# Pinned CLI versions (Renovate in upbound/sa-up bumps them).
UP_VERSION: v0.56.0
XP_CHANNEL: stable
XP_VERSION: v2.5.0
UP_API_TOKEN: ${{ secrets.UP_E2E_API_TOKEN }}
UP_ORG: ${{ secrets.UP_E2E_ORG }}
UP_GROUP: ${{ secrets.UP_E2E_GROUP }}
UP_ROBOT_ID: ${{ secrets.UP_E2E_ROBOT_ID }}
# Throwaway e2e artifacts (the built function + the e2e project) are pushed under
# the e2e org (UP_ORG) the robot owns -- the e2e robot can't push to the
# github.repository (upbound/) namespace.
XPKG_REGISTRY: xpkg.upbound.io
jobs:
e2e:
# Not "e2e": branch protection requires the "e2e" commit status that the
# report job below posts, not this job's check.
name: e2e-run
if: >-
contains(github.event.pull_request.labels.*.name, 'run-e2e-tests') &&
(github.event.label.name == 'run-e2e-tests' ||
(github.event.action == 'synchronize' &&
github.event.pull_request.head.repo.full_name == github.repository))
# up test names the control plane <project>-uptest-<test>, so every run in this
# repo uses the same one: run them one at a time.
concurrency:
group: e2e-control-plane
cancel-in-progress: false
runs-on: ubuntu-24.04
steps:
- name: Checkout
id: checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
# Repos that don't yet have an e2e harness pass without doing anything.
- name: Detect e2e harness
id: harness
run: |
if [ -f e2e/upbound.yaml ] && ls e2e/tests/* >/dev/null 2>&1; then
echo "present=true" >> "$GITHUB_OUTPUT"
else
echo "present=false" >> "$GITHUB_OUTPUT"
echo "No e2e/ harness in this repo; nothing to run."
fi
# The image is built with the Go toolchain pinned in the repo's go.mod
# (`toolchain goX.Y.Z`), which Renovate bumps (security fixes merge
# themselves after e2e). No fallback to the `go` directive: that is only
# the minimum language version and would silently build with an old Go.
- name: Read Go toolchain from go.mod
if: steps.harness.outputs.present == 'true'
run: |
v=$(awk '$1 == "toolchain" { sub(/^go/, "", $2); print $2 }' go.mod)
if [ -z "$v" ]; then
echo "::error file=go.mod::go.mod needs a toolchain directive (e.g. 'toolchain go1.26.11'): the function image is built with that Go version. Keep the go directive lower (the minimum language version); go mod tidy drops a toolchain line equal to it."
exit 1
fi
echo "Building with Go $v"
echo "GO_VERSION=$v" >> "$GITHUB_ENV"
- name: Require e2e credentials
if: steps.harness.outputs.present == 'true'
run: |
missing=""
for v in UP_API_TOKEN UP_ORG UP_GROUP UP_ROBOT_ID; do
[ -n "${!v}" ] || missing="$missing UP_E2E_${v#UP_}"
done
if [ -n "$missing" ]; then
echo "::error::Missing e2e secrets:$missing (managed in solutions-gitops-prod)"
exit 1
fi
- name: Install and login with up
if: steps.harness.outputs.present == 'true'
uses: upbound/action-up@53fe6395637d884c80d2bbc8c2d75d0ece776ced # v1
with:
version: ${{ env.UP_VERSION }}
api-token: ${{ env.UP_API_TOKEN }}
organization: ${{ env.UP_ORG }}
# doesn't work with plain token when pushing otherwise
- name: Login to xpkg with robot
if: steps.harness.outputs.present == 'true'
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4
with:
registry: xpkg.upbound.io
username: ${{ env.UP_ROBOT_ID }}
password: ${{ env.UP_API_TOKEN }}
- name: Setup Docker Buildx
if: steps.harness.outputs.present == 'true'
uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4
# Build the function from the PR head so e2e exercises this PR's code.
- name: Build function runtime image
if: steps.harness.outputs.present == 'true'
uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7
with:
context: .
platforms: linux/amd64
target: image
build-args:
GO_VERSION=${{ env.GO_VERSION }}
outputs: type=docker,dest=runtime-amd64.tar
# Pinned release, verified against the checksum published next to it
# (XP_VERSION is bumped by Renovate in upbound/sa-up). The runner is
# linux/amd64; matrix.arch is only the package's target platform.
- name: Setup the Crossplane CLI
if: steps.harness.outputs.present == 'true'
run: |
base="https://cli.crossplane.io/${XP_CHANNEL}/${XP_VERSION}/bin/linux_amd64"
curl -sSfL "${base}/crossplane" -o crossplane
echo "$(curl -sSfL "${base}/crossplane.sha256" | cut -d' ' -f1) crossplane" | sha256sum -c -
chmod +x crossplane
./crossplane version --client
- name: Build and push the function dev package
if: steps.harness.outputs.present == 'true'
run: |
set -euo pipefail
./crossplane xpkg build --package-file=function.xpkg \
--package-root=package/ --embed-runtime-image-tarball=runtime-amd64.tar
REPO="${GITHUB_REPOSITORY##*/}"
REPO="${REPO,,}"
DEV_VERSION="v0.0.0-e2e.$(git rev-parse --short=12 HEAD)"
DEV_PKG="${XPKG_REGISTRY}/${UP_ORG}/${REPO}:${DEV_VERSION}"
./crossplane xpkg push --package-files function.xpkg "${DEV_PKG}"
echo "FUNCTION_VERSION=${DEV_VERSION}" >> "$GITHUB_ENV"
# Replace the @FUNCTION_VERSION@ sentinel in the e2e project's dependsOn with
# the dev tag just pushed, so the test configuration installs this PR's build.
- name: Pin e2e project to dev package
if: steps.harness.outputs.present == 'true'
run: |
set -euo pipefail
sed -i "s|@FUNCTION_VERSION@|${FUNCTION_VERSION}|g" e2e/upbound.yaml
grep -n -B2 'version:' e2e/upbound.yaml
- name: Switch up context
id: ctx
if: steps.harness.outputs.present == 'true'
run: up ctx ${{ env.UP_ORG }}/upbound-gcp-us-central-1/${{ env.UP_GROUP }}
# up test names its control plane <project>-uptest-<test>. Runs are serialized
# per repo, so one that exists now was left behind by a cancelled run and
# would collide with this one: delete it and wait until it is gone. Fails
# the run if the control planes can't be listed.
- name: Delete leftover e2e control planes
if: steps.harness.outputs.present == 'true' && steps.ctx.outcome == 'success'
working-directory: e2e
env:
CLEANUP: before
run: &delete-control-planes |
# One script for both cleanup steps (YAML anchor). up ctp list ignores
# --group and lists the group of the current up context, which the
# "Switch up context" step set to UP_GROUP.
set -euo pipefail
strict=true
[ "${CLEANUP}" = after ] && strict=false
fail() {
if ${strict}; then echo "::error::$*"; exit 1; fi
echo "::warning::$*"; exit 0
}
PROJECT=$(awk '/^metadata:/{m=1; next} m && /^ name:/{print $2; exit}' upbound.yaml)
[ -n "${PROJECT}" ] || fail "no metadata.name in upbound.yaml"
stale() {
local out
out=$(up ctp list --format json) || return 1
# An empty group prints this text instead of JSON.
[ "${out}" = "No control planes found" ] && return 0
jq -r --arg p "${PROJECT}-uptest-" '.[].metadata.name | select(startswith($p))' <<<"${out}"
}
names=$(stale) || fail "cannot list control planes"
for c in ${names}; do
echo "Deleting control plane ${c}"
up ctp delete "${c}" || echo "::warning::cannot delete ${c}"
done
${strict} || exit 0
# Before a run: wait until they are gone, or up test would collide.
for _ in $(seq 1 60); do
names=$(stale) || fail "cannot list control planes"
[ -z "${names}" ] && exit 0
sleep 10
done
fail "control planes still being deleted: ${names//$'\n'/ }"
# The wrapper project is pushed to a SEPARATE repo from the function: both
# the project and the function-under-test would otherwise share the same
# source in Crossplane's package lock, which collides ("node already
# exists"). The <repo>-e2e-wrapper repo is granted to the e2e robot in
# solutions-gitops-prod.
#
# TMPDIR=e2e/.tmp: up test copies each test manifest into a new directory
# directly under $TMPDIR, and uptest resolves hook annotations
# (uptest.upbound.io/pre-assert-hook etc.) relative to that copy. With
# TMPDIR here, a hook path like ../../tests/<test>/hook.sh points at the
# script in the repo.
- name: Run e2e tests
if: steps.harness.outputs.present == 'true'
working-directory: e2e
run: |
REPO="${GITHUB_REPOSITORY##*/}"
REPO="${REPO,,}"
mkdir -p .tmp
TMPDIR="$PWD/.tmp" up test run tests/* --e2e --public \
--repository "${XPKG_REGISTRY}/${UP_ORG}/${REPO}-e2e-wrapper"
# Also after a failed or cancelled run, so nothing is left behind. Best
# effort: a cleanup problem only warns here.
- name: Delete e2e control planes
if: always() && steps.ctx.outcome == 'success'
working-directory: e2e
env:
CLEANUP: after
run: *delete-control-planes
# Branch protection requires the "e2e" commit status, not a job check. GitHub
# reports a skipped job as success, and every event that doesn't start a run
# (another label, a push to a fork) skips the e2e job. Only a run that actually
# finished posts "e2e", so a skipped run can't turn a failed e2e green, and a
# new commit has no "e2e" until a run passes on it. This job runs no code from
# the pull request, so it is the only one that can write statuses.
report:
name: e2e-report
needs: e2e
if: always() && (needs.e2e.result == 'success' || needs.e2e.result == 'failure')
permissions:
statuses: write
runs-on: ubuntu-24.04
steps:
- name: Post the e2e commit status
env:
GH_TOKEN: ${{ github.token }}
RESULT: ${{ needs.e2e.result }}
SHA: ${{ github.event.pull_request.head.sha }}
run: |
gh api -X POST "repos/${GITHUB_REPOSITORY}/statuses/${SHA}" \
-f state="${RESULT}" \
-f context=e2e \
-f description="End to End Testing: ${RESULT}" \
-f target_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"