Run job after dependent jobs, no matter if they fail or succeed

circle.yml
2.0

#1

I have workflows set up like this:

workflows:
  version: 2
  jobs:
   - checkout_environment
   - test_e2e
     requires:
       - checkout_environment
   - test_unit
     requires:
       - checkout_environment

Now I want to run a job after all the previous jobs are done, which I can achieve like this:

workflows:
  version: 2
  jobs:
   - checkout_environment
   - test_e2e
   - test_unit
   - cleanup
     requires:
       - checkout_environment
       - test_unit
       - test_e2e

The problem is that I also want to run the cleanup job no matter if any of the dependent jobs fail. I always always always want to run the cleanup job. I know I can do this inside of a job itself with the when: always key, but I only want to run it once not once per job.

It’d be great if workflow jobs also had the when option like this:

workflows:
  version: 2
  jobs:
   ...
   - cleanup
     when: always
     requires:
       ...

Hope that makes sense!


#2

Workaround:
Setting environment variable if it is failed or succeeded.
Run cleanup.
Run exit $SUCCESS.
This will run cleanup and stops after an error. (It is very like the try-catch-finally block)


#3