Jenkins 管道:从从属代理获取构建输出

Jenkins Pipeline: Get build output from slave agent

背景

假设我有两份工作,一份 'Pipeline Job' 和一份 'Build Job'。 'Pipeline Job' 在 master 上运行,当然是一个管道(使用 groovy)。然后,对于管道中的构建部分,我在 Windows、'Build Job' 上使用从 运行,它负责构建我无法在主服务器上执行的操作。大师也在 Windows 上 运行,但缺少特定构建所需的一些软件。

问题

我有一个 groovy 脚本,看起来像这样:

#!groovy
node {
    stage('Environment Preparation') {
        // fetches stuff and sets up the environment on master
    }
    stage('Unit Testing') {
        // some testing
    }
    stage('Build on Slave') {
        def slaveJob = build job: 'BuildJob'
    }
}

它工作正常,其中 'BuildJob' 是 "Restrict where this project can be run",即在从站上。

我的问题是我希望 'BuildJob' 的输出打印在管道日志中。你有什么聪明的方法可以做到这一点吗?我对一切都持开放态度,所以如果你知道更聪明的方法来启动 'BuildJob' 等。我很想在这里。

谢谢!

嗯,有时候提出问题会让你从另一个角度思考。

希望有人能从中受益。 我偶然发现了一个 Pipeline-Plugin tutorial,在那里他们展示了如何使用节点来标记脚本代码应该在哪里 运行。生成的 groovy 文件看起来像这样:

#!groovy
stage('Environment Preparation') {
    node('master') {
        // fetches stuff and sets up the environment on master
    }
}
stage('Unit Testing') {
    node('master') {
        // some testing
    }
}
stage('Build on Slave') {
    node('remote') {
        def out = bat script: 'C:\Build\build.bat', returnStdout: true
    }
}

如您所见,教程让我稍微重构了脚本。 node('remote') 部分定义即将到来的东西应该在从机上 运行 。

我必须在批处理脚本中进行一些自定义,以便将所有重要内容打印到标准输出。

您必须让 Jenkins 知道哪个节点是 'remote',方法是进入 管理 Jenkins > 管理节点,选择要记住的从代理,配置 Node 并在 labels 字段中添加 'remote' 或任何适合您的内容。

已编辑 您必须在脚本批准下批准您想要访问的内容。不确定您是否真的需要 getRawBuild 但它有效。

Search through console output of a Jenkins job

#!groovy
node {
    stage('Environment Preparation') {
        // fetches stuff and sets up the environment on master
    }
    stage('Unit Testing') {
        // some testing
    }
    stage('Build on Slave') {
        def slaveJob = build job: 'BuildJob'
        println slaveJob.rawBuild.log
    }
}

jenkinsurl/scriptApproval/ 您同意以下内容:

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