Exit on success

Hi,

I am interested if there is an option to exit the workflow prematurely after a successful step. Is there another status code that can be used for these cases?

For example, if I explicitly return with exit 0, the step will be successful, but the job will continue with the other steps? Is there a way to skip all other steps without using conditionals for every subsequent step.

      - name: step one
        run: exit 0
      - name: step two
        run: ./something.sh

@ivanovaleksandar ,

Is there a way to skip all other steps without using conditionals for every subsequent step.

We have no methods to skip the subsequent steps without using the if conditional.
The if conditional is the designed and only way that used to skip jobs or steps in the workflow.

In a job, by default only when a step is failure, the subsequent steps will be automatically skipped. Every step in the job will be implicitly added the if statement with the success status (if: ${{ success() }}) by default.
If you want to skip the subsequent steps when the previous step is success, you must use the explicit if conditional with other statuses to override the default status, or combine with other statements.

Examples:

  • No explicit if conditional
- name: test step
  run: echo "This step does not have the explicit if statement".
# In fact, this step will follow the if statement "if: ${{ success() }}" by default.
# It only runs if all the previous steps in the same job are success.
  • Override the default status
- name: test step
  if: ${{ failure() }}
  run: echo "This step does not have the explicit if statement".
# This step will follow the if statement "if: ${{ failure() }}".
# It will run if any previous step in the same job is failure.
  • Combine with other statements
- name: test step
  if: ${{ github.event_name == 'push' }}
  run: echo "This step does not have the explicit if statement".
# In fact, this step will follow the if statement
# "if: ${{ success() && github.event_name == 'push' }}".
# It only runs when all the previous steps in the same job are 
# success AND the event name is 'push'.
1 Like

@brightran

Thank you for the explanation. After testing this I came to the same conclusion.

Nevertheless, is there a plan for this to change? Because this way, if you have 15 steps afterwards, there is a lot of boilerplate included just for this condition to be fulfilled. Also, it is kind of weird to have support for failure exit code (i.e exit 1 will terminate the workflow), while success means continue (as already mentioned in your answer).

@ivanovaleksandar ,

it is kind of weird to have support for failure exit code (i.e exit 1 will terminate the workflow)

Essentially, it is not that the failed step directly terminate the job or the workflow run. It just follows the implicit statement “if: ${{ success() }}” to orderly skip all the subsequent steps to make the job completed. This is consistent with what I mentioned above.

Nevertheless, is there a plan for this to change?

I do not find any plan about this.
If you really need the related features, I recommend that you can directly report a feature request here.
That will allow you to directly interact with the appropriate engineering team, and make it more convenient for the engineering team to collect and categorize your suggestions.

In addition, you also can follow the “GitHub public roadmap” repository where you can learn about what features the appropriate engineering teams are working on, what stage the features are in, and when the teams expect to bring them to you.
When the new features are published, you can view the release note from “GitHub Changelog”.

@brightran

Thank you a lot for the detailed explanation. :+1: :metal: