'Jenkins shell script returned exit code 1 when the searched string isn't exsist

I'm tring the following at Jenkins in aim to search strings of failures in jobs.
This will run on daily basis.


def sd = "2020" + "${env.START_DATE}" + "0000"
def ed = "2020" + "${env.END_DATE}" + "2359.59"

pipeline {
    agent {label "master"}
    stages {
        stage('Build') {
            steps {

                print(sd)
                sh 'echo "Hello World"'
                sh """
                    pwd
                    #rm end-time start-time
                    #rm $WORKSPACE/$PARSING_OUTPUT
                    touch -t $sd $WORKSPACE/start-time
                    touch -t $ed $WORKSPACE/end-time
                    find /var/lib/jenkins/jobs/. -type f -newer $WORKSPACE/start-time  ! -newer $WORKSPACE/end-time -name '*' -exec grep $SEARCH_STRING /dev/null {} + >> $WORKSPACE/$PARSING_OUTPUT
                    ls -ltr
                """
            }
        post {
                always {
                    echo "sending mail"
                    //mail to: '[email protected]',
                    //subject: "Parse Jenkins log",
                    //body: "TBD"
                    //body: "${env.BUILD_URL} has result ${currentBuild.result}"
                }
            }
        }
    }
}

My problem is that if the string I'm looking for isn't exsist. the job fail..
Console output display ERROR: script returned exit code 1.
I tried adding #!/bin/sh that will allow me to execute with no option - didn't help.
any suggestions ?



Solution 1:[1]

There are multiple way to achive above

  1. use set +e # Disable exit on non-zero

     sh ''''
     set +e
     ..
     '''
    
  2. use OR || with cmd

     sh''''
     $CMD || echo "string doesn't exist."
     '''
    
  3. use like below

     sh (
       script: 'YOUR SCRIPT',
       returnStatus: true
     )
    

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 BuZZ-dEE