Hold if fail. Proceed if successful

I am running some tests. If the test are successful I want the workflow to continue. If the tests are a failure, I want to leave it “on hold” for an admin to see it (visual regression tests may fail, but an admin may decide “this was the expected visual result”).

Is there a way to make the workflow/jobs go on hold only if something fails?

Hi @koyan,

You can configure a step with “when: on_fail” for this (See: https://circleci.com/docs/2.0/configuration-reference/#the-when-attribute)

Hi @stella
Thanks for the answer, but this gives conditioning on the steps level, inside a job.
I want to trigger (or not trigger) a whole job based on the results of an other job.
I want

  • If job A fails -> trigger approval job B -> when approved trigger job C
  • If job A succeeds -> trigger job C

@koyan You should be able to achieve this using our v2 API which is currently in preview release mode. You can now define a “when” key in workflows to decide whether or not to run a workflow. More information about conditional workflows is here: https://github.com/CircleCI-Public/api-preview-docs/blob/master/docs/conditional-workflows.md

Here is an example approach.
You can restructure your configuration to have 3 workflows.
The workflow containing job A would become a conditional workflow that runs by default unless a certain pipeline parameter is given.
" approval job B -> when approved trigger job C" could be modelled as a second workflow that is conditional on a pipeline parameter, and there can be a third workflow that runs job C and that is also conditional on a pipeline parameter.

You can make job A invoke our pipeline triggering API here: https://github.com/CircleCI-Public/api-preview-docs/blob/master/docs/api-changes.md#post-projectproject_slugpipeline , passing in the appropriate pipeline parameter values to control whether the 2nd or 3rd workflow will get run, and making sure the 1st workflow does not get run.

Thanks @stella .
We ended up following your suggestion.

For the record, in case anyone wants to do the same.

a) We use the script https://github.com/greenpeace/planet4-backstop/blob/master/trigger_api.sh to trigger the different workflows inside the docker image we use.
b) We use in our script the logic we want to call the different workflows
https://github.com/greenpeace/planet4-backstop/blob/master/makecomparison.sh#L29-L41

  if [ $testresult = 0 ]  && [[ $git_message == *"[AUTO-PROCEED]"* ]]; then
    echo "The auto-proceed message exists and the testresult is 0"
    #Parameter 3 is release_init
    #Parameter 4 is release_hold
    #Parameter 5 is release_finish
    ./trigger_api.sh $CIRCLE_PROJECT_REPONAME $CIRCLE_BRANCH false false true
  else
    echo "Either the auto-proceed does not exist or the testresult is not 0"
    #Parameter 3 is release_init
    #Parameter 4 is release_hold
    #Parameter 5 is release_finish
    ./trigger_api.sh $CIRCLE_PROJECT_REPONAME $CIRCLE_BRANCH false true false
  fi;

c) We have setup the following in the repos config files:

version: 2.1

parameters:
  run_release_init:
    type: boolean
    default: true
  run_release_hold:
    type: boolean
    default: false
  run_release_finish:
    type: boolean
    default: false

and also:

workflows:
  release-init:
    when: << pipeline.parameters.run_release_init >>
    .
    .

  release-hold-and-finish:
    when: << pipeline.parameters.run_release_hold >>
    . 
    . 

  release-finish:
    when: << pipeline.parameters.run_release_finish >>
    . 
    . 

(It can all be seen at: https://github.com/greenpeace/planet4-international/blob/develop/.circleci/config.yml )

Works like a charm
Thanks

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.