diff --git a/.github/workflows/no-test.yml b/.github/workflows/no-test.yml index d0ebfab..48d37e3 100644 --- a/.github/workflows/no-test.yml +++ b/.github/workflows/no-test.yml @@ -8,6 +8,7 @@ on: - '**' - '!.github/workflows/test-*' - '!buildkite/run/**' + - '!check-dependent-jobs/**' - '!git/setup/**' - '!google/auth/**' - '!updatecli/run/**' diff --git a/.github/workflows/test-check-dependent-jobs.yml b/.github/workflows/test-check-dependent-jobs.yml new file mode 100644 index 0000000..35b6e2c --- /dev/null +++ b/.github/workflows/test-check-dependent-jobs.yml @@ -0,0 +1,42 @@ +name: test-check-dependent-jobs + +on: + pull_request: + branches: + - main + paths: + - '.github/workflows/test-check-dependent-jobs.yml' + - 'check-dependent-jobs/**' + push: + branches: + - main + paths: + - '.github/workflows/test-check-dependent-jobs.yml' + - 'check-dependent-jobs/**' + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ./check-dependent-jobs + id: check + with: + jobs: '{ "job-a": { "result": "success" }, "job-b": { "result": "success" } }' + - name: assert outputs.is-success==true + run: test "${{ steps.check.outputs.is-success }}" = "true" + - uses: ./check-dependent-jobs + id: check-2 + with: + jobs: '{ "job-a": { "result": "success" }, "job-b": { "result": "failure" } }' + - name: assert outputs.is-success==false + run: test "${{ steps.check-2.outputs.is-success }}" = "false" + - uses: ./check-dependent-jobs + id: check-3 + with: + jobs: '{ "job-a": { "result": "failure" }, "job-b": { "result": "failure" } }' + - name: assert outputs.is-success==false + run: test "${{ steps.check-3.outputs.is-success }}" = "false" diff --git a/check-dependent-jobs/README.md b/check-dependent-jobs/README.md new file mode 100644 index 0000000..8a1b480 --- /dev/null +++ b/check-dependent-jobs/README.md @@ -0,0 +1,49 @@ +# check-dependent-jobs + + +Evaluates the combined the status results of the provided needs context. + + +## Inputs + +| Name | Description | Required | Default | +|--------|------------------------------|----------|---------| +| `jobs` | needs context as JSON string | `true` | ` ` | + + +## Outputs + + +| Name | Description | +|--------------|-----------------------------------------------------------------| +| `is-success` | The evaluated result of all provided jobs in the needs context. | +| `status` | One of success or failure. | + + +## Usage + + +```yaml +jobs: + job-a: + runs-on: ubuntu-latest + steps: + - run: exit 1; + job-b: + runs-on: ubuntu-latest + steps: + - run: exit 0; + job-c: + if: always() + runs-on: ubuntu-latest + needs: + - job-a + - job-b + steps: + - id: check + uses: elastic/oblt-actions/check-dependent-jobs@v1 + with: + jobs: ${{ toJSON(needs) }} + - run: ${{ steps.check.outputs.is-success }} # should exit with 1 or 0. +``` + diff --git a/check-dependent-jobs/action.yml b/check-dependent-jobs/action.yml new file mode 100644 index 0000000..9725caf --- /dev/null +++ b/check-dependent-jobs/action.yml @@ -0,0 +1,27 @@ +name: check-dependent-jobs +description: | + Evaluates the combined the status results of the provided needs context. +inputs: + jobs: + required: true + description: needs context as JSON string +outputs: + is-success: + description: The evaluated result of all provided jobs in the needs context. + value: ${{ steps.test.outputs.is-success }} + status: + description: One of success or failure. + value: ${{ steps.test.outputs.status }} +runs: + using: composite + steps: + - id: test + uses: actions/github-script@v7 + with: + script: | + const jobs = JSON.parse(process.env.JOBS) + const isSuccess = Object.values(jobs).every(job => job.result === 'success') + core.setOutput('is-success', isSuccess) + core.setOutput('status', isSuccess ? 'success' : 'failure') + env: + JOBS: ${{ inputs.jobs }}