且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

管道中的控制台输出:Jenkins

更新时间:2023-12-05 13:43:28

从构建步骤返回的对象可用于查询日志,如下所示:

The object returned from a build step can be used to query the log like this:

pipeline {
    agent any

    stages {
        stage('test') {
            steps {

                echo 'Building anotherJob and getting the log'

                script {
                    def bRun = build 'anotherJob' 
                    echo 'last 100 lines of BuildB'
                    for(String line : bRun.getRawBuild().getLog(100)){
                        echo line
                    }
                }
            }
        }
    }
}

从构建步骤返回的对象是运行对象-除了从此类的外观逐行读取日志外,还有其他选择.为此,您需要禁用管道沙箱或获取这些方法的脚本批准:

The object returned from the build step is a RunWrapper class object. The getRawBuild() call is returning a Run object - there may be other options than reading the log line-by-line from the looks of this class. For this to work you need to either disable the pipeline sandbox or get script approvals for these methods:

method hudson.model.Run getLog int
method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild

如果要对许多构建都执行此操作,则有必要将一些代码放入管道共享库中以执行所需的操作或在管道中定义函数.

If you are doing this for many builds, it would be worth putting some code in a pipeline shared library to do what you need or define a function in the pipeline.