2

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!

hilms
  • 53
  • 1
  • 6

2 Answers2

2

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
Community
  • 1
  • 1
MaTePe
  • 936
  • 1
  • 6
  • 11
  • unfortunatly I get "*org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper log*" when trying your solution – hilms Nov 28 '16 at 10:49
  • Whitelisted methods: https://github.com/jenkinsci/workflow-support-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java – MaTePe Nov 28 '16 at 15:31
  • yeah okey, that might work, but I have no time to revert and test it now. Thank you for your time!! – hilms Nov 29 '16 at 09:08
1

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.

hilms
  • 53
  • 1
  • 6
  • Can't you do something like this? http://stackoverflow.com/questions/36188512/search-through-console-output-of-a-jenkins-job So you first code and then just "println slaveJob.log"? – MaTePe Nov 28 '16 at 08:47