Skip to content

Commit 89409f8

Browse files
authored
GitLab CI: No need to wait for gitlab pipeline to finish (#4797)
We have switched to using amrex-gitlab-ci-reporter to report back the status.
1 parent 4fa60fb commit 89409f8

File tree

3 files changed

+26
-53
lines changed

3 files changed

+26
-53
lines changed

.github/workflows/trigger-hpsf-gitlab-ci.yml

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ jobs:
6161
PIPELINE_URL="https://gitlab.spack.io/${{ env.GITLAB_PROJECT_PATH }}/-/pipelines/$PIPELINE_ID"
6262
echo "Pipeline URL: $PIPELINE_URL"
6363
echo "pipeline_url=$PIPELINE_URL" >> $GITHUB_OUTPUT
64-
echo "pipeline_id=$PIPELINE_ID" >> $GITHUB_OUTPUT
6564
6665
- name: Post status to GitHub PR
6766
shell: bash
@@ -74,44 +73,3 @@ jobs:
7473
-f body="$COMMENT" || echo "Failed to post comment to GitHub"
7574
env:
7675
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77-
78-
- name: Wait for GitLab pipeline to finish
79-
id: wait
80-
shell: bash
81-
run: |
82-
PIPELINE_ID=${{ steps.trigger.outputs.pipeline_id }}
83-
84-
echo "Waiting on GitLab pipeline $PIPELINE_ID..."
85-
86-
STATUS="running"
87-
while [[ "$STATUS" == "running" || "$STATUS" == "pending" ]]; do
88-
sleep 900 # 15 minutes
89-
90-
STATUS=$(curl -s \
91-
"https://gitlab.spack.io/api/v4/projects/${{ env.GITLAB_PROJECT_ID }}/pipelines/$PIPELINE_ID" \
92-
| jq -r '.status')
93-
94-
if [ "$STATUS" == "null" ] || [ -z "$STATUS" ]; then
95-
echo "Failed to get pipeline status. Response: $RESPONSE"
96-
echo "final_status=error" >> $GITHUB_OUTPUT
97-
exit 1
98-
fi
99-
100-
echo "Status: $STATUS"
101-
done
102-
103-
echo "final_status=$STATUS" >> $GITHUB_OUTPUT
104-
105-
- name: Post result back to GitHub PR
106-
shell: bash
107-
run: |
108-
STATUS=${{ steps.wait.outputs.final_status }}
109-
PIPELINE_ID=${{ steps.trigger.outputs.pipeline_id }}
110-
PIPELINE_URL=${{ steps.trigger.outputs.pipeline_url }}
111-
COMMENT="GitLab CI ${PIPELINE_ID} finished with status: **$STATUS**. See details at ${PIPELINE_URL}."
112-
113-
gh api \
114-
repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments \
115-
-f body="$COMMENT" || echo "Failed to post comment to GitHub"
116-
env:
117-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitlab/README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ pipeline: `.gitlab/hpsf-gitlab-ci.yml`. The PR comment triggered job pulls
3636
the PR branch from GitHub first before running tests. For this approach to
3737
work, we store a pipeline trigger (obtained from GitLab's `Settings -> CI/CD
3838
-> Pipeline trigger tokens`) as a secret at GitHub's `Settings -> Secrets
39-
and variables -> Actions -> Repository secrets`. The GitHub workflow waits
40-
for the result of the GitLab pipeline result and posts the final status and
41-
a link to the result as a comment.
39+
and variables -> Actions -> Repository secrets`.
40+
41+
After the GitLab pipeline finishes, its `.post` stage will post the final
42+
status and a link to the results back to the GitHub PR as a comment. This is
43+
done through a GitHub App that we built and installed in the AMReX
44+
repository. The App was created via
45+
https://github.com/organizations/AMReX-Codes/settings/apps. It does not need
46+
"Webhook" access. For repository permissions, it only needs read & write to
47+
pull requests so it can create PR comments. The app requires a private key,
48+
which you generate during the setup. After the app was installed in the
49+
amrex repository, we got an installation ID. We then stored the app ID,
50+
installation ID and the private key in GitLab's `Settings -> CI/CD ->
51+
Variables`. The app ID isn't a secret. So you can store it as clear text. In
52+
fact, GitLab does not allow 7-digit masked variables anyway. The
53+
installation ID is also not senstive, but nevertheless we stored it as
54+
protected and masked. The private key is a secret that must be protected and
55+
masked. We also diabled "Expand" for all of these variables because the CI
56+
script doesn't need variable expansion. GitLab seems to have a bug that
57+
prevents saving the private key as a multi-line value, and saving it as a
58+
file didn't work either. So we encoded it with `base64 -w0` to turn it into
59+
a single line. That's why in the GitLab CI script we have to decode it.

.gitlab/hpsf-gitlab-ci.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ post_github_comment:
136136
if job['name'] == current_job_name:
137137
continue
138138
139-
if job['status'] in ['failed', 'canceld']:
139+
if job['status'] in ['failed', 'canceled']:
140140
final_status = 'failed'
141141
break # Found a failure, no need to check further
142142
@@ -160,13 +160,12 @@ post_github_comment:
160160
161161
# Get installation access token
162162
installation_id = os.environ['GITHUB_APP_INSTALLATION_ID']
163-
headers = {
164-
'Authorization': f'Bearer {token}',
165-
'Accept': 'application/vnd.github+json'
166-
}
167163
token_response = requests.post(
168164
f'https://api.github.com/app/installations/{installation_id}/access_tokens',
169-
headers=headers
165+
headers = {
166+
'Authorization': f'Bearer {token}',
167+
'Accept': 'application/vnd.github+json'
168+
}
170169
)
171170
if token_response.status_code != 201:
172171
print(f"Failed to get access token: HTTP {token_response.status_code}")
@@ -186,11 +185,9 @@ post_github_comment:
186185
},
187186
json={'body': comment_body}
188187
)
189-
del token_response
190188
if comment_response.status_code != 201:
191189
print(f"Failed to post comment: HTTP {comment_response.status_code}")
192190
sys.exit(1)
193-
del comment_response
194191
print("Successfully posted comment to GitHub PR")
195192
EOF
196193
when: always

0 commit comments

Comments
 (0)