Yogesh
Yogesh

Reputation: 305

How to set gitlab Pipeline status as failed if any of stage got failed

I have created a gitlab pipeline, in which I have created 8 stages. For each stage I have set the property **Allow_failure:true** so that it will execute remaining stages even if any stage got failed.

Currently, if any stage got failed, then the final pipeline status is showing as "! passed". I want to execute all the stages of the pipeline and if any stage got failed then I want to display pipeline status as failed.

Note: I can't change the value of property Allow_failure.

Please find attached Image for your reference.

enter image description here

Upvotes: 1

Views: 1349

Answers (1)

Jay Joshi
Jay Joshi

Reputation: 1562

There's no configuration given in Gitlab for this. So we'll mostly have to handle this using scripting.

Idea:

  • We add a new job at the end which validates that all previous jobs were successful. if it sees any failure, it will fail.
  • How to check?: We leverage the files/artifacts to pass on that information.
  • All the stages till the end will be executed by Gitlab (either passed or failed)

Output: Example

Minimal Snippet:


jobA:
  stage: A
  allow_failure: true
  script:
    - echo "building..."
    - echo "jobA" > ./completedA
  artifacts:
    paths:
      - ./completedA


jobB:
  stage: B
  allow_failure: true
  script:
    - echo "testing..."
    - exit 1
    - echo "jobB" > ./completedB
  artifacts:
    paths:
      - ./completedB

jobC:
  stage: C
  allow_failure: true
  script:
    - echo "deplying..."
    - echo "jobC" > ./completedC
  artifacts:
    paths:
      - ./completedC


validate: 
  stage: Validate 
  script: 
    - |
      if [[ -f ./completedA && -f ./completedB && -f ./completedC ]]; then
      echo "All stages were completed"
      else
      echo "Stages were not completed"
      exit 1
      fi

stages:
  - A
  - B
  - C
  - Validate

Upvotes: 2

Related Questions