如果某些文件包含特定字符串,则失败詹金斯管道阶段

时间:2019-03-22 14:37:00

标签: jenkins-pipeline

当一个文件包含“错误”时,我需要使一个詹金斯管道阶段失败 我不知道如何从bash向Jenkins返回错误

   stage('check if file continas error and exit if true') {
        steps {
                sh "grep 'error' filetocheck.txt"
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

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

这对我有用,

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"
                    }
                }
            }
        }
    }
}