Fallback via if: failure() without failing the job

Given pseudo steps like this:

- name: Pull docker image if available
        run: |
          docker pull A:TAG

      - name: ... else build and push Docker image
        if: failure()
        run: |
          docker build --tag A:B .
          docker push A:B

How do I succeed the overall job?

If I set continue-on-error for the first step the if: failure() will not trigger.

Without succeding this job I can not run other jobs with needs:

Thanks.

A failing step will cause the workflow to fail.  Instead of allowing the step to fail and then testing for it, you can set a variable when the docker pull fails, allow that step to succeed and then test for that variable.

For example:

- name: Pull docker image if available
        run: |
          docker pull A:TAG || echo "::set-env name=NEEDS_CONTAINER::true"
      - name: ... else build and push Docker image
        if: env.NEEDS_CONTAINER == 'true'
        run: |
          docker build --tag A:B .
          docker push A:B
2 Likes