Next job is running even previous is failing while using rules api

I have three stages in my yml file and i want them to execute in sequence and if any one fails then just don’t want to execute next job. but behaviour is not as expected

stages:
  • build
  • sonar
  • wsdev
  • wsprod

build:
stage: build
script:
- /mvn-bd.sh {SKIP_TESTS} {CI_COMMIT_REF_NAME}
tags:
- medium-runner

sonar:
stage: sonar
script:

  • echo "sonar Build started for for ${COMPONENT_NAME} "
  • /mvn-bd.sh {SKIP_TESTS} {CI_COMMIT_REF_NAME} ${SKIP_DEPLOY}
  • mvn -s /settings.xml sonar:sonar -Dsonar.host.url=$SONAR_HOST -Dsonar.projectKey=evb-$COMPONENT_NAME -Dsonar.login=$SONAR_TOKEN
    rules:
    - if: $CI_MERGE_REQUEST_ID
    when: never
    - if: CI_COMMIT_REF_NAME=~/dev/ || $CI_COMMIT_REF_NAME == “master” && $CI_MERGE_REQUEST_ID == null
    when: always
    tags:
  • medium-runner

wsdev:
stage: wsdev
script:

  • echo “Whitesource Build started for for ws dev ${COMPONENT_NAME}”
  • /mvn-bd.sh {SKIP_TESTS} {CI_COMMIT_REF_NAME} ${SKIP_DEPLOY}
  • sh /wss_agent.sh -c /wss-unified-agent.config -userKey $WS_USER_KEY -apiKey $WS_API_TOKEN -product $WS_P_NAME -project $COMPONENT_NAME
    rules:
    - if: $CI_MERGE_REQUEST_ID
    when: never
    - if: CI_COMMIT_REF_NAME=~/dev/ && $CI_MERGE_REQUEST_ID == null
    when: always
    tags:
  • medium-runner

I believe the reason is due to your rules sections for your other jobs.

Setting when: always will always make the job run (this is generally used for cleanup tasks at the end of a pipeline, or a notification job that emails users that should run regardless if previous stages failed or succeeded).

You should be using when: on_success to only run the next stages when all previous stages have been successful.

Documentation regarding when options can be found here