How to run a workflow when the previous one failed

How to run a workflow when the previous one failed

This article outlines how you can use the when attribute with the on_fail parameter in your CircleCI configuration to run jobs selectively based on the status of previous jobs.

Maximizing Workflow Control on Failures

Using the `when: on_fail` Attribute

To run a job when a previous job has failed, you can employ the when: on_fail attribute in your **CircleCI configuration file** (config.yml). This allows you to define steps that should execute only under failure conditions. Here's a step-by-step guide:

1. Open your config.yml file in your CircleCI project.
2. Add the when: on_fail attribute to the step you wish to run upon the failure of any preceding step.

Example Configuration

Here’s an example demonstrating how to use the `when: on_fail` attribute effectively:

yaml
jobs:
example-job:
docker:
- image: circleci/python:3.8
steps:
- run:
name: Run commands
command: |
echo "I will exit fail status"
exit 1
- run:
name: Run on fail status
command: |
echo "I am the result of the above failed job"
when: on_fail

In this example:

  • The first step will intentionally exit with a failure status (exit 1).
  • The second step, which uses the when: on_fail attribute, will execute only if the first step fails.

Important Considerations

- It’s crucial to note that the when: on_fail attribute works for steps within a job only.
- CircleCI does not currently support automatic retries of failed jobs or workflows directly.

If you intend to run a separate job after a failure in a broader workflow context, you may need to consider implementing a more complex setup that could involve the CircleCI API or custom scripts.

Additional Notes

Due to the current limitations in CircleCI, using when: on_fail provides a degree of control within a job but does not extend to managing separate jobs in a workflow upon failure. Keep this in mind while designing your CI/CD processes to ensure you have a suitable fallback mechanism or error handling strategy in place.

By following the steps and guidelines outlined here, you can enhance your workflow management in CircleCI, ensuring that even on failures, you can respond and execute necessary actions to maintain your development processes smoothly.

Additional Resources

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.