Skip to content

Commit 4fa60fb

Browse files
authored
GitLib CI: Fix CI status check (#4798)
1 parent 56eece6 commit 4fa60fb

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

.gitlab/hpsf-gitlab-ci.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@ post_github_comment:
114114
import sys
115115
import base64
116116
117+
# --- START: Get effective Pipeline Status from GitLab API ---
118+
gitlab_api_url = os.environ['CI_API_V4_URL']
119+
project_id = os.environ['CI_PROJECT_ID']
120+
pipeline_id = os.environ['CI_PIPELINE_ID']
121+
job_token = os.environ['CI_JOB_TOKEN']
122+
current_job_name = os.environ['CI_JOB_NAME']
123+
124+
final_status = 'success' # Assume success until a failure is found
125+
try:
126+
# Get all jobs for the pipeline
127+
api_endpoint = f"{gitlab_api_url}/projects/{project_id}/pipelines/{pipeline_id}/jobs"
128+
headers = {'JOB-TOKEN': job_token}
129+
params = {'scope[]': ['success', 'failed', 'canceled'], 'per_page': 100}
130+
response = requests.get(api_endpoint, headers=headers, params=params)
131+
response.raise_for_status()
132+
133+
jobs = response.json()
134+
for job in jobs:
135+
# We only care about the result of test/build jobs, not this notification job itself
136+
if job['name'] == current_job_name:
137+
continue
138+
139+
if job['status'] in ['failed', 'canceld']:
140+
final_status = 'failed'
141+
break # Found a failure, no need to check further
142+
143+
except requests.exceptions.RequestException as e:
144+
print(f"Error fetching job statuses from GitLab API: {e}", file=sys.stderr)
145+
final_status = 'unknown' # If API fails, report unknown status
146+
# --- END: Get effective Pipeline Status ---
147+
117148
# Generate JWT for GitHub App authentication
118149
app_id = os.environ['GITHUB_APP_ID']
119150
payload = {
@@ -144,9 +175,8 @@ post_github_comment:
144175
# Post comment to PR
145176
pr_number = os.environ.get('GITHUB_PR_NUMBER')
146177
pipeline_url = os.environ['CI_PIPELINE_URL']
147-
pipeline_status = os.environ['CI_PIPELINE_STATUS']
148178
149-
comment_body = f"GitLab pipeline [{pipeline_status}]({pipeline_url})"
179+
comment_body = f"GitLab CI {pipeline_id} finished with status: **{final_status}**. See details at {pipeline_url}."
150180
151181
comment_response = requests.post(
152182
f'https://api.github.com/repos/AMReX-Codes/amrex/issues/{pr_number}/comments',

0 commit comments

Comments
 (0)