Repository navigation
218 lines (198 loc) · 8.7 KB
/
Copy pathgitlab-e2e.yml
File metadata and controls
218 lines (198 loc) · 8.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
name: GitLab E2E
run-name: "GitLab E2E: ${{ github.event.pull_request.head.sha || inputs.setup_ref || github.sha }} / ${{ inputs.suite || 'full' }}"
on:
pull_request:
branches: [main]
types: [labeled]
workflow_dispatch:
inputs:
pull_request:
description: "Optional PR number. Requires setup_ref to be its reviewed head SHA."
required: false
default: ""
setup_ref:
description: "Exact commit SHA or release tag. If empty, test this workflow commit."
required: false
default: ""
suite:
description: "Select the GitLab test suite."
type: choice
options: [full, required]
default: full
vite_plus_version:
description: "Vite+ version or dist-tag to install."
required: false
default: latest
permissions:
contents: read
pull-requests: read
jobs:
gitlab-e2e:
name: GitLab E2E
if: >-
(github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') ||
(github.event_name == 'pull_request' && github.event.action == 'labeled' &&
github.event.label.name == 'run-e2e' &&
github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-latest
timeout-minutes: 35
# Fork PRs use manual dispatch on main. Never check out or execute PR files.
steps:
- name: Resolve test parameters
id: parameters
env:
EVENT_NAME: ${{ github.event_name }}
EVENT_ACTION: ${{ github.event.action }}
EVENT_LABEL: ${{ github.event.label.name }}
LABEL_ACTOR: ${{ github.event.sender.login }}
PR_NUMBER: ${{ github.event.pull_request.number || inputs.pull_request }}
SETUP_REF: ${{ github.event.pull_request.head.sha || inputs.setup_ref }}
TEST_SUITE: ${{ inputs.suite }}
VITE_PLUS_VERSION: ${{ inputs.vite_plus_version }}
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
case "$EVENT_NAME" in
workflow_dispatch) ;;
pull_request)
if [ "$EVENT_ACTION" != labeled ] || [ "$EVENT_LABEL" != run-e2e ] ||
! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ && "$SETUP_REF" =~ ^[0-9a-f]{40}$ ]]; then
echo "::error::GitLab E2E requires a run-e2e label event with an exact PR head SHA."
exit 1
fi
# Check the original labeler, including when someone reruns this job.
permission="$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${LABEL_ACTOR}/permission" --jq '.permission')"
case "$permission" in
admin | write) ;;
*) echo "::error::Adding run-e2e requires repository write access."; exit 1 ;;
esac
TEST_SUITE=full
VITE_PLUS_VERSION=latest
;;
*) echo "::error::Unsupported GitLab E2E event."; exit 1 ;;
esac
setup_vp_ref="${SETUP_REF:-$GITHUB_SHA}"
suite="${TEST_SUITE:-full}"
vite_plus_version="${VITE_PLUS_VERSION:-latest}"
ref_pattern='^([0-9a-f]{40}|v[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)?)$'
if ! [[ "$setup_vp_ref" =~ $ref_pattern ]]; then
echo "::error::setup_ref must be an exact commit SHA or release tag."
exit 1
fi
case "$suite" in
full | required) ;;
*) echo "::error::Select the full or required suite."; exit 1 ;;
esac
if [[ "$vite_plus_version" == *$'\n'* || "$vite_plus_version" == *$'\r'* ]]; then
echo "::error::vite_plus_version must be a single line."
exit 1
fi
if [ -n "$PR_NUMBER" ]; then
if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ && "$SETUP_REF" =~ ^[0-9a-f]{40}$ ]]; then
echo "::error::Enter a valid PR number and its reviewed 40-character head SHA."
exit 1
fi
pull_request="$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}")"
if ! jq -e --arg sha "$SETUP_REF" --arg repo "$GITHUB_REPOSITORY" \
'.state == "open" and .head.sha == $sha and .head.repo != null and
.base.repo.full_name == $repo and .base.ref == "main"' \
<<< "$pull_request" > /dev/null; then
echo "::error::The PR changed or no longer targets main. Review its current head and start a new run."
exit 1
fi
if [ "$EVENT_NAME" = pull_request ] && ! jq -e --arg repo "$GITHUB_REPOSITORY" \
'.head.repo.full_name == $repo and any(.labels[]; .name == "run-e2e")' \
<<< "$pull_request" > /dev/null; then
echo "::error::Label requests require a same-repository PR with run-e2e. Use manual dispatch on main for forks."
exit 1
fi
fi
{
echo "setup_vp_ref=$setup_vp_ref"
echo "suite=$suite"
echo "vite_plus_version=$vite_plus_version"
} >> "$GITHUB_OUTPUT"
- name: Trigger GitLab pipeline
id: trigger
env:
GITLAB_PROJECT_ID: "85578524"
GITLAB_TRIGGER_TOKEN: ${{ secrets.GITLAB_TRIGGER_TOKEN }}
SETUP_VP_REF: ${{ steps.parameters.outputs.setup_vp_ref }}
TEST_SUITE: ${{ steps.parameters.outputs.suite }}
VITE_PLUS_VERSION: ${{ steps.parameters.outputs.vite_plus_version }}
run: |
set -euo pipefail
if [ -z "$GITLAB_TRIGGER_TOKEN" ]; then
echo "::error::This repository does not have the GITLAB_TRIGGER_TOKEN secret."
exit 1
fi
response="$(curl --silent --show-error --fail-with-body \
--request POST \
--form-string "token=$GITLAB_TRIGGER_TOKEN" \
--form-string "ref=main" \
--form-string "inputs[setup_vp_ref]=$SETUP_VP_REF" \
--form-string "inputs[suite]=$TEST_SUITE" \
--form-string "inputs[vite_plus_version]=$VITE_PLUS_VERSION" \
"https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/trigger/pipeline")"
pipeline_id="$(jq -er '.id' <<< "$response")"
pipeline_url="$(jq -er '.web_url' <<< "$response")"
echo "Triggered GitLab pipeline: $pipeline_url"
{
echo "pipeline_id=$pipeline_id"
echo "pipeline_url=$pipeline_url"
} >> "$GITHUB_OUTPUT"
{
echo "### GitLab E2E"
echo
echo "- Pipeline: [$pipeline_id]($pipeline_url)"
echo "- setup-vp ref: \`$SETUP_VP_REF\`"
echo "- Suite: \`$TEST_SUITE\`"
echo "- Vite+ version: \`$VITE_PLUS_VERSION\`"
} >> "$GITHUB_STEP_SUMMARY"
- name: Wait for GitLab pipeline
env:
GITLAB_PROJECT_ID: "85578524"
GITLAB_PIPELINE_ID: ${{ steps.trigger.outputs.pipeline_id }}
GITLAB_PIPELINE_URL: ${{ steps.trigger.outputs.pipeline_url }}
run: |
set -euo pipefail
api_url="https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/pipelines/${GITLAB_PIPELINE_ID}"
deadline=$((SECONDS + 1800))
previous_status=""
final_status="timed_out"
while [ "$SECONDS" -lt "$deadline" ]; do
if payload="$(curl --silent --show-error --fail --retry 3 --retry-delay 2 "$api_url")"; then
status="$(jq -er '.status' <<< "$payload")"
if [ "$status" != "$previous_status" ]; then
echo "GitLab pipeline status: $status"
previous_status="$status"
fi
case "$status" in
success)
final_status=success
break
;;
failed | canceled | skipped | manual)
final_status="$status"
break
;;
esac
else
echo "The workflow could not read the GitLab pipeline status. It will try again."
fi
sleep 10
done
{
echo "- Result: \`$final_status\`"
} >> "$GITHUB_STEP_SUMMARY"
if [ "$final_status" = "success" ]; then
echo "GitLab pipeline passed: $GITLAB_PIPELINE_URL"
exit 0
fi
echo "GitLab pipeline did not pass: $GITLAB_PIPELINE_URL"
jobs_url="https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/pipelines/${GITLAB_PIPELINE_ID}/jobs?per_page=100"
if jobs="$(curl --silent --show-error --fail --retry 3 --retry-delay 2 "$jobs_url")"; then
jq -r 'sort_by(.name)[] | "\(.status)\t\(.name)\t\(.web_url)"' <<< "$jobs"
fi
echo "::error::GitLab pipeline finished with status $final_status."
exit 1