Ankur Gupta
Ankur Gupta

Reputation: 153

Stop the gitlab pipeline if previous stages failed

I have a gitlab pipeline which consiste of multiple stages[checkout->build->test->Deploy->cleanup->validation->terminate ec2].I have 3 requirement.

  1. if any stage before the deploy stage failed, the pipeline stopped immediately.
  2. if the deploy stage succeeds then the cleanup stage must skip and continue with another stage.
  3. when only the deploy stage failed then only and must clean up stage run and exit the pipeline.

Here is my example pipeline.

stages: 
  - checkout
  - build
  - test
  - Deploy
  - cleanup
  - reset
  - Jenkins Jobs
  - validation


checkout:
    stage: checkout
    tags:
        - publishing
    script:
        - echo checkout

build:
    stage: build
    tags:
      - automated-content-publishing
    script:
      -  echo build

test:
    stage: test
    tags:
      - publishing
    script:
      - echo test

Deploy:
  stage: Deploy
  tags:
      - publishing
  script:
      - echo deploy
    

Cleanup:
  stage: Cleanup
  tags:
    - publishing
  script:
    - echo clean-up
  when: on_failure

reset:
    stage: Nexus Insert
    tags:
      - publishing
    script:
      - echo reset
      

Jenksin Jobs:
  stage: Jenkins Jobs
  tags:
    - publishing
  script:
      - echo jenkins jobs

      

vilidation:
  stage: Vilidation
  tags:
    - publishing
  script:
    - echo validation
    

this pipeline works perfectly in 2 and 3 requirements. pipeline not working for 1 requirement. if any stage failed before the deploy stage failed cleanup stage call.

I want to call the cleanup stage only and only when the deploy stage failed.

Please guide me to build this case.

Upvotes: 4

Views: 8000

Answers (1)

Vers_us
Vers_us

Reputation: 333

You could use something like this

Deploy:
  stage: Deploy
tags:
  - publishing
script:
  - echo deploy

...

Cleanup:
  stage: Cleanup
  tags:
    - publishing
  script:
    - echo clean-up
  needs: 
    - job: Deploy
  when: on_failure

If deploy stage has many jobs just add them to needs section. For more info look at reference.

Upvotes: 1

Related Questions