From a8b859ddd8ccd146d89b87d5a74eab66998f1845 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 12 Aug 2024 09:30:06 -0500 Subject: [PATCH 01/48] Implement action --- .github/workflows/integration-tests.yaml | 152 +++++++++++++++++++++++ README.md | 77 +++++++++++- action.yaml | 63 ++++++++++ 3 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/integration-tests.yaml create mode 100644 action.yaml diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml new file mode 100644 index 0000000..c5a0804 --- /dev/null +++ b/.github/workflows/integration-tests.yaml @@ -0,0 +1,152 @@ +--- +name: Integration Tests +on: + pull_request: + paths: + - "action.yaml" + - ".github/workflows/integration-tests.yaml" + +jobs: + setup-simple: + name: Setup Simple + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + strategy: + fail-fast: false + matrix: + index: + - 1 + - 2 + outputs: + results-json: ${{ steps.matrix-output.outputs.json }} + steps: + - uses: actions/checkout@v4 + # Slow down on job to ensure that this is the last run + - if: ${{ strategy.job-index == 0 }} + run: sleep 5 + # Keep `id` the same between `setup-simple` and `setup-complex` + # to ensure we can separate output per job. + - uses: ./output + id: matrix-output + with: + yaml: | + index: ${{ matrix.index }} + + test-simple: + name: Test Simple + needs: setup-simple + runs-on: ubuntu-latest + steps: + - name: Output JSON + run: | + if [[ "${output_json}" != "${expected_json}" ]]; then + cat <<<"${output_json}" >"output" + cat <<<"${expected_json}" >"expected" + diff output expected | cat -te + exit 1 + fi + env: + output_json: ${{ needs.setup-simple.outputs.results-json }} + expected_json: |- + [ + { + "index": 1 + }, + { + "index": 2 + } + ] + + setup-complex: + name: Setup Complex + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + strategy: + fail-fast: false + matrix: + build: + - name: App One + repo: user/app1 + - name: App Two + repo: user/app2 + version: + - "1.0" + - "2.0" + outputs: + results-json: ${{ steps.matrix-output.outputs.json }} + steps: + - uses: actions/checkout@v4 + # Keep `id` the same between `setup-simple` and `setup-complex` + # to ensure we can separate output per job. + - uses: ./output + id: matrix-output + with: + yaml: | + name: ${{ matrix.build.name }} + repo: ${{ matrix.build.repo }} + version_string: "${{ matrix.version }}" + version_number: ${{ matrix.version }} + + test-complex: + name: Test Complext + needs: setup-complex + runs-on: ubuntu-latest + steps: + - name: Output JSON + run: | + if [[ "${output_json}" != "${expected_json}" ]]; then + cat <<<"${output_json}" >"output" + cat <<<"${expected_json}" >"expected" + diff output expected | cat -te + exit 1 + fi + env: + output_json: ${{ needs.setup-complex.outputs.results-json }} + expected_json: |- + [ + { + "name": "App One", + "repo": "user/app1", + "version_string": "1.0", + "version_number": 1 + }, + { + "name": "App One", + "repo": "user/app1", + "version_string": "2.0", + "version_number": 2 + }, + { + "name": "App Two", + "repo": "user/app2", + "version_string": "1.0", + "version_number": 1 + }, + { + "name": "App Two", + "repo": "user/app2", + "version_string": "2.0", + "version_number": 2 + } + ] + + test-empty: + name: Test Empty + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + steps: + - uses: actions/checkout@v4 + - uses: ./output + id: matrix-output + continue-on-error: true + with: + yaml: "" + - name: Action failed + if: ${{ steps.matrix-output.outcome == 'success' }} + run: exit 1 diff --git a/README.md b/README.md index fe889be..c3aca59 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,75 @@ -# matrix-output -Collect outputs from each matrix job +# Matrix Output + +Collect outputs from each matrix job. Currently, setting output for matrix jobs will cause outputs of earlier completed jobs to be overwritten by jobs completed later (see [discussion](https://github.com/orgs/community/discussions/26639)). This action allows the output from each matrix job to be collected into a JSON list to be utilized by dependent jobs. + +## Examples + +```yaml +# CI.yaml +jobs: + build: + name: Build ${{ matrix.build.name }} + # These permissions are needed to: + # - Use `matrix-output`: https://github.com/beacon-biosignals/matrix-output#permissions + permissions: + actions: read + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + build: + - name: App One + repo: user/app1 + - name: App Two + repo: user/app2 + outputs: + json: ${{ steps.matrix-output.outputs.json }} + steps: + - uses: docker/build-push-action@v6 + id: build-push + with: + tags: ${{ matrix.build.repo }}:latest + push: true + - uses: beacon-biosignals/matrix-output@v1 + id: matrix-output + with: + yaml: | + name: ${{ matrix.build.name }} + image: ${{ matrix.build.repo }}@${{ steps.build-push.outputs.digest }} + + test: + name: Test ${{ matrix.build.name }} + needs: build + runs-on: ubuntu-latest + container: + image: ${{ matrix.build.image }} + strategy: + fail-fast: false + matrix: + build: ${{ fromJSON(needs.build.outputs.json) }} + steps: + ... +``` + +## Inputs + +The `matrix-output` action supports the following inputs: + +| Name | Description | Required | Example | +|:-----------------|:------------|:---------|:--------| +| `yaml` | A string representing a YAML data. Typically, a simple dictionary of key/value pairs. | Yes |
name: ${{ matrix.name }}
...
| + +## Outputs + +| Name | Description | Example | +|:-------|:------------|:--------| +| `json` | A string representing a JSON list of dictionaries. Each dictionary in the list contains the output for a single job from the job matrix. The order of this list corresponds to the job index (i.e. `strategy.job-index`). |
[
  {
    "name": "Server.jl",
    ...
  },
  {
    "name": "Client.jl",
    ...
  }
]
| + +## Permissions + +The follow [job permissions](https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs) are required to run this action: + +```yaml +permissions: + action: read +``` diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..806f87d --- /dev/null +++ b/action.yaml @@ -0,0 +1,63 @@ +--- +name: Matrix Output +description: >- + Collect outputs from each matrix job. +inputs: + yaml: + description: >- + A string representing YAML data. Typically, a simple dictionary of key/value pairs. + type: string + required: true +outputs: + json: + description: >- + A string representing a JSON list of dictionaries. Each dictionary in the list + contains the output for a single job from the job matrix. The order of this list + corresponds to the job index (i.e. `strategy.job-index`). + value: ${{ steps.merge.outputs.json }} +runs: + using: composite + steps: + - name: Generate job output + shell: bash + run: yq -o=json <<<"${input_yaml:?}" >job-output.json + env: + input_yaml: ${{ inputs.yaml }} + - name: Upload job output + uses: actions/upload-artifact@v4 + with: + name: ${{ github.action }}-${{ github.job }}-${{ strategy.job-index }} + path: job-output.json + if-no-files-found: error + - name: Determine available matrix job outputs + id: job-output + shell: bash + run: | + # https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#list-workflow-run-artifacts + artifacts="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/artifacts" --jq '.artifacts')" + num_job_uploads="$(jq --arg prefix "${prefix:?}" 'map(select(.name | startswith($prefix))) | length' <<<"$artifacts")" + echo "Uploaded ${num_job_uploads}/${{ strategy.job-total }}" >&2 + echo "num=${num_job_uploads:?}" | tee -a "$GITHUB_OUTPUT" + env: + GH_TOKEN: ${{ github.token }} + prefix: ${{ github.action }}-${{ github.job }}- + run_id: ${{ github.run_id }} + - name: Download job matrix outputs + uses: actions/download-artifact@v4 + if: ${{ steps.job-output.outputs.num == strategy.job-total }} + with: + pattern: ${{ github.action }}-${{ github.job }}-* + path: matrix-output + merge-multiple: false + - name: Merge job matrix output + id: merge + if: ${{ steps.job-output.outputs.num == strategy.job-total }} + shell: bash + run: | + # Specify our multiline output using GH action flavored heredocs + # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings + { + echo "json< Date: Mon, 12 Aug 2024 09:31:59 -0500 Subject: [PATCH 02/48] fixup! Implement action --- .github/workflows/integration-tests.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index c5a0804..d66b875 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -28,7 +28,7 @@ jobs: run: sleep 5 # Keep `id` the same between `setup-simple` and `setup-complex` # to ensure we can separate output per job. - - uses: ./output + - uses: ./ id: matrix-output with: yaml: | @@ -82,7 +82,7 @@ jobs: - uses: actions/checkout@v4 # Keep `id` the same between `setup-simple` and `setup-complex` # to ensure we can separate output per job. - - uses: ./output + - uses: ./ id: matrix-output with: yaml: | @@ -142,7 +142,7 @@ jobs: contents: read steps: - uses: actions/checkout@v4 - - uses: ./output + - uses: ./ id: matrix-output continue-on-error: true with: From 4f7723fccc46523d7e9c15192d26f48f09c346e5 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:13:17 -0500 Subject: [PATCH 03/48] Experiment with job-name action --- .github/workflows/integration-tests.yaml | 15 +++++++++++++++ job-name/action.yaml | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 job-name/action.yaml diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index d66b875..d2c02ff 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -150,3 +150,18 @@ jobs: - name: Action failed if: ${{ steps.matrix-output.outcome == 'success' }} run: exit 1 + + test-job-name: + name: Setup Simple + runs-on: ubuntu-latest + permissions: + contents: read + strategy: + fail-fast: false + matrix: + index: + - 1 + - 2 + steps: + - uses: actions/checkout@v4 + - uses: ./job-name/ diff --git a/job-name/action.yaml b/job-name/action.yaml new file mode 100644 index 0000000..402d528 --- /dev/null +++ b/job-name/action.yaml @@ -0,0 +1,24 @@ +--- +name: Job Name +description: +inputs: {} +outputs: + job-name: + value: ${{ steps.merge.outputs.json }} +runs: + using: composite + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.workflow_sha }} + path: ${{ github.action_path }}/repo + - id: job + shell: bash + run: + # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml + workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" + job_name_template="$(yq '.jobs[env(job_key)].name' "${workflow_path:?}")" + echo "name=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" + env: + workflow_ref: ${{ github.workflow_ref }} + job_key: ${{ github.job }} From d429748a97a805fdd9c76b3080e993fa523be5f7 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:16:00 -0500 Subject: [PATCH 04/48] fixup! Experiment with job-name action --- .github/workflows/integration-tests.yaml | 2 +- job-name/action.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index d2c02ff..2a2951c 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -152,7 +152,7 @@ jobs: run: exit 1 test-job-name: - name: Setup Simple + name: Job Name runs-on: ubuntu-latest permissions: contents: read diff --git a/job-name/action.yaml b/job-name/action.yaml index 402d528..64323b5 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -17,7 +17,7 @@ runs: run: # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" - job_name_template="$(yq '.jobs[env(job_key)].name' "${workflow_path:?}")" + job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${workflow_path:?}")" echo "name=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" env: workflow_ref: ${{ github.workflow_ref }} From db0323594d29ce877399056c0f1f95873fe54b9c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:17:04 -0500 Subject: [PATCH 05/48] Debug --- job-name/action.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/job-name/action.yaml b/job-name/action.yaml index 64323b5..37c1167 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -17,6 +17,8 @@ runs: run: # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" + echo "$workflow_path" + yq '.jobs' "$workflow_path" job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${workflow_path:?}")" echo "name=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" env: From f60b798bf9985a7cd24d150d0d6ba13bf269a71b Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:20:06 -0500 Subject: [PATCH 06/48] fixup --- job-name/action.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 37c1167..8adcadf 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -14,13 +14,13 @@ runs: path: ${{ github.action_path }}/repo - id: job shell: bash - run: + run: | + set -x # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" - echo "$workflow_path" - yq '.jobs' "$workflow_path" - job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${workflow_path:?}")" + job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${repo_path}/${workflow_path:?}")" echo "name=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" env: + repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} job_key: ${{ github.job }} From 9bc93ce2877913c9ee515347e6f64d97204584de Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:30:20 -0500 Subject: [PATCH 07/48] Test matrix support --- .github/workflows/integration-tests.yaml | 11 ++++++++--- job-name/action.yaml | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 2a2951c..c28fc52 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -159,9 +159,14 @@ jobs: strategy: fail-fast: false matrix: - index: - - 1 - - 2 + build: + - name: App One + repo: user/app1 + - name: App Two + repo: user/app2 + version: + - "1.0" + - "2.0" steps: - uses: actions/checkout@v4 - uses: ./job-name/ diff --git a/job-name/action.yaml b/job-name/action.yaml index 8adcadf..a9cf29f 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -20,7 +20,9 @@ runs: workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${repo_path}/${workflow_path:?}")" echo "name=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" + echo "$matrix_json" env: repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} job_key: ${{ github.job }} + matrix_json: ${{ toJSON(matrix) }} From 5f2729b09a93c25eccf97edbb34e7ebedfc8ec40 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:38:39 -0500 Subject: [PATCH 08/48] Iterate --- job-name/action.yaml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index a9cf29f..c279b3d 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -19,8 +19,14 @@ runs: # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${repo_path}/${workflow_path:?}")" - echo "name=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" - echo "$matrix_json" + + # Job matrix which doesn't use any expressions + if [[ -n "${matrix_json}" ]]; then + matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" + job_name="${job_name_template} (${matrix_values})" + fi + + echo "name=${job_name:?}" | tee -a "$GITHUB_OUTPUT" env: repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} From 95479c086f4189b0505de804cdb74c11f43af95e Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:40:22 -0500 Subject: [PATCH 09/48] Determine when matrix values are automatically added --- .github/workflows/integration-tests.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index c28fc52..f8ede52 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -170,3 +170,18 @@ jobs: steps: - uses: actions/checkout@v4 - uses: ./job-name/ + + test-job-name2: + name: ${{ github.job }} + runs-on: ubuntu-latest + permissions: + contents: read + strategy: + fail-fast: false + matrix: + index: + - 1 + - 2 + steps: + - uses: actions/checkout@v4 + - uses: ./job-name/ From 89f799750f182217387cb29280cec30c12339561 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 10:41:34 -0500 Subject: [PATCH 10/48] Another test --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index f8ede52..b38f0c1 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -172,7 +172,7 @@ jobs: - uses: ./job-name/ test-job-name2: - name: ${{ github.job }} + name: ${{ github.job } runs-on: ubuntu-latest permissions: contents: read From 18c9f49e8ad99b4a82c548f2e0643b98f98bfe9f Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:07:19 -0500 Subject: [PATCH 11/48] Feature complete --- job-name/action.yaml | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index c279b3d..aa1cace 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -4,7 +4,7 @@ description: inputs: {} outputs: job-name: - value: ${{ steps.merge.outputs.json }} + value: ${{ steps.job-name.outputs.string }} runs: using: composite steps: @@ -12,23 +12,46 @@ runs: with: ref: ${{ github.workflow_sha }} path: ${{ github.action_path }}/repo - - id: job + - id: job-name-tpl shell: bash run: | set -x # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${repo_path}/${workflow_path:?}")" - - # Job matrix which doesn't use any expressions - if [[ -n "${matrix_json}" ]]; then - matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" - job_name="${job_name_template} (${matrix_values})" - fi - - echo "name=${job_name:?}" | tee -a "$GITHUB_OUTPUT" + echo "string=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" env: repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} job_key: ${{ github.job }} + - id: job-name + shell: bash + run: | + # Determine if job name contains GitHub expressions. As GitHub expressions must be + # closed we can just check for the opening sequence. + if echo '${job_name_template}' | grep -qE '\$\{\{'; then + # Convert GHA expressions into Bash expressions + job_name_expr="$(perl -pe 's/\${{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq ".\2" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" + job_name="$(eval "${job_name_expr}")" + else + # GitHub adds job matrix values to job names which do not contain expressions. + matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" + + if [[ -n "$matrix_values" ]]; + job_name="${job_name_template} (${matrix_values})" + else + job_name="${job_name_template}" + fi + fi + echo "string=${job_name:?}" | tee -a "$GITHUB_OUTPUT" + env: + job_name_template: ${{ steps.job-name-tpl.outputs.string }} + + # `jobs..name` can use the contexts: [github, needs, strategy, matrix, vars, inputs] + # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/contexts#context-availability + github_json: ${{ toJSON(github) }} + needs_json: ${{ toJSON(needs) }} + strategy_json: ${{ toJSON(strategy) }} matrix_json: ${{ toJSON(matrix) }} + vars_json: ${{ toJSON(vars) }} + inputs_json: ${{ toJSON(inputs) }} From d8636372fa4564c3ddc15b529a930cc8fbab1394 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:09:31 -0500 Subject: [PATCH 12/48] Fix integration tests --- .github/workflows/integration-tests.yaml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index b38f0c1..bba3092 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -152,6 +152,15 @@ jobs: run: exit 1 test-job-name: + name: Job Name + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@v4 + - uses: ./job-name/ + + test-job-name-matrix: name: Job Name runs-on: ubuntu-latest permissions: @@ -171,8 +180,8 @@ jobs: - uses: actions/checkout@v4 - uses: ./job-name/ - test-job-name2: - name: ${{ github.job } + test-job-name-matrix-expr: + name: ${{ github.job }} runs-on: ubuntu-latest permissions: contents: read From f1a45c9e61bb99d07b1f001dc0d0288a6b63a1fe Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:13:54 -0500 Subject: [PATCH 13/48] Iterating --- job-name/action.yaml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index aa1cace..4639cf8 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -31,7 +31,7 @@ runs: # closed we can just check for the opening sequence. if echo '${job_name_template}' | grep -qE '\$\{\{'; then # Convert GHA expressions into Bash expressions - job_name_expr="$(perl -pe 's/\${{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq ".\2" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" + job_name_expr="$(perl -pe "${gha_expr_regex:?}" <<<"${job_name_template}")" job_name="$(eval "${job_name_expr}")" else # GitHub adds job matrix values to job names which do not contain expressions. @@ -47,11 +47,14 @@ runs: env: job_name_template: ${{ steps.job-name-tpl.outputs.string }} + # https://github.com/orgs/community/discussions/26621#discussioncomment-3252557 + gha_expr_regex: ${{ 's/\${{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq ".\2" <<<"\${\1_json:?}")/g' }} + # `jobs..name` can use the contexts: [github, needs, strategy, matrix, vars, inputs] # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/contexts#context-availability github_json: ${{ toJSON(github) }} - needs_json: ${{ toJSON(needs) }} + # needs_json: ${{ toJSON(needs) }} strategy_json: ${{ toJSON(strategy) }} matrix_json: ${{ toJSON(matrix) }} - vars_json: ${{ toJSON(vars) }} - inputs_json: ${{ toJSON(inputs) }} + # vars_json: ${{ toJSON(vars) }} + # inputs_json: ${{ toJSON(inputs) }} From 944a4cb34573e5bd69c81775a0b356d02624047f Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:15:29 -0500 Subject: [PATCH 14/48] fixup --- job-name/action.yaml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 4639cf8..b4cb7e1 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -12,7 +12,8 @@ runs: with: ref: ${{ github.workflow_sha }} path: ${{ github.action_path }}/repo - - id: job-name-tpl + - name: Determine job name template + id: job-name-tpl shell: bash run: | set -x @@ -24,7 +25,8 @@ runs: repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} job_key: ${{ github.job }} - - id: job-name + - name: Render job name + id: job-name shell: bash run: | # Determine if job name contains GitHub expressions. As GitHub expressions must be @@ -37,7 +39,7 @@ runs: # GitHub adds job matrix values to job names which do not contain expressions. matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" - if [[ -n "$matrix_values" ]]; + if [[ -n "$matrix_values" ]]; then job_name="${job_name_template} (${matrix_values})" else job_name="${job_name_template}" From b9134b6ebcd6c826dd70e7bd38234ba0390c04b0 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:18:33 -0500 Subject: [PATCH 15/48] fixup --- job-name/action.yaml | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index b4cb7e1..28dd746 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -12,10 +12,10 @@ runs: with: ref: ${{ github.workflow_sha }} path: ${{ github.action_path }}/repo - - name: Determine job name template - id: job-name-tpl + - id: job-name-tpl shell: bash run: | + # Determine job name template set -x # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" @@ -25,21 +25,22 @@ runs: repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} job_key: ${{ github.job }} - - name: Render job name - id: job-name + - id: job-name shell: bash run: | + # Render job name + set -x # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. if echo '${job_name_template}' | grep -qE '\$\{\{'; then # Convert GHA expressions into Bash expressions - job_name_expr="$(perl -pe "${gha_expr_regex:?}" <<<"${job_name_template}")" + job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq ".\2" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" job_name="$(eval "${job_name_expr}")" else # GitHub adds job matrix values to job names which do not contain expressions. matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" - if [[ -n "$matrix_values" ]]; then + if [[ -n "${matrix_values}" ]]; then job_name="${job_name_template} (${matrix_values})" else job_name="${job_name_template}" @@ -49,9 +50,6 @@ runs: env: job_name_template: ${{ steps.job-name-tpl.outputs.string }} - # https://github.com/orgs/community/discussions/26621#discussioncomment-3252557 - gha_expr_regex: ${{ 's/\${{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq ".\2" <<<"\${\1_json:?}")/g' }} - # `jobs..name` can use the contexts: [github, needs, strategy, matrix, vars, inputs] # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/contexts#context-availability github_json: ${{ toJSON(github) }} From cf1d51e744602b0ca26a5917c8b9b3235d6881de Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:22:07 -0500 Subject: [PATCH 16/48] Make proper tests --- .github/workflows/integration-tests.yaml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index bba3092..928f3f0 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -159,6 +159,14 @@ jobs: steps: - uses: actions/checkout@v4 - uses: ./job-name/ + id: job-name + - run: | + if [[ "${output}" != "${expected}" ]]; then + exit 1 + fi + env: + output: ${{ steps.job-name.outputs.job-name }} + expected: "Job Name" test-job-name-matrix: name: Job Name @@ -179,6 +187,14 @@ jobs: steps: - uses: actions/checkout@v4 - uses: ./job-name/ + id: job-name + - run: | + if [[ "${output}" != "${expected}" ]]; then + exit 1 + fi + env: + output: ${{ steps.job-name.outputs.job-name }} + expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: name: ${{ github.job }} @@ -194,3 +210,11 @@ jobs: steps: - uses: actions/checkout@v4 - uses: ./job-name/ + id: job-name + - run: | + if [[ "${output}" != "${expected}" ]]; then + exit 1 + fi + env: + output: ${{ steps.job-name.outputs.job-name }} + expected: ${{ github.job }} From 937536a3c1d4b88a163923d48145caa7840c9943 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:26:00 -0500 Subject: [PATCH 17/48] fixup --- job-name/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 28dd746..bad1be6 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -32,7 +32,7 @@ runs: set -x # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. - if echo '${job_name_template}' | grep -qE '\$\{\{'; then + if grep -qE '\$\{\{' <<<"${job_name_template}"; then # Convert GHA expressions into Bash expressions job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq ".\2" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" job_name="$(eval "${job_name_expr}")" From aea824d439445f1f800f61d26079ee42db2689c3 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:28:34 -0500 Subject: [PATCH 18/48] fixup --- job-name/action.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index bad1be6..e33f6c8 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -34,8 +34,8 @@ runs: # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then # Convert GHA expressions into Bash expressions - job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq ".\2" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" - job_name="$(eval "${job_name_expr}")" + job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r ".\2" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" + job_name="$(eval "echo ${job_name_expr}")" else # GitHub adds job matrix values to job names which do not contain expressions. matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" From b482108fb3f2d7a4408b200a7a229c0e2127c31c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:31:48 -0500 Subject: [PATCH 19/48] Iterate --- .github/workflows/integration-tests.yaml | 4 ++-- job-name/action.yaml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 928f3f0..f32632e 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: ${{ github.job }} + name: ${{ github.job }} - ${{ matrix.index }} - ${{ strategy.job-index }} runs-on: ubuntu-latest permissions: contents: read @@ -217,4 +217,4 @@ jobs: fi env: output: ${{ steps.job-name.outputs.job-name }} - expected: ${{ github.job }} + expected: ${{ github.job }} - ${{ matrix.index }} - ${{ strategy.job-index }} diff --git a/job-name/action.yaml b/job-name/action.yaml index e33f6c8..53a9526 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -16,7 +16,7 @@ runs: shell: bash run: | # Determine job name template - set -x + [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${repo_path}/${workflow_path:?}")" @@ -29,7 +29,7 @@ runs: shell: bash run: | # Render job name - set -x + [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then From a8333477167c3287f945d467d7972f10fbe66f9c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:34:38 -0500 Subject: [PATCH 20/48] Debug --- .github/workflows/integration-tests.yaml | 2 +- job-name/action.yaml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index f32632e..fc18a8a 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: ${{ github.job }} - ${{ matrix.index }} - ${{ strategy.job-index }} + name: "${{ github.job }} - ${{ matrix.index }} - ${{ strategy.job-index }}" runs-on: ubuntu-latest permissions: contents: read diff --git a/job-name/action.yaml b/job-name/action.yaml index 53a9526..59bc600 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -30,6 +30,7 @@ runs: run: | # Render job name [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x + set -x # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then From f297fca2faf5132adf536a68e8cf3cdd5520f034 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:41:12 -0500 Subject: [PATCH 21/48] Proper hypen support --- job-name/action.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 59bc600..6578a63 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -30,12 +30,12 @@ runs: run: | # Render job name [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x - set -x + set -euo pipefail # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then # Convert GHA expressions into Bash expressions - job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r ".\2" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" + job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r --arg p "\2" "getpath(\$p | split(\".\"))" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" job_name="$(eval "echo ${job_name_expr}")" else # GitHub adds job matrix values to job names which do not contain expressions. From 09a77e57802cbac9287c82a8c728cb6cf792d80a Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:42:24 -0500 Subject: [PATCH 22/48] Debug --- job-name/action.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/job-name/action.yaml b/job-name/action.yaml index 6578a63..033cd1f 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -31,6 +31,7 @@ runs: # Render job name [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x set -euo pipefail + set -x # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then From 1d015259bd6603e8dc622ae9d2a73a5f8cfde3b7 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:46:18 -0500 Subject: [PATCH 23/48] fixup --- job-name/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 033cd1f..21efe16 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -36,7 +36,7 @@ runs: # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then # Convert GHA expressions into Bash expressions - job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r --arg p "\2" "getpath(\$p | split(\".\"))" <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" + job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r --arg p "\2" '\''getpath(\$p | split("."))'\'' <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" job_name="$(eval "echo ${job_name_expr}")" else # GitHub adds job matrix values to job names which do not contain expressions. From 5e8b3519f6f8a6b87e49e3c72c5131482d2e2db6 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:47:35 -0500 Subject: [PATCH 24/48] Test missing github.job --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index fc18a8a..02e31cd 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: "${{ github.job }} - ${{ matrix.index }} - ${{ strategy.job-index }}" + name: "${{ github.job }} - ${{ matrix.index }}" runs-on: ubuntu-latest permissions: contents: read From fda45db277adba700c018ab9a6e3bfa95175fe14 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:48:29 -0500 Subject: [PATCH 25/48] Test missing github.job --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 02e31cd..7552293 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: "${{ github.job }} - ${{ matrix.index }}" + name: ${{ github.job }} runs-on: ubuntu-latest permissions: contents: read From a03a72880935b5b37ff7b4d5c91ee523ecc88326 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:50:05 -0500 Subject: [PATCH 26/48] Test --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 7552293..7bb6880 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: ${{ github.job }} + name: ${{ github.job }} - ${{ strategy.fast-fail }} runs-on: ubuntu-latest permissions: contents: read From 8e5814c27e3c3929bb2958b3d71513ee4828fcb2 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:51:56 -0500 Subject: [PATCH 27/48] Test --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 7bb6880..885f298 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: ${{ github.job }} - ${{ strategy.fast-fail }} + name: ${{ github.job }} - ${{ strategy.job-index }} runs-on: ubuntu-latest permissions: contents: read From 88dddf97e56a152c63b8dc1086d79040a18c783d Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:54:04 -0500 Subject: [PATCH 28/48] Fail when path doesn't exist --- job-name/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 21efe16..4ea2826 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -36,7 +36,7 @@ runs: # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then # Convert GHA expressions into Bash expressions - job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r --arg p "\2" '\''getpath(\$p | split("."))'\'' <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" + job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -er --arg p "\2" '\''getpath(\$p | split("."))'\'' <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" job_name="$(eval "echo ${job_name_expr}")" else # GitHub adds job matrix values to job names which do not contain expressions. From 952383709cc72704dd42af6c18c9c49056686664 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:54:48 -0500 Subject: [PATCH 29/48] Order --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 885f298..096b4c8 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: ${{ github.job }} - ${{ strategy.job-index }} + name: ${{ strategy.job-index }} - ${{ github.job }} runs-on: ubuntu-latest permissions: contents: read From a89cae05202e2df579b9e6c25444894d55235e64 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:55:38 -0500 Subject: [PATCH 30/48] Try event name --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 096b4c8..1aaa914 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: ${{ strategy.job-index }} - ${{ github.job }} + name: ${{ strategy.job-index }} - ${{ github.event_name }} runs-on: ubuntu-latest permissions: contents: read From 6a6784a346e65f26b77df86b9d8698fa0c16b4e7 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 11:57:41 -0500 Subject: [PATCH 31/48] Update tests --- .github/workflows/integration-tests.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 1aaa914..7125287 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,7 +197,8 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" test-job-name-matrix-expr: - name: ${{ strategy.job-index }} - ${{ github.event_name }} + # TODO: Using `github.job` with any expressions results in it being empty + name: ${{ github.event_name }} - ${{ matrix.input }} - ${{ strategy.job-index }} runs-on: ubuntu-latest permissions: contents: read @@ -217,4 +218,4 @@ jobs: fi env: output: ${{ steps.job-name.outputs.job-name }} - expected: ${{ github.job }} - ${{ matrix.index }} - ${{ strategy.job-index }} + expected: ${{ github.event_name }} - ${{ matrix.input }} - ${{ strategy.job-index }} From f27f8762bfb357fa351e75a1eb1a57dd93a7c731 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 12:07:55 -0500 Subject: [PATCH 32/48] Update tests --- job-name/action.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 4ea2826..a70b2cf 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -35,8 +35,8 @@ runs: # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then - # Convert GHA expressions into Bash expressions - job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -er --arg p "\2" '\''getpath(\$p | split("."))'\'' <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" + # Convert GHA expressions into jq Bash expressions. Convert `null` to `""`. + job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r --arg p "\2" '\''getpath(\$p | split(".")) | select(type != "null")'\'' <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" job_name="$(eval "echo ${job_name_expr}")" else # GitHub adds job matrix values to job names which do not contain expressions. From 45c684684b176fe391d3f85d799b49d202711eef Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 13:30:30 -0500 Subject: [PATCH 33/48] fixup --- .github/workflows/integration-tests.yaml | 4 ++-- job-name/action.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 7125287..88466a1 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -198,7 +198,7 @@ jobs: test-job-name-matrix-expr: # TODO: Using `github.job` with any expressions results in it being empty - name: ${{ github.event_name }} - ${{ matrix.input }} - ${{ strategy.job-index }} + name: ${{ github.event_name }} - ${{ matrix.dne }} - ${{ matrix.index }} - ${{ strategy.job-index }} runs-on: ubuntu-latest permissions: contents: read @@ -218,4 +218,4 @@ jobs: fi env: output: ${{ steps.job-name.outputs.job-name }} - expected: ${{ github.event_name }} - ${{ matrix.input }} - ${{ strategy.job-index }} + expected: ${{ github.event_name }} - - ${{ matrix.index }} - ${{ strategy.job-index }} diff --git a/job-name/action.yaml b/job-name/action.yaml index a70b2cf..03556fa 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -37,7 +37,7 @@ runs: if grep -qE '\$\{\{' <<<"${job_name_template}"; then # Convert GHA expressions into jq Bash expressions. Convert `null` to `""`. job_name_expr="$(perl -pe 's/\$\{\{\s*([a-z]+)\.([a-z0-9._-]+)\s*}}/\$(jq -r --arg p "\2" '\''getpath(\$p | split(".")) | select(type != "null")'\'' <<<"\${\1_json:?}")/g' <<<"${job_name_template}")" - job_name="$(eval "echo ${job_name_expr}")" + job_name="$(eval "echo \"${job_name_expr}\"")" else # GitHub adds job matrix values to job names which do not contain expressions. matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" From abdd272a1839d466aa811060ee992ed1415508ec Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 13:38:43 -0500 Subject: [PATCH 34/48] Improve tests --- .github/workflows/integration-tests.yaml | 36 ++++++++++++++++-------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 88466a1..f5f0093 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -160,10 +160,8 @@ jobs: - uses: actions/checkout@v4 - uses: ./job-name/ id: job-name - - run: | - if [[ "${output}" != "${expected}" ]]; then - exit 1 - fi + - name: Matches expected name + run: '[[ "${output}" == "${expected}" ]] || exit 1' env: output: ${{ steps.job-name.outputs.job-name }} expected: "Job Name" @@ -172,6 +170,7 @@ jobs: name: Job Name runs-on: ubuntu-latest permissions: + actions: read contents: read strategy: fail-fast: false @@ -188,19 +187,26 @@ jobs: - uses: actions/checkout@v4 - uses: ./job-name/ id: job-name - - run: | - if [[ "${output}" != "${expected}" ]]; then - exit 1 - fi + - name: Matches expected name + run: '[[ "${output}" == "${expected}" ]] || exit 1' env: output: ${{ steps.job-name.outputs.job-name }} expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" + - name: Matches GitHub API name + run: | + length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') + [[ $length -eq 1 ]] || exit 1 + env: + run_id: ${{ github.run_id }} + run_attempt: ${{ github.run_attempt }} + job_name: ${{ steps.job-name.outputs.job-name }} test-job-name-matrix-expr: # TODO: Using `github.job` with any expressions results in it being empty name: ${{ github.event_name }} - ${{ matrix.dne }} - ${{ matrix.index }} - ${{ strategy.job-index }} runs-on: ubuntu-latest permissions: + actions: read contents: read strategy: fail-fast: false @@ -212,10 +218,16 @@ jobs: - uses: actions/checkout@v4 - uses: ./job-name/ id: job-name - - run: | - if [[ "${output}" != "${expected}" ]]; then - exit 1 - fi + - name: Matches expected name + run: '[[ "${output}" == "${expected}" ]] || exit 1' env: output: ${{ steps.job-name.outputs.job-name }} expected: ${{ github.event_name }} - - ${{ matrix.index }} - ${{ strategy.job-index }} + - name: Matches GitHub API name + run: | + length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') + [[ $length -eq 1 ]] || exit 1 + env: + run_id: ${{ github.run_id }} + run_attempt: ${{ github.run_attempt }} + job_name: ${{ steps.job-name.outputs.job-name }} From d3637ac432ee398c0e6b5c55a219dcfbfabec879 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 13:40:08 -0500 Subject: [PATCH 35/48] fixup! Improve tests --- .github/workflows/integration-tests.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index f5f0093..3a16295 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -197,6 +197,7 @@ jobs: length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') [[ $length -eq 1 ]] || exit 1 env: + GH_TOKEN: ${{ github.token }} run_id: ${{ github.run_id }} run_attempt: ${{ github.run_attempt }} job_name: ${{ steps.job-name.outputs.job-name }} @@ -228,6 +229,7 @@ jobs: length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') [[ $length -eq 1 ]] || exit 1 env: + GH_TOKEN: ${{ github.token }} run_id: ${{ github.run_id }} run_attempt: ${{ github.run_attempt }} job_name: ${{ steps.job-name.outputs.job-name }} From a8933998e1c261b299be4ab7f2fbd3005a60a73d Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 13:41:07 -0500 Subject: [PATCH 36/48] Debug --- .github/workflows/integration-tests.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 3a16295..8615331 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -194,7 +194,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" - name: Matches GitHub API name run: | - length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') + length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') [[ $length -eq 1 ]] || exit 1 env: GH_TOKEN: ${{ github.token }} @@ -226,7 +226,8 @@ jobs: expected: ${{ github.event_name }} - - ${{ matrix.index }} - ${{ strategy.job-index }} - name: Matches GitHub API name run: | - length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') + gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" + length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') [[ $length -eq 1 ]] || exit 1 env: GH_TOKEN: ${{ github.token }} From bbb7233d4b30c3466ca279bcdf4f2204a3fc9038 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 13:44:31 -0500 Subject: [PATCH 37/48] Ensure graceful failure --- .github/workflows/integration-tests.yaml | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 8615331..dd864d1 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -194,8 +194,11 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" - name: Matches GitHub API name run: | - length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') - [[ $length -eq 1 ]] || exit 1 + jobs=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" + if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') -ne 1 ]]; then + jq '.jobs[].name' + exit 1 + fi env: GH_TOKEN: ${{ github.token }} run_id: ${{ github.run_id }} @@ -226,11 +229,13 @@ jobs: expected: ${{ github.event_name }} - - ${{ matrix.index }} - ${{ strategy.job-index }} - name: Matches GitHub API name run: | - gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" - length=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" | jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') - [[ $length -eq 1 ]] || exit 1 + jobs=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" + if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') -ne 1 ]]; then + jq '.jobs[].name' + exit 1 + fi env: GH_TOKEN: ${{ github.token }} run_id: ${{ github.run_id }} run_attempt: ${{ github.run_attempt }} - job_name: ${{ steps.job-name.outputs.job-name }} + job_name: foo From 550caee12de1d60a1e22cba6cb90f5fa9a73bc84 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 13:45:35 -0500 Subject: [PATCH 38/48] fixup --- .github/workflows/integration-tests.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index dd864d1..e9dc7db 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -194,7 +194,7 @@ jobs: expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" - name: Matches GitHub API name run: | - jobs=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" + jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') -ne 1 ]]; then jq '.jobs[].name' exit 1 @@ -229,7 +229,7 @@ jobs: expected: ${{ github.event_name }} - - ${{ matrix.index }} - ${{ strategy.job-index }} - name: Matches GitHub API name run: | - jobs=$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs" + jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') -ne 1 ]]; then jq '.jobs[].name' exit 1 From 8b638b716e726be14d21d271b54edb8631466e61 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 13:46:37 -0500 Subject: [PATCH 39/48] fixup --- .github/workflows/integration-tests.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index e9dc7db..0796817 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -195,8 +195,8 @@ jobs: - name: Matches GitHub API name run: | jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" - if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') -ne 1 ]]; then - jq '.jobs[].name' + if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length' <<<"${jobs}") -ne 1 ]]; then + jq '.jobs[].name' <<<"${jobs}" exit 1 fi env: @@ -230,8 +230,8 @@ jobs: - name: Matches GitHub API name run: | jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" - if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length') -ne 1 ]]; then - jq '.jobs[].name' + if [[ $(jq --arg name "$job_name" '.jobs | map(select(.name == $name)) | length' <<<"${jobs}") -ne 1 ]]; then + jq '.jobs[].name' <<<"${jobs}" exit 1 fi env: From 5c3681f2513a153f3f2986f18b882d3bed9c34e5 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:05:21 -0500 Subject: [PATCH 40/48] Refactor --- .github/workflows/integration-tests.yaml | 2 +- job-name/action.yaml | 51 +++++++++++++++++++----- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 0796817..2eb9b53 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -238,4 +238,4 @@ jobs: GH_TOKEN: ${{ github.token }} run_id: ${{ github.run_id }} run_attempt: ${{ github.run_attempt }} - job_name: foo + job_name: ${{ steps.job-name.outputs.job-name }} diff --git a/job-name/action.yaml b/job-name/action.yaml index 03556fa..485ad7e 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -1,31 +1,44 @@ --- -name: Job Name +name: Job Context description: -inputs: {} +inputs: + job-name: + description: >- + A string representing YAML data. Typically, a simple dictionary of key/value pairs. + type: string + default: "" + required: false outputs: job-name: - value: ${{ steps.job-name.outputs.string }} + value: ${{ inputs.job-name || steps.render.outputs.job-name }} + job-id: + value: ${{ steps.job-api.outputs.job-id }} + job-ids: + value: ${{ steps.job-api.outputs.job-ids }} runs: using: composite steps: - uses: actions/checkout@v4 + if: ${{ inputs.job-name != '' }} with: ref: ${{ github.workflow_sha }} path: ${{ github.action_path }}/repo - - id: job-name-tpl + - id: workflow + if: ${{ inputs.job-name != '' }} shell: bash run: | - # Determine job name template + # Workflow job name template [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${repo_path}/${workflow_path:?}")" - echo "string=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" + echo "job-name-template=${job_name_template:?}" | tee -a "$GITHUB_OUTPUT" env: repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} job_key: ${{ github.job }} - - id: job-name + - id: render + if: ${{ inputs.job-name != '' }} shell: bash run: | # Render job name @@ -43,14 +56,14 @@ runs: matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" if [[ -n "${matrix_values}" ]]; then - job_name="${job_name_template} (${matrix_values})" + job_name="${job_name_expr} (${matrix_values})" else - job_name="${job_name_template}" + job_name="${job_name_expr}" fi fi - echo "string=${job_name:?}" | tee -a "$GITHUB_OUTPUT" + echo "job-name=${job_name:?}" | tee -a "$GITHUB_OUTPUT" env: - job_name_template: ${{ steps.job-name-tpl.outputs.string }} + job_name_template: ${{ steps.workflow.outputs.job-name-template }} # `jobs..name` can use the contexts: [github, needs, strategy, matrix, vars, inputs] # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/contexts#context-availability @@ -60,3 +73,19 @@ runs: matrix_json: ${{ toJSON(matrix) }} # vars_json: ${{ toJSON(vars) }} # inputs_json: ${{ toJSON(inputs) }} + - id: job-api + shell: bash + run: | + # Fetch job ID + jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" + job_ids="$(jq -c --arg name "$job_name" '[.jobs | select(.name == $name) | .id]')" + echo "job-ids=${job_ids:?}" | tee -a "$GITHUB_OUTPUT" + + if [[ $(jq length <<<"${job_ids}") -eq 1 ]]; then + echo "job-id=$(jq 'first // empty' <<<"${job_ids}")" | tee -a "$GITHUB_OUTPUT" + fi + env: + GH_TOKEN: ${{ github.token }} + run_id: ${{ github.run_id }} + run_attempt: ${{ github.run_attempt }} + job_name: ${{ inputs.job-name || steps.render.outputs.job-name }} From d9716234797c8e809e3c42bae3a463851ab821ce Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:07:15 -0500 Subject: [PATCH 41/48] fixup! Refactor --- .github/workflows/integration-tests.yaml | 1 + job-name/action.yaml | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index 2eb9b53..f214e58 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -155,6 +155,7 @@ jobs: name: Job Name runs-on: ubuntu-latest permissions: + actions: read contents: read steps: - uses: actions/checkout@v4 diff --git a/job-name/action.yaml b/job-name/action.yaml index 485ad7e..80576a1 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -19,12 +19,12 @@ runs: using: composite steps: - uses: actions/checkout@v4 - if: ${{ inputs.job-name != '' }} + if: ${{ inputs.job-name == '' }} with: ref: ${{ github.workflow_sha }} path: ${{ github.action_path }}/repo - id: workflow - if: ${{ inputs.job-name != '' }} + if: ${{ inputs.job-name == '' }} shell: bash run: | # Workflow job name template @@ -38,7 +38,7 @@ runs: workflow_ref: ${{ github.workflow_ref }} job_key: ${{ github.job }} - id: render - if: ${{ inputs.job-name != '' }} + if: ${{ inputs.job-name == '' }} shell: bash run: | # Render job name From af3df40ff039e6b4c4b62d13a1c38e2e11a3020a Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:08:09 -0500 Subject: [PATCH 42/48] fixup! Refactor --- job-name/action.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 80576a1..4425d0d 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -56,9 +56,9 @@ runs: matrix_values="$(jq -r '[.. | select(type != "object")] | join(", ")' <<<"${matrix_json}")" if [[ -n "${matrix_values}" ]]; then - job_name="${job_name_expr} (${matrix_values})" + job_name="${job_name_template} (${matrix_values})" else - job_name="${job_name_expr}" + job_name="${job_name_template}" fi fi echo "job-name=${job_name:?}" | tee -a "$GITHUB_OUTPUT" From 45b4337bdada493bef5c3eaea2cb27b044d77515 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:09:03 -0500 Subject: [PATCH 43/48] Debug --- job-name/action.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/job-name/action.yaml b/job-name/action.yaml index 4425d0d..4dcc3cf 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -77,6 +77,9 @@ runs: shell: bash run: | # Fetch job ID + [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x + set -euo pipefail + set -x jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" job_ids="$(jq -c --arg name "$job_name" '[.jobs | select(.name == $name) | .id]')" echo "job-ids=${job_ids:?}" | tee -a "$GITHUB_OUTPUT" From 8d24c7f51483451d840e5b358cfa696683ff136c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:11:02 -0500 Subject: [PATCH 44/48] fixup! Refactor --- job-name/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 4dcc3cf..002f5e6 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -81,7 +81,7 @@ runs: set -euo pipefail set -x jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" - job_ids="$(jq -c --arg name "$job_name" '[.jobs | select(.name == $name) | .id]')" + job_ids="$(jq -c --arg name "$job_name" '[.jobs[] | select(.name == $name) | .id]')" echo "job-ids=${job_ids:?}" | tee -a "$GITHUB_OUTPUT" if [[ $(jq length <<<"${job_ids}") -eq 1 ]]; then From 9fa30416493eb1b397774ce68e9a789330aaa876 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:12:31 -0500 Subject: [PATCH 45/48] fixup! Refactor --- job-name/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 002f5e6..4289018 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -81,7 +81,7 @@ runs: set -euo pipefail set -x jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" - job_ids="$(jq -c --arg name "$job_name" '[.jobs[] | select(.name == $name) | .id]')" + job_ids="$(jq -c --arg name "$job_name" '[.jobs[] | select(.name == $name) | .id]' <<<"${jobs}")" echo "job-ids=${job_ids:?}" | tee -a "$GITHUB_OUTPUT" if [[ $(jq length <<<"${job_ids}") -eq 1 ]]; then From 15f3cf49ae3c173364f56c02a0e8018080e910b3 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:16:43 -0500 Subject: [PATCH 46/48] Cleanup --- job-name/action.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/job-name/action.yaml b/job-name/action.yaml index 4289018..24f814f 100644 --- a/job-name/action.yaml +++ b/job-name/action.yaml @@ -3,8 +3,6 @@ name: Job Context description: inputs: job-name: - description: >- - A string representing YAML data. Typically, a simple dictionary of key/value pairs. type: string default: "" required: false @@ -29,6 +27,8 @@ runs: run: | # Workflow job name template [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x + set -euo pipefail + # e.g. octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch -> .github/workflows/my-workflow.yml workflow_path="$(echo "${workflow_ref%@*}" | cut -d/ -f3-)" job_name_template="$(job_key="${job_key}" yq '.jobs[env(job_key)].name' "${repo_path}/${workflow_path:?}")" @@ -36,7 +36,7 @@ runs: env: repo_path: ${{ github.action_path }}/repo workflow_ref: ${{ github.workflow_ref }} - job_key: ${{ github.job }} + job_key: ${{ github.job }} # User supplied job key cannot be used as we cannot properly render `matrix` expressions - id: render if: ${{ inputs.job-name == '' }} shell: bash @@ -44,7 +44,7 @@ runs: # Render job name [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x set -euo pipefail - set -x + # Determine if job name contains GitHub expressions. As GitHub expressions must be # closed we can just check for the opening sequence. if grep -qE '\$\{\{' <<<"${job_name_template}"; then @@ -79,7 +79,7 @@ runs: # Fetch job ID [[ "$RUNNER_DEBUG" -eq 1 ]] && set -x set -euo pipefail - set -x + jobs="$(gh api -X GET "/repos/{owner}/{repo}/actions/runs/${run_id:?}/attempts/${run_attempt:?}/jobs")" job_ids="$(jq -c --arg name "$job_name" '[.jobs[] | select(.name == $name) | .id]' <<<"${jobs}")" echo "job-ids=${job_ids:?}" | tee -a "$GITHUB_OUTPUT" From 07840290b6da1d44da17fbe921afb09f38f5da7a Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:23:26 -0500 Subject: [PATCH 47/48] Rename to job-context --- .github/workflows/integration-tests.yaml | 51 +++++++++++++++++++----- {job-name => job-context}/action.yaml | 0 2 files changed, 40 insertions(+), 11 deletions(-) rename {job-name => job-context}/action.yaml (100%) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index f214e58..d717184 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -159,12 +159,12 @@ jobs: contents: read steps: - uses: actions/checkout@v4 - - uses: ./job-name/ - id: job-name + - uses: ./job-context/ + id: job-context - name: Matches expected name run: '[[ "${output}" == "${expected}" ]] || exit 1' env: - output: ${{ steps.job-name.outputs.job-name }} + output: ${{ steps.job-context.outputs.job-name }} expected: "Job Name" test-job-name-matrix: @@ -186,12 +186,12 @@ jobs: - "2.0" steps: - uses: actions/checkout@v4 - - uses: ./job-name/ - id: job-name + - uses: ./job-context/ + id: job-context - name: Matches expected name run: '[[ "${output}" == "${expected}" ]] || exit 1' env: - output: ${{ steps.job-name.outputs.job-name }} + output: ${{ steps.job-context.outputs.job-name }} expected: "Job Name (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})" - name: Matches GitHub API name run: | @@ -204,7 +204,7 @@ jobs: GH_TOKEN: ${{ github.token }} run_id: ${{ github.run_id }} run_attempt: ${{ github.run_attempt }} - job_name: ${{ steps.job-name.outputs.job-name }} + job_name: ${{ steps.job-context.outputs.job-name }} test-job-name-matrix-expr: # TODO: Using `github.job` with any expressions results in it being empty @@ -221,12 +221,12 @@ jobs: - 2 steps: - uses: actions/checkout@v4 - - uses: ./job-name/ - id: job-name + - uses: ./job-context/ + id: job-context - name: Matches expected name run: '[[ "${output}" == "${expected}" ]] || exit 1' env: - output: ${{ steps.job-name.outputs.job-name }} + output: ${{ steps.job-context.outputs.job-name }} expected: ${{ github.event_name }} - - ${{ matrix.index }} - ${{ strategy.job-index }} - name: Matches GitHub API name run: | @@ -239,4 +239,33 @@ jobs: GH_TOKEN: ${{ github.token }} run_id: ${{ github.run_id }} run_attempt: ${{ github.run_attempt }} - job_name: ${{ steps.job-name.outputs.job-name }} + job_name: ${{ steps.job-context.outputs.job-name }} + + test-job-name-ambiguous: + name: ${{ github.job }} + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + strategy: + fail-fast: false + matrix: + index: + - 1 + - 2 + steps: + - uses: actions/checkout@v4 + - uses: ./job-context/ + id: job-context + - name: Matches expected name + run: '[[ "${output}" == "${expected}" ]] || exit 1' + env: + output: ${{ steps.job-context.outputs.job-name }} + expected: ${{ github.job }} + - name: Matches GitHub API name + run: | + [[ $(jq length <<<"${job_ids}") -eq 2 ]] || exit 1 + [[ -z "${job_id}" ]] || exit 1 + env: + job_ids: ${{ steps.job-context.outputs.job-ids }} + job_id: ${{ steps.job-context.outputs.job-id }} diff --git a/job-name/action.yaml b/job-context/action.yaml similarity index 100% rename from job-name/action.yaml rename to job-context/action.yaml From 21b194f8f9ce4b9e6db7c0dcfe43bfd5e85167d8 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 13 Aug 2024 14:24:33 -0500 Subject: [PATCH 48/48] fixup --- .github/workflows/integration-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index d717184..f6a363e 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -262,7 +262,7 @@ jobs: env: output: ${{ steps.job-context.outputs.job-name }} expected: ${{ github.job }} - - name: Matches GitHub API name + - name: Matches multiple jobs run: | [[ $(jq length <<<"${job_ids}") -eq 2 ]] || exit 1 [[ -z "${job_id}" ]] || exit 1