How to fail a gitlab CI pipeline if the python script throws error code 1?

PforPython

I have a python file that opens and checks for a word. The program returns 0 if pass and 1 if fails.

import sys

word = "test"

def check():
    with open("tex.txt", "r") as file:
        for line_number, line in enumerate(file, start=1):  
            if word in line:
                return 0
            
        return 1

is_word_found = check() # store the return value of check() in variable `is_word_found`
print(is_word_found)
output

1

I have gitlab-ci.yml that runs this python script in a pipeline.

image: davidlor/python-git-app:latest

stages:
    - Test
Test_stage:
   tags:
        - docker
   stage: Test
   script:
        - echo "test stage started"
        - python verify.py

When this pipeline runs the python code prints 1 that means the program failed to find the test word. But the pipeline passes successfully.

I want to fail the pipeline if the python prints 1. Can somebody help me here?

Prabhu Vignesh Rajagopal

You can use sys.exit(is_word_found). But remember you use sys module (import sys).

Like this:

import sys
word = "test"
def check():
    with open("tex.txt", "r") as file:
        for line_number, line in enumerate(file, start=1):  
            if word in line:
                return 0
        return 1
is_word_found = check() 
sys.exit(is_word_found)

However, you have a lot of other options too. check out this: https://www.geeksforgeeks.org/python-exit-commands-quit-exit-sys-exit-and-os-_exit/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Deploying React app on Debian 11 with serve throws unexplainable error in the CI/CD Pipeline on gitlab

Gitlab CI fails with ERROR: Job failed: exit code 1

Laravel Gitlab CI/CD, ERROR: Job failed: exit code 1

Prevent commit when gitlab-CI pipeline fail

Error while Running CI Pipeline on Gitlab

How to fail a build on Gitlab CI shell runner

GitLab CI: How to fail on new compiler warnings

.gitlab-ci.yaml throws "Cleaning up file based variables 00:01 ERROR: Job failed: exit code 1" at the end after successfully run the job

How to pass env variables from python script to gitlab ci with requests?

gitlab CI build script syntax error

Gitlab CI Pipeline job

Gitlab CI Child pipeline

Circle ci build doesnt fail when node scipt throws error

Using shell variables within a Gitlab CI/CD pipeline script?

GITLAB CI Error loading key "/dev/fd/63": invalid format ERROR: Job failed: exit code 1

Getting error in GitLab CI pipeline even after installing package successfully

GitLab CI - pg_dump error in pipeline stage

GITLAB CI/CD: How to know if a pipeline is a tag pipeline, an MR pipeline or a scheduled pipeline?

How to manually add "script" in an Azure CI Pipeline?

How can I communicate a parameter value from a gitlab CI/CD pipeline via Terraform to a user_data script in AWS?

Jestjs tests fail in Gitlab CI

Concourse Pipeline: How to have an Embedded Script Fail the Pipeline

How do I get my Azure DevOps Pipeline build to fail when my linting script returns an error?

How to execute different script in same GitLab CI stage based on code changes

How to eliminate code repitition in a ".gitlab-ci.yml"-script that uses stages that are almost identical?

Fail Gitlab pipeline based on output

Gitlab CI/CD error Machine Learning Python

How to get the merge request information in a gitlab-ci pipeline?

How do I push to a repo from within a gitlab CI pipeline?