In high-concurrency scenarios, a runner can sometimes be “stolen” by a job other than the one that launched it, potentially leading to jobs no longer having a runner available or runners timing out before executing a job.
To ensure a runner is only assigned to the job that launched it, you can pin it to that job by adding a job pinning discriminator to the label.
This discriminator is unique and shared by both the job and its runner and ensures they are always matched together. The easiest way to do this would have been to use the job’s unique ID, but unfortunately, GitHub doesn’t let you access that from within the workflow yml. We can however achieve the same result by combining various job attributes, depending on whether the job is a regular job, a matrix job or a reusable workflow job.
The following attributes uniquely identify a regular job:
Attribute | Description |
---|---|
github.run_id |
The unique ID of the current workflow run. |
github.run_attempt |
The attempt number of the current workflow run. |
job_name | The name of the current job. |
The job pinning discriminator would therefore be
${{ github.run_id }}-${{ github.run_attempt }}-job_name
Simply add it as an additional label (all variables will be replaced at runtime):
runs-on:
- sprinters:aws:ubuntu-latest
- ${{ github.run_id }}-${{ github.run_attempt }}-my-regular-job
Your regular job will now be pinned to the correct runner.
The following attributes uniquely identify a matrix job:
Attribute | Description |
---|---|
github.run_id |
The unique ID of the current workflow run. |
github.run_attempt |
The attempt number of the current workflow run. |
job_name | The name of the current job. |
strategy.job-index |
The position of the job within the matrix. |
The job pinning discriminator would then be
${{ github.run_id }}-${{ github.run_attempt }}-job_name-${{ strategy.job-index }}
Simply add it as an additional label (all variables will be replaced at runtime):
runs-on:
- sprinters:aws:ubuntu-latest
- ${{ github.run_id }}-${{ github.run_attempt }}-my-matrix-job-${{ strategy.job-index }}
Your matrix job will now be pinned to the correct runner.
The following attributes uniquely identify a reusable workflow job:
Attribute | Description |
---|---|
github.run_id |
The unique ID of the current workflow run. |
github.run_attempt |
The attempt number of the current workflow run. |
job_name | The name of the current job. |
inputs. param1 |
The inputs that uniquely distinguish this call of the reusable workflow. |
inputs. paramN |
The inputs that uniquely distinguish this call of the reusable workflow. |
The job pinning discriminator would then be
${{ github.run_id }}-${{ github.run_attempt }}-job_name-${{ inputs.param1 }}-${{ inputs.paramN }}
Simply add it as an additional label (all variables will be replaced at runtime):
runs-on:
- sprinters:aws:ubuntu-latest
- ${{ github.run_id }}-${{ github.run_attempt }}-my-reusable-workflow-job-${{ inputs.my-unique-param }}
Your reusable workflow job will now be pinned to the correct runner.