Running a step on failure instead of command line

Hello! I have a step that I would like to re-run on when: on_fail, however it looks like the run parameter only takes in command line statements.

https://circleci.com/docs/2.0/configuration-reference/#run

Is there a way to tell this job to run a step called “failed-notifcation” if any of the steps fail?

jobs:
    build-test:
        docker:
            - image: cimg/python:3.8.6
        steps:
            - checkout
            - some-step
            - success_notification
            - run:
                name: Failure
                command: "failed_notification_step"
                when: on_fail
2 Likes

There is not currently a way to run a step on failure outside of the run step unless that step has a built-in method of running on fail.

You would likely need to build that into the step itself. For example, the CircleCI Slack Orb has a notify command that takes in an event parameter where fail can be passed in. Internally, that Orb makes use of the same run step when attribute you have linked above.

If this is an Orb, you may want to consider forking the Orb and modifying the run step from within that failed-notification step to include when.

If you wanted to send the source of the failed-notification step, we might be able to help make that adustment.