Skip to content

feat: add job_name to github context#3489

Closed
qoomon wants to merge 1 commit intoactions:mainfrom
qoomon:patch-1
Closed

feat: add job_name to github context#3489
qoomon wants to merge 1 commit intoactions:mainfrom
qoomon:patch-1

Conversation

@qoomon
Copy link
Copy Markdown

@qoomon qoomon commented Oct 4, 2024

Important

Solved see #3489 (comment) for a stable workaround.


Currently there is no system variable that contains a job id value that can be used to request the github api for a job.
system.jobIdcontains a GUID and therefore is useless for job API endpoint

By providing the real/effective job name (as in the github api response of jobs), we are able to determine and request the current job by using list-jobs-for-a-workflow-run-attempt API endpoint and find the actual job by its name.

const currentJob = await octokit.paginate(octokit.rest.actions.listJobsForWorkflowRunAttempt, {
  owner: 'qoomon',
  repo: 'sandbox',
  run_id: parseInt(process.env.GITHUB_RUN_ID),
  attempt_number: parseInt(process.env.GITHUB_RUN_ATTEMPT),
})
  .then((jobs) => jobs.filter((job) => job.name === process.env.GITHUB_JOB_NAME && job.runner_name === process.env.RUNNER_NAME))
  .then((jobs) => {
    if (jobs.length != 1) { 
      throw new Error('Could not determine current job')
    } else { 
      return
    }
  })

This would solve a lot of community requests e.g., https://github.com/orgs/community/discussions/8945

There was an attempt from attempt already from @TingluoHuang to fix this issue two years ago #1950

Update
It looks like the job.name of the api response job objects getting truncated at 100 characters 🤯.
So to make this work always the api response need to be adjusted as well.

@qoomon qoomon requested a review from a team as a code owner October 4, 2024 11:45
@Felixoid
Copy link
Copy Markdown

Felixoid commented Oct 4, 2024

Hi, thanks for the patch. I am really looking forward to have it merged!

By the way, what do you think about including the job ID? It would help with a bridge from https://api.github.com/repos/ClickHouse/ClickHouse/actions/runs/8363985040/jobs?per_page=1&page=13 to https://api.github.com/repos/ClickHouse/ClickHouse/actions/jobs/22961931602 in no additional actions

@qoomon
Copy link
Copy Markdown
Author

qoomon commented Oct 4, 2024

@Felixoid unfortunately the job id needed for the api is not present, the system.jobId variable is set to a GUID see below

System Variables
 {
    "system.isScheduled": {
      "value": "False"
    },
    "system.servertype": {
      "value": "Hosted"
    },
    "system.culture": {
      "value": ""
    },
    "system.pipelineStartTime": {
      "value": "2024-10-04 11:28:22+00:00"
    },
    "system.runner.lowdiskspacethreshold": {
      "value": "100"
    },
    "DistributedTask.NewActionMetadata": {
      "value": "true"
    },
    "DistributedTask.EnableCompositeActions": {
      "value": "true"
    },
    "DistributedTask.EnhancedAnnotations": {
      "value": "true"
    },
    "DistributedTask.UploadStepSummary": {
      "value": "true"
    },
    "DistributedTask.ForceGithubJavascriptActionsToNode16": {
      "value": "true"
    },
    "DistributedTask.ForceGithubJavascriptActionsToNode20": {
      "value": "true"
    },
    "DistributedTask.AddWarningToNode12Action": {
      "value": "true"
    },
    "DistributedTask.AddWarningToNode16Action": {
      "value": "true"
    },
    "DistributedTask.LogTemplateErrorsAsDebugMessages": {
      "value": "true"
    },
    "DistributedTask.AllowRunnerContainerHooks": {
      "value": "true"
    },
    "DistributedTask.DeprecateStepOutputCommands": {
      "value": "true"
    },
    "DistributedTask.UseWhich2": {
      "value": "true"
    },
    "DistributedTask.DetailUntarFailure": {
      "value": "true"
    },
    "DistributedTask.UseActionArchiveCache": {
      "value": "true"
    },
    "DistributedTask.EnableJobServerQueueTelemetry": {
      "value": "true"
    },
    "DistributedTask.MarkJobAsFailedOnWorkerCrash": {
      "value": "true"
    },
    "Actions.EnableHttpRedirects": {
      "value": "true"
    },
    "system.teamProjectId": {
      "value": "00000000-0000-0000-0000-000000000000"
    },
    "build.buildId": {
      "value": "666"
    },
    "build.containerId": {
      "value": "18431423"
    },
    "system.workflowFilePath": {
      "value": ".github/workflows/example.yaml"
    },
    "system.phaseDisplayName": {
      "value": "build"
    },
    "system.phaseId": {
      "value": "08529e53-a864-5e57-8255-b4ac74128910"
    },
    "system.phaseName": {
      "value": "build"
    },
    "system.phaseAttempt": {
      "value": "1"
    },
    "system.jobIdentifier": {
      "value": "build-with-reusable-workflow-with-matrix._20_x.build.__default"
    },
    "system.jobAttempt": {
      "value": "1"
    },
    "System.JobPositionInPhase": {
      "value": "1"
    },
    "System.TotalJobsInPhase": {
      "value": "1"
    },
    "system.github.job": {
      "value": "build"
    },
    "system.jobDisplayName": {
      "value": "build-with-reusable-workflow-with-matrix (20.x) / build"
    },
    "system.jobId": {
      "value": "0315e721-8f55-564f-db13-292a0596e43b"
    },
    "system.jobName": {
      "value": "__default"
    },
    "system.github.token.permissions": {
      "value": "{\"Contents\":\"read\",\"Metadata\":\"read\",\"Packages\":\"read\"}"
    },
    "system.workflowFileFullPath": {
      "value": "qoomon/actions--context/.github/workflows/example-reusable.yml"
    },
    "system.github.token": {
      "value": "***",
      "isSecret": true
    },
    "system.github.results_endpoint": {
      "value": "https://results-receiver.actions.githubusercontent.com/"
    },
    "system.github.results_upload_with_sdk": {
      "value": "True"
    },
    "system.workflowFileRef": {
      "value": "refs/heads/main"
    },
    "system.workflowFileSha": {
      "value": "d2fec0f6c3c70eea6c8124a0a9458c7aea4565f4"
    },
    "system.runnerEnvironment": {
      "value": "github-hosted"
    },
    "system.orchestrationId": {
      "value": "595b9a62-e373-4b13-ba58-2f92a6c39f3a.build-with-reusable-workflow-with-matrix._20_x.build.__default"
    },
    "system.runnerGroupName": {
      "value": "GitHub Actions"
    }
  }

@Felixoid
Copy link
Copy Markdown

Felixoid commented Oct 4, 2024

I see.

Even a correct jobDisplayName is already a solid solution for the job ID resolving. So, many kudos to you

@Felixoid
Copy link
Copy Markdown

Felixoid commented Oct 4, 2024

Oh, wait. What about a defined env variable for this value?

@qoomon
Copy link
Copy Markdown
Author

qoomon commented Oct 4, 2024

@Felixoid No unfortunately not.

@jsoref
Copy link
Copy Markdown
Contributor

jsoref commented Oct 10, 2024

Thanks for making this PR.

@c3charvat
Copy link
Copy Markdown

This would make life exponentially easier.

@c3charvat
Copy link
Copy Markdown

c3charvat commented Oct 21, 2024

I have been involved tangentially with @qoomon a bit on this. @qoomon Correct me if i am wrong.
TL;DR: We are trying to resolve job URLs for a given job in a reusable workflow. There is nothing to match to in the job api (https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#get-a-job-for-a-workflow-run) that readily available in the environment.

Not Short, sadly too long full detail version:
We are trying to resolve job URLs for a given job in a reusable workflow/matrix.
Since we don't have the FULL job name readily available to us in we have to do something like:
https://github.com/qoomon/actions--context/blob/402df64fd4c405e739f0e60e9ed0987c56013605/lib/actions.ts#L203-L227
to build the absolute job name.
Then search for it in the API context..

Up to this point it seems like a quality of life issue not necessarily a bug... But...

Since the system.jobId contains a GUID that is not present in job api endpoint, the only unique identifier is the JOB name.
(and MAYBE the runner name if status == in progress )

The issue becomes apparent when you apply a matrix or reusable workflow:
Note the code that handles job length....
So even while we can compile the full absolute job name the api truncates it... ie:
"build-with-reusable-workflow-with-matrix-inputs (1.1, In_this_input_we_are_testing_very_very_very... / build"
from:
API: https://api.github.com/repos/c3charvat/actions--context/actions/runs/11445449487/jobs
JOB URL: https://github.com/c3charvat/actions--context/actions/runs/11445449487/job/31842529999
Job # 2 Job # 3 which are unique but truncated to not be unique.

EDIT:
This also messes with the frontend as well. Since it cant differentiate the jobs it groups them, together like steps rather than different jobs

(Since the is the only guaranteed unique data shared between the job level api and the github.job context)
You cant really guarantee that you can resolve a individual jobs url.

Something similar was added for the Telemetry.
3d34a3c#diff-4085092f16aac893358021b20c7a92b02a504a1a99830bc88454ae0380638c45

@qoomon
Copy link
Copy Markdown
Author

qoomon commented Mar 15, 2025

Finally I found a stable way 🎉

You could use my qoomon/actions--context@v4 action to add additional environment variables.

It will work also with matrix jobs and within reusable workflows. And any amount of jobs within a workflow run.

jobs:
  example:
    runs-on: ubuntu-latest
    environment: playground
    steps:
      - uses: qoomon/actions--context@v4
      - run: |
          echo "Job Id:         ${GITHUB_JOB_ID}"
          echo "Job Name:       ${GITHUB_JOB_NAME}"
          echo "Job Log URL:    ${GITHUB_JOB_URL}"
          echo "Environment:    ${GITHUB_ENVIRONMENT}"
          echo "Deployment URL: ${GITHUB_DEPLOYMENT_URL}"

This action determines the current job by listing all job of current workflow with current run id and then finds current job by matching the runner name or id.

Source: https://github.com/qoomon/actions--context

@c3charvat
Copy link
Copy Markdown

c3charvat commented Mar 15, 2025

Finally I found a stable way 🎉

You could use my qoomon/actions--context@v4 action to add additional environment variables.

It will work also with matrix jobs and within reusable workflows. And any amount of jobs within a workflow run.

jobs:
  example:
    runs-on: ubuntu-latest
    environment: playground
    steps:
      - uses: qoomon/actions--context@v4
      - run: |
          echo "Job Id:         ${GITHUB_JOB_ID}"
          echo "Job Name:       ${GITHUB_JOB_NAME}"
          echo "Job Log URL:    ${GITHUB_JOB_URL}"
          echo "Environment:    ${GITHUB_ENVIRONMENT}"
          echo "Deployment URL: ${GITHUB_DEPLOYMENT_URL}"

This action determines the current job by listing all job of current workflow with current run id and then finds current job by matching the runner name or id.

Source: https://github.com/qoomon/actions--context

This should work. As long as the names of the runners are unique? Which I think they are as that was the fall back method we had before if I remember correctly.

@qoomon
Copy link
Copy Markdown
Author

qoomon commented Mar 15, 2025

They are unique in the environment variable however for the new GitHub runners they are all named 'GitHub Actions'. To work around that you need to parse the id from the environment variable and match the runner id with the job runner IDs from the API.

@qoomon qoomon closed this by deleting the head repository Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants