Skip to content

Commit

Permalink
Initial implementation (#1)
Browse files Browse the repository at this point in the history
* Initial implementation

* Fallback on using job key

* Add trailing newline

* Create LICENSE

* Be explicit about expected names

* Always test for job ID

* Test ambiguous output
  • Loading branch information
omus authored Sep 17, 2024
1 parent f354a63 commit abc4b53
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 2 deletions.
221 changes: 221 additions & 0 deletions .github/workflows/integration-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
---
name: Integration Tests
on:
pull_request:
paths:
- "action.yaml"
- ".github/workflows/integration-tests.yaml"

jobs:
test-default-name:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: test-default-name # ${{ github.job }}
- name: Test job ID
run: |
[[ "${job_id}" =~ ^[0-9]+$ ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}

test-custom-name:
name: Custom Name
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: Custom Name
- name: Test job ID
run: |
[[ "${job_id}" =~ ^[0-9]+$ ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}

test-empty-name:
name: ""
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: test-empty-name # ${{ github.job }}
- name: Test job ID
run: |
[[ "${job_id}" =~ ^[0-9]+$ ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}

test-null-name:
name: null
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: test-null-name # ${{ github.job }}
- name: Test job ID
run: |
[[ "${job_id}" =~ ^[0-9]+$ ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}

test-false-name:
name: false
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: false # ${{ github.job }}
- name: Test job ID
run: |
[[ "${job_id}" =~ ^[0-9]+$ ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}

test-matrix:
name: Matrix
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"
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: Matrix (${{ matrix.build.name }}, ${{ matrix.build.repo }}, ${{ matrix.version }})
- name: Test job ID
run: |
[[ "${job_id}" =~ ^[0-9]+$ ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}
- name: Test job name is distinct
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' <<<"${jobs}") -ne 1 ]]; then
jq '.jobs[].name' <<<"${jobs}"
exit 1
fi
env:
GH_TOKEN: ${{ github.token }}
run_id: ${{ github.run_id }}
run_attempt: ${{ github.run_attempt }}
job_name: ${{ steps.job.outputs.name }}

test-matrix-expr:
name: Matrix Expression - ${{ github.event_name }} - ${{ matrix.dne }} - ${{ matrix.index }} - ${{ strategy.job-index }}
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
strategy:
fail-fast: false
matrix:
index:
- 1
- 2
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: Matrix Expression - ${{ github.event_name }} - - ${{ matrix.index }} - ${{ strategy.job-index }}
- name: Test job ID
run: |
[[ "${job_id}" =~ ^[0-9]+$ ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}
- name: Test job name is distinct
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' <<<"${jobs}") -ne 1 ]]; then
jq '.jobs[].name' <<<"${jobs}"
exit 1
fi
env:
GH_TOKEN: ${{ github.token }}
run_id: ${{ github.run_id }}
run_attempt: ${{ github.run_attempt }}
job_name: ${{ steps.job.outputs.name }}

test-ambiguous:
# Using `github.job` with any other expressions present results in it being empty
name: ${{ github.job }}
runs-on: ubuntu-latest
permissions:
contents: read
strategy:
fail-fast: false
matrix:
index:
- 1
- 2
steps:
- uses: actions/checkout@v4
- uses: ./
id: job
continue-on-error: true
- name: Action failed
if: ${{ steps.job.outcome != 'failure' }}
run: exit 1
- name: Test job name
run: '[[ "${output}" == "${expected}" ]] || exit 1'
env:
output: ${{ steps.job.outputs.name }}
expected: test-ambiguous
- name: Test job ID empty
run: |
[[ -z "${job_id}" ]] || exit 1
env:
job_id: ${{ steps.job.outputs.id }}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Beacon Biosignals

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
# job-context
Provides additional context for the currently running job
# Job Context

Provides additional context for the currently running job. GitHub Actions provides a [`job` context](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#job-context) but is missing some pieces including the job name and the job ID (the numeric value as used by the GitHub API).

## Requirements

The `job-context` action requires that the job in which this action is used has a job name that is unique within the workflow file. By default job names are unique so this is only a problem if you specify a custom `jobs.<job_key>.name`. If the job name is not unique within the workflow this action will fail and report the ambiguous job name.

Additionally, this job currently does not support job names which utilize GHA expressions using the contexts: `needs`, `vars`, or `inputs`.

## Examples

```yaml
# CI.yaml
jobs:
demo:
name: Demo
# These permissions are needed to:
# - Use `job-context`: https://github.com/beacon-biosignals/job-context#permissions
permissions:
context: read
runs-on: ubuntu-latest
strategy:
matrix:
version:
- "1.0"
- "2.0"
steps:
- uses: beacon-biosignals/job-context@v1
id: job
- run: |
echo "job-name=${{ steps.job.outputs.name }} # e.g. Demo (1.0)
echo "job-id=${{ steps.job.outputs.id }} # e.g. 28842064821
```
## Inputs
The `job-context` action supports the following inputs:

| Name | Description | Required | Example |
|:-----------------|:------------|:---------|:--------|
| `path` | The path to the cloned repo containing the workflow. Should only be needed by other GHAs using this action. | No | <pre><code>${{ github.action_path }}/repo</code></pre> |

## Outputs

| Name | Description | Example |
|:-------|:------------|:--------|
| `name` | The rendered job name. | <pre><code>Demo (1.0)</code></pre> |
| `id` | The numeric job ID as used by the GitHub API. Not be be confused with the workflow job key `github.job`. | <pre><code>28842064821</code></pre> |

## 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:
context: read
```
Loading

0 comments on commit abc4b53

Please sign in to comment.