The best effor to get the current job ID and URL

This partially addresses https://github.com/actions/runner/issues/2577
This commit is contained in:
Mikhail f. Shiryaev 2023-05-12 12:17:37 +02:00
parent c375f948d0
commit 4b7aa30017
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -1,3 +1,4 @@
import logging
import os
from os import path as p
@ -65,6 +66,32 @@ def GITHUB_JOB_ID() -> str:
):
_GITHUB_JOB_ID = "0"
# FIXME: until it's here, we can't move to reusable workflows
if not _GITHUB_JOB_URL:
# This is a terrible workaround for the case of another broken part of
# GitHub actions. For nested workflows it doesn't provide a proper GITHUB_JOB
# value, but only the final one. So, for `OriginalJob / NestedJob / FinalJob`
# full name, GITHUB_JOB contains only FinalJob
matched_jobs = []
for job in jobs:
nested_parts = job["name"].split(" / ")
if len(nested_parts) <= 1:
continue
if nested_parts[-1] == GITHUB_JOB:
matched_jobs.append(job)
if len(matched_jobs) == 1:
# The best case scenario
_GITHUB_JOB_ID = matched_jobs[0]["id"]
_GITHUB_JOB_URL = matched_jobs[0]["html_url"]
return _GITHUB_JOB_ID
if matched_jobs:
logging.error(
"We could not get the ID and URL for the current job name %s, there "
"are more than one jobs match it for the nested workflows. Please, "
"refer to https://github.com/actions/runner/issues/2577",
GITHUB_JOB,
)
return _GITHUB_JOB_ID