8

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.

Sparc Splunk
  • 93
  • 1
  • 1
  • 5
  • Is it wanted that the 3 stages are not linked? The 3rd does not depend on the 2nd for instance. – Gaël J Sep 05 '21 at 11:56

2 Answers2

20
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
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
2

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
Steve Whitmore
  • 903
  • 1
  • 9
  • 22