How to pass success() or failure() to a github action from a workflow
Asked Answered
K

3

5

So I have a github workflow that calls a composite action at the end, regardless of success or failure. I want to pass whether or not the workflow has succeeded to the action to use within it. I have tried passing success() as bellow, I have also tried writing success into a variable in a step and then calling that variable. Is success() only able to be called inside a if:, and if so is there any similar way to get the workflows status.

The code I have tried looks like this:

...previous steps

- name: The step to call the action
  if: always()
  uses: ./path/to/action
  with:
    some-token: ${{ inputs.some-token }}
    some-value: ${{ inputs.some-value }}
    workflow-status: ${{ success() }}
Kries answered 11/4, 2023 at 14:55 Comment(0)
H
3

You could use the outcome of the previous step.

when a step fails. Set to true to allow a job to pass when this step fails

Check the outcome of the step in order to understand if was failed or not like:

steps.<id>.outcome != 'success'

See outcome doc here and continue-on-error

Hooch answered 11/4, 2023 at 15:12 Comment(1)
So this was able to solve the issue. I created a throw away step as a "check step", I could then pass the outcome of this step as a parameter and finally in my action simply check if the input was == success Thanks @HoochKries
K
3

Solution I used based off @Matteos comment:

- name: Check status step
  id: check-step
  if: success()
  shell: bash
  run: |
    echo "Workflow successful"

- name: The step to call the action
   if: always()
   uses: ./path/to/action
   with:
    some-token: ${{ inputs.some-token }}
    some-value: ${{ inputs.some-value }}
    workflow-status: ${{ steps.check-step.outcome }}
Kries answered 11/4, 2023 at 15:59 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Whomever
C
2

Job status can be passed as follows

- name: Step
  uses: ./path/to/action
  if: always()
  with:
    # possible values: success, failure, cancelled
    status: ${{ job.status }}

then in composite action:

inputs:
  status:
    description: 'Status'
    required: true
runs:
  using: "composite"
  steps:
    - name: Notify Slack about failure
      if: inputs.status == 'failure'
      (...)
Crunode answered 30/8, 2024 at 12:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.