Count the total number of successful steps in a job using github actions

Hi all,
This is for getting the total number of successful steps in a Github action job.
I need this value for further decision making.
Example:

name: Litmus-CI
on:
  issue_comment:
    types: [created, edited]

jobs:
  job1:
    if: contains(github.event.comment.html_url, '/pull/') && startsWith(github.event.comment.body, '/run-e2e')
    runs-on: ubuntu-latest
    steps:

   ...step1...

   ...step2...
  
   ...step3...

   ...step4...

   ...step5...

Let us suppose I want to run step5 only if anyone of step4 or step3 ran successfully(or both doesn’t skip) how to build such logic around this?

If I have some parameters to get the total number of steps ran successfully then this would be done easily.

Hi @uditgaurav,

Step status can be got from step context: ‘steps.step_id.outcome’ or ‘steps.step_id.conclusion’. Please check the official doc for more details.
You can use a combined expression for step5, code sample as below:

      - name: step3
        id: step3
        continue-on-error: true
        run: |
          exit 1
      - name: step4
        id: step4
        continue-on-error: true
        run: |
          exit 1
      - name: step5
        if: always()&&(steps.step3.outcome=='success' || steps.step4.outcome=='success') # step5 will be skipped if both step3 and step4 are not successful.
        run: |
          echo ${{ steps.step3.outcome }}
          echo ${{ steps.step4.outcome }}

Please change the outcome value according to your requirements.

You can check the step context at the last of the job.

      - name: Dump steps context
        env:
          STEPS_CONTEXT: ${{ toJson(steps) }}
        run: echo "$STEPS_CONTEXT"

My output is as below, you can parse the context of outcome value, and caculate the numbe of successful steps in the job.
stepcontext

Thanks.

1 Like

Hi @weide-zhou, I’m running my job on push and have only been seeing an empty object {} returned when I encode the steps object. Not entirely sure what I might be doing wrong, my action is the copy pasted contents of: https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#example-printing-context-information-to-the-log-file

Hi @blakethepatton,

Please confirm that you add ‘id’ for your steps.

      - name: step4
        id: step4     # add step id here!
        continue-on-error: true
        run: |
          exit 1
      - name: Dump steps context
        env:
          STEPS_CONTEXT: ${{ toJson(steps) }}
        run: echo "$STEPS_CONTEXT"

If there still no output, please share your yaml for further investigation.

Thanks.

Yes @weide-zhou, adding id made the steps visible. Thank you so much I’ve spent probably three hours now trying to figure that out! What is the purpose of ID verses name? Other than id implies that it will be unique to that step?

Hi @blakethepatton,

step id is a unique identifier for the step. You can use the id to reference the step in contexts. It’s mentioned in the doc here.

Thanks

Is there a way to access steps from inside a javascript runner without having to explicitly pass it in through an input variable? Additionally, is there a way to get the message behind why a step failed? Or would you need to redirect the console output of the command you’re running into a output variable and then access the error that way.