Search code examples
jenkinsgroovyjenkins-pipelinevirtualboxjenkins-agent

Jenkins Pipeline: Get build output from slave agent


Background

Let's say I have two jobs, one 'Pipeline Job' and one 'Build Job'. The 'Pipeline Job' runs on master and is of course a pipeline (using groovy). Then for a build part in the pipeline I use a slave running on Windows, the 'Build Job', which is responsible for building something I can't do on the master. The master is also running on Windows but lack some software needed for the specific build.

The Question

I have a groovy script that looks something like this:

#!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'
    }
}

It works fine, where 'BuildJob' is "Restrict where this project can be run", i.e., on the slave.

My issue is that I want the output from 'BuildJob' to print in the pipeline logs. Do you have some clever ways of how this could be done? I'm open for everything, so if you know of more clever ways to start the 'BuildJob' etc. I'm eager to here it.


Solution

  • EDITED You have to approve the things you want to access under script-approval. Not sure if you really neeed getRawBuild but it worked.

    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/ you approve the following:

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