Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab pipeline should stop on pipeline failure

enter image description hereI have a sample pipeline in gitlab. i want the the pipe to stop if any of the jobs fail. below is the code am using to replicate the scenario. the pipe does show failure as the status but the jobs continue to run even after a failed stage. example in the picture attached.

  stages:
  - unittest
  - coverage_test
  - static_code_analysis

variables:
  skip_integration:
    value: 'false'
    description: "This variable for skipping integration tests"
  skip_migration:
    value: 'false'

unittest:
  stage: unittest
  script:
    - echo "testing the code"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"' 
      when: always
    - if: '$skip_integration == "true"'
      when: never 
    - if: '$skip_integration == "false"'
      when: always

    
lint_test:
  stage: static_code_analysis
  allow_failure: false
  script:
    - echo "this is a test"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"' 
      when: always
    - if: '$skip_integration == "true"'
      when: never 
    - if: '$skip_integration == "false"'
      when: always
    - when: on_success

coverage_test:
  stage: coverage_test
  script:
    - echo00 "this is test again"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"' 
      when: always
    - if: '$skip_integration == "true"'
      when: never 
    - if: '$skip_integration == "false"'
      when: always
    - when: on_success

but the pipe does not stop on failure.

like image 628
Sparc Splunk Avatar asked Jan 22 '26 01:01

Sparc Splunk


2 Answers

when: always

As the name implies, runs the job always. If you want to run the job when the previous stage succeeded, run it on_success. Change all always to on_success.

when: on_success
like image 192
KamilCuk Avatar answered Jan 24 '26 21:01

KamilCuk


I've had success using needs for jobs that should only run if previous jobs succeed.

lint_test:
  stage: static_code_analysis
  allow_failure: false
  script:
    - echo "this is a test"
  needs:
    - coverage_test
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"' 
      when: always
    - if: '$skip_integration == "true"'
      when: never 
    - if: '$skip_integration == "false"'
      when: always
    - when: on_success
like image 41
swhitmore Avatar answered Jan 24 '26 20:01

swhitmore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!