sumant
sumant

Reputation: 127

Failing a Jenkins job on a condition

The end goal is to search for all the files n sub-directories within a folder a on a Linux box for control m characters. If yes, do not proceed with the ANT build and fail the job. If not, proceed with the ANT build.

In a Jenkins job I write this script outline of the script:

Cd batch Grep -r "\r" > text-file \ to find ctrl m char and redirect to text file if so.

Then compare the content. If text file is empty build else fail the job.

If [-s] fail job else build.

  1. Is this a good approach of using Linux script to achieve? If not what's the better way?

  2. How to configure a Jenkins job using execute shell and to make the shell fail and the Jenkins job fail on condition ?

Thanks

Upvotes: 3

Views: 9835

Answers (2)

Muhammad Tariq
Muhammad Tariq

Reputation: 4654

For those who still find it difficult to understand how and where to write exit with non-zero value. Below is a Jenkins pipeline script with a non-zero exit.

 pipeline{
    agent any
    
    stages{
        stage('Git Checkout'){
            steps{
                git branch: "${BRANCH}",
                credentialsId: 'github-cred', 
                url: 'https://github.com/exam105/some-repo.git'
                exit 1
            }
        }
        stage('Build Project'){
            steps{

            }        
        }
    } 
}

The Build Project stage will not run.

Upvotes: 1

Jayan
Jayan

Reputation: 18459

Option 1: Exit your scripts with non zero value. Jenkins, by default, detect this as failure.

Option 2: Use Console Parser plugin for looking for error patterns.

Both can be combined with Build failure analyser for neat message to user.

Upvotes: 3

Related Questions