Search code examples
github-actions

Github Actions: check steps status


I have in my job for CI some steps which can throw an error. I don't want restart workflow on every step with error and want to go to the last step that checks those steps and complete this job as fail. But I can't get status info previously steps.

name: CI
on: [pull_request]
jobs:
  myjob:
    runs-on: ubuntu-latest
    steps:
      - name: Step 1
        id: hello
        run: <any> 
        continue-on-error: true
      - name: Step 2
        id: world
        run: <any> 
        continue-on-error: true
      - name: Check on failures
        if: job.steps.hello.status == failure() || job.steps.world.status == failure()
        run: exit 1

When I use next constructions in "if" or "run" then will get: steps -> {}, job.steps -> null.

How can I get status information?


Solution

  • Update: The steps context now contains detail about the execution of each step by default. Using the outcome property of each step we can check the result of its execution.

    name: CI
    on: [pull_request]
    jobs:
      myjob:
        runs-on: ubuntu-latest
        steps:
          - name: Step 1
            id: hello
            run: <any> 
            continue-on-error: true
          - name: Step 2
            id: world
            run: <any> 
            continue-on-error: true
          - name: Check on failures
            if: steps.hello.outcome != 'success' || steps.world.outcome != 'success'
            run: exit 1
    

    Original answer Looking at the documentation for the steps context, it doesn't look like it contains any information about the step other than outputs. These must be explicitly defined by steps. That is why the steps context is empty {}.

    https://help.github.com/en/articles/contexts-and-expression-syntax-for-github-actions#steps-context

    Unfortunately, as far as I can tell, there is no default status for a step that can be accessed. The solution involves manually defining a status output variable from each step.

    name: CI
    on: [pull_request]
    jobs:
      myjob:
        runs-on: ubuntu-latest
        steps:
          - name: Step 1
            id: hello
            run: echo ::set-output name=status::failure
            continue-on-error: true
          - name: Step 2
            id: world
            run: echo ::set-output name=status::success
            continue-on-error: true
          - name: Dump steps context
            env:
              STEPS_CONTEXT: ${{ toJson(steps) }}
            run: echo "$STEPS_CONTEXT"
          - name: Check on failures
            if: steps.hello.outputs.status == 'failure' || steps.world.outputs.status == 'failure'
            run: exit 1
    

    This creates the following context output and the job fails.

    {
      "hello": {
        "outputs": {
          "status": "failure"
        }
      },
      "world": {
         "outputs": {
          "status": "success"
        }
      }
    }
    

    https://help.github.com/en/articles/metadata-syntax-for-github-actions#outputs https://help.github.com/en/articles/development-tools-for-github-actions#set-an-output-parameter-set-output