A conditional step with a previous output and failure won't run

I wrote the following workflow file to invoke upload-artifact if do_test fails and hello.txt is outputted due to do_test. But contrary to what I want, the workflow always skips the last step, shown here. Is there any solution?

name: test
on:
  push:

jobs:
  test:
    name: annotate
    runs-on: ubuntu-latest
    steps:
# Instead of actual tests, write a file and return non-zero status to simulate the situation.
      - id: do_test
        run: |
          echo hello > hello.txt
          false

      - id: foo
        run: |
          if [-e 'hello.txt']; then
            echo "::set-output name=exists::true"
          fi
        if: failure()

      - name: check steps.foo.outputs.exists
        run: echo ${{ steps.foo.outputs.exists }}
        if: always()

      - uses: actions/upload-artifact@v2
        with:
          name: hello
          path: hello.txt
        if: steps.foo.outputs.exists

Hi @utgwkk ,

The upload-artifact step is not executed due to previous step ‘do_test’ failed. You need to add failure() expression check as well. Code sample as below:

steps:
      - id: do_test
        run: |
          echo hello > hello.txt
          false
      - id: foo
        run: |
          if [-e 'hello.txt']; then
            echo "::set-output name=exists::true"
          fi
        if: failure()

      - name: check steps.foo.outputs.exists
        run: echo ${{ steps.foo.outputs.exists }}
        if: always()

      - uses: actions/upload-artifact@v2
        with:
          name: hello
          path: hello.txt
<font color="#FF0000"> if: failure() &amp;&amp; (steps.foo.outputs.exists == 'true')</font>

Or you can use ‘contiue-on-error’ to make step1 always succeeds. Code as below:

steps:
      - id: do_test
<font color="#FF0000">continue-on-error: true</font> run: |
          echo hello > hello.txt
          false
      - id: foo
        run: |
          if [-e 'hello.txt']; then
            echo "::set-output name=exists::true"
          fi
      - name: check steps.foo.outputs.exists
        run: echo ${{ steps.foo.outputs.exists }}

      - uses: actions/upload-artifact@v2
        with:
          name: hello
          path: hello.txt
        if: steps.foo.outputs.exists == 'true'

Thanks.

1 Like