Kristoffer Hilmersson Kristoffer Hilmersson - 12 days ago 9
Groovy Question

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.

Thanks!

Answer

Well, sometimes stating a question makes you think from another perspective.

Hopefully someone will benefit from this. I stumbled upon a Pipeline-Plugin tutorial where they showed how you could use node, to label where script code should run. The resulting groovy file looks something like this:

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

As you can see the tutorial made me refactor the script a bit. The node('remote') part is what defines that the upcoming stuff should be run on the slave machine.

I had to make some customizations in the batch script, so that everything important was printed to stdout.

You have to let Jenkins know which node is 'remote' by going in to Manage Jenkins > Manage Nodes, choose the slave agent in mind, Configure Node and add 'remote' or whatever suits you to the labels field.