-1

I need to fail one Jenkins pipeline stage when one file contains 'errors' I do not know how to return an error from bash to Jenkins

   stage('check if file continas error and exit if true') {
        steps {
                sh "grep 'error' filetocheck.txt"
            }
        }
    }
brane
  • 585
  • 6
  • 20
  • Possible duplicate of [Is it possible to capture the stdout from the sh DSL command in the pipeline](https://stackoverflow.com/questions/36507410/is-it-possible-to-capture-the-stdout-from-the-sh-dsl-command-in-the-pipeline) – Matthew Schuchard Mar 22 '19 at 15:00

2 Answers2

4

reference Is it possible to capture the stdout from the sh DSL command in the pipeline

This worked for me,

def runShell(String command){
    def responseCode = sh returnStatus: true, script: "${command} &> tmp.txt"
    def output =  readFile(file: "tmp.txt")
    return (output != "")
}

pipeline {
    agent any
    stages {
        stage('check shellcheck') {
            steps {
                script {
                    if (runShell('grep \'error\' file_to_parse.txt')) {
                        sh "exit 1"
                    }
                }
            }
        }
    }
}
brane
  • 585
  • 6
  • 20
1

you can try using String.count(charSequence) where String could be a file or string.

def file = 'path/to/file.txt'
if ( file.count('error') > 0 )
    return stageResultMap.didB2Succeed = false
rüff0
  • 906
  • 1
  • 12
  • 26