根据输出,使Gradle构建暂停/失败以执行失败的Exec任务

时间:2016-02-09 18:00:32

标签: jenkins gradle webpack

我们在Gradle构建定义中使用Exec任务来运行Webpack。 此任务有时会失败,但构建任务完成,我们的Jenkins服务器认为一切顺利,并发布war文件,其中包含损坏的客户端代码。

在Exec任务的输出中,您可以看到Webpack收到错误并停止。

我现在想知道是否有可能让我的Exec任务消耗输出并在退出之前对其做出反应,以便我可以使任务失败。

我玩过setStandardOutput()和standardOutput,但我似乎无法让它工作。

以下是我的任务定义

task webpack(type: Exec) {
    def mainCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'webpack.cmd' : 'webpack'

    if (project.environment == 'prod')
        commandLine mainCommand, '-p'
    else
        commandLine mainCommand
}

1 个答案:

答案 0 :(得分:0)

这对我来说似乎很好。它解析输出,如果输出包含以“error”开头的行,则任务失败。

task webpack(type: Exec) {
    def mainCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'webpack.cmd' : 'webpack'
        standardOutput = new ByteArrayOutputStream()
        if (project.environment == 'prod')
            commandLine mainCommand, '-p'
        else
            commandLine mainCommand

    doLast {
        String output = standardOutput.toString()

        if(output.readLines().any{line->line.trim().toLowerCase().startsWith("error")}) {
            throw new GradleException("Error in WebPack: \n${output}")
        } else {
            println "Great success! Output is: ${output}"
        }
    }

}
相关问题