Skip to content

Commit

Permalink
Fix issue where truncated job names would cause the job to fail. (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
c3charvat authored Oct 21, 2024
1 parent d72aacf commit c4a611a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 10 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/example-reusable-from-matrix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Example Reusable Workflow Called From a Matrix
on:
workflow_call:
inputs:
id:
required: true
type: string
input_1:
required: true
type: string
input_2:
required: false
default: ''
type: string
workflow-context:
required: true
type: string

jobs:
build:
runs-on: ubuntu-latest
steps:
- run: env | grep GITHUB_

- uses: actions/checkout@v4

- uses: ./
with:
workflow-context: ${{ inputs.workflow-context }}
46 changes: 46 additions & 0 deletions .github/workflows/matrix-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Example Matrix Action

on:
push:
branches: ['main']
workflow_dispatch:

jobs:
build-with-reusable-workflow-with-matrix-inputs:
strategy:
fail-fast: false
matrix:
inputs: [
{id: '1.1', input_1: 'In_this_input_we_are_testing_very_very_very_long_strings', input_2: 'In_this_input_we_are_testing_very_very_very_long_strings'},
{id: '1.1', input_1: 'In_this_input_we_are_testing_very_very_very_long_strings', input_2: 'In_this_input_we_are_testing_very_very_very_long_strings'},
{id: '1.1', input_1: 'In_this_input_we_are_testing_very_very_very_long_strings', input_2: 'In_this_input_we_are_testing_very_very_very_long_string'},
{id: '1.2', input_1: 'Example', input_2: 'In_this_input_we_are_testing_very_very_very_long_strings'},
{id: '1.3', input_2: 'In_this_input_we_are_testing_very_very_very_long_strings'},]
uses: ./.github/workflows/example-reusable-from-matrix.yml
with:
id: ${{matrix.inputs.id}} # Id to seperate the different runs
input_2: ${{matrix.inputs.input_2}}
input_1: ${{matrix.inputs.input_1}}
workflow-context: build-with-reusable-workflow-with-matrix-inputs, ${{ toJSON(matrix) }}

build-with-reusable-workflow-with-limit:
strategy:
max-parallel: 2
fail-fast: false
matrix:
inputs: [
{id: '2.1', input_1: 'In_this_input_we_are_testing_very_very_very_long_strings', input_2: 'In_this_input_we_are_testing_very_very_very_long_strings'},
{id: '2.1', input_1: 'In_this_input_we_are_testing_very_very_very_long_strings', input_2: 'In_this_input_we_are_testing_very_very_very_long_strings'},
{id: '2.1', input_1: 'In_this_input_we_are_testing_very_very_very_long_strings', input_2: 'In_this_input_we_are_testing_very_very_very_long_string'},
{id: '2.2', input_1: 'Example', input_2: 'In_this_input_we_are_testing_very_very_very_long_strings'},
{id: '2.3', input_1: 'In_this_input_we_are_testing_very_very_very_long_strings'},
{id: '2.4', input_1: 'In_this_test_we_are_testing', input_2: 'the_character_sm_of_98'},
{id: '2.5', input_1: 'In_this_test_we_are_testing', input_2: 'the_character_sum_of_99'},
{id: '2.6', input_1: 'In_this_test_we_are_testing', input_2: 'the_character_sum_of_100'},
{id: '2.7', input_1: 'In_this_test_we_are_testing', input_2: 'the_character_sum_of_101_'}]
uses: ./.github/workflows/example-reusable-from-matrix.yml
with:
id: ${{matrix.inputs.id}} # Id to seperate the different runs
input_2: ${{matrix.inputs.input_2}}
input_1: ${{matrix.inputs.input_1}}
workflow-context: build-with-reusable-workflow-with-limit, ${{ toJSON(matrix) }}
2 changes: 2 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ inputs:
# --- Workarounds for missing GitHub Actions context variables ---
job-name:
default: '${{ github.job }}'
runner-name:
default: '${{ runner.name }}'
'#job-matrix':
description: 'DO NOT SET MANUALLY'
default: '${{ toJSON(matrix) }}'
Expand Down
17 changes: 13 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44573,7 +44573,8 @@ function getAbsoluteJobName({ job, matrix, workflowContextChain }) {
actualJobName = `${actualJobName} (${flatValues.join(', ')})`;
}
}
if (actualJobName.length > 97) {
// If the job name is too long, github truncates it and adds an ellipsis
if (actualJobName.length > 100) {
actualJobName = actualJobName.substring(0, 97) + '...';
}
workflowContextChain?.forEach((workflowContext) => {
Expand Down Expand Up @@ -44634,15 +44635,23 @@ async function getJobObject(octokit) {
}
throw error;
});
const currentJob = workflowRunJobs.find((job) => job.name === absoluteJobName);
if (!currentJob) {
const runnerName = getInput('runner-name', { required: true });
//In the case of truncated job name the only
const currentJob = workflowRunJobs.filter((job) => job.name === absoluteJobName && job.status === "in_progress" && job.runner_name === runnerName);
if (currentJob.length === 0) {
throw new Error(`Current job '${absoluteJobName}' could not be found in workflow run.\n` +
'If this action is used within a reusable workflow, ensure that ' +
'action input \'workflow-context\' is set to ${{ inputs.workflow-context }}' +
'and workflow input \'workflow-context\' was set to \'"CALLER_JOB_NAME", ${{ toJSON(matrix) }}\'' +
'or \'"CALLER_JOB_NAME", ${{ toJSON(matrix) }}, ${{ inputs.workflow-context }}\' in case of a nested workflow.');
}
const jobObject = { ...currentJob, };
else if (currentJob.length != 1) {
throw new Error(`Current job '${absoluteJobName}' returned multiple matches'.\n` +
'If this action is used within a reusable workflow, or matrix please ensure that the job name is unique.' +
'If the length of \'"CALLER_JOB_NAME" + ${{ toJSON(matrix) }}\' exceeds 97 characters' +
'Github Actions may have truncated it. Thus, potentially making it non unique. ');
}
const jobObject = { ...currentJob[0], };
return _jobObject = jobObject;
}
let _deploymentObject;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ function getAbsoluteJobName({job, matrix, workflowContextChain}: {
actualJobName = `${actualJobName} (${flatValues.join(', ')})`
}
}
if (actualJobName.length > 97) {
// If the job name is too long, github truncates it and adds an ellipsis
if (actualJobName.length > 100) {
actualJobName = actualJobName.substring(0, 97) + '...'
}

Expand Down Expand Up @@ -286,17 +287,25 @@ export async function getJobObject(octokit: InstanceType<typeof GitHub>): Promis
throw error
})

const currentJob = workflowRunJobs.find((job) => job.name === absoluteJobName)
if (!currentJob) {

const runnerName = getInput('runner-name', {required: true})
//In the case of truncated job name the only other shared identifier is the runner name
const currentJob = workflowRunJobs.filter((job) => job.name === absoluteJobName && job.status=== "in_progress" && job.runner_name === runnerName)
if (currentJob.length === 0) {
throw new Error(`Current job '${absoluteJobName}' could not be found in workflow run.\n` +
'If this action is used within a reusable workflow, ensure that ' +
'action input \'workflow-context\' is set to ${{ inputs.workflow-context }}' +
'and workflow input \'workflow-context\' was set to \'"CALLER_JOB_NAME", ${{ toJSON(matrix) }}\'' +
'or \'"CALLER_JOB_NAME", ${{ toJSON(matrix) }}, ${{ inputs.workflow-context }}\' in case of a nested workflow.'
)
} else if (currentJob.length != 1) {
throw new Error(`Current job '${absoluteJobName}' returned multiple matches'.\n` +
'If this action is used within a reusable workflow, or matrix please ensure that the job name is unique.' +
'If the length of \'"CALLER_JOB_NAME" + ${{ toJSON(matrix) }}\' exceeds 97 characters' +
'Github Actions may have truncated it. Thus, potentially making it non unique. '
)
}

const jobObject = {...currentJob,}
const jobObject = {...currentJob[0],}
return _jobObject = jobObject;
}

Expand Down

0 comments on commit c4a611a

Please sign in to comment.