'How to stop a gitlab-ci job based on a condition?

I want to evaluate in my gitlab_ci pipeline if the current java project is to build with a SNAPSHOT dependency inside the maven pom.xml:

Like so: grep -q "SNAPSHOt" pom.xml && echo 1

Question: how could I integrate this into a ci pipeline so that the pipeline does not even execute if this condition is true?

gitlab_ci.yml:

image: docker:20
build:
  stage: build
  script:
    - mvn package...

Where would I add that sort of condition check, and how could I then let the full job fail?



Solution 1:[1]

before_script:
  - grep -q "SNAPSHOT" pom.xml && exit 1

Solution 2:[2]

  1. If you want the pipeline to run only based on some condition you can use rules.
  2. if you want the pipeline to run and then fail when some condition is not satisfied you can just check the condition in script and then use exit some_non_zero number.

eg:

test job:
  stage: test
  script:
    - | 
        if [ 1 == 1 ]
        then
          exit 1
        fi

Here for condition i have used 1==1.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 membersound
Solution 2