Pythomania
Pythomania

Reputation: 658

Jenkins - Running a single job in master as well as slave

I have a master (linux) and a windows slave set up, and would like to build a single job both on the master and the slave. The "Restrict where this project can be run" option allows us to bind the job to a particular slave but is it possible to bind one job to the master as well as slave? How would one configure the "Build Step" since running it on Windows requires a build with Windows batch command and Linux requires shell command. For example even if the job tries to run on master and slave, wouldn't it fail at one point since both the build options (with batch and shell command) will be executed?

Upvotes: 13

Views: 37703

Answers (2)

Nikolai Ehrhardt
Nikolai Ehrhardt

Reputation: 726

This is how you should do it:

import groovy.json.JsonSlurperClassic

def requestNodes() {
    def response = httpRequest url: '<your-master-url>/computer/api/json', authentication: '<configured-authentication>'
    println("Status: "+response.status)
    return new JsonSlurperClassic().parseText(response.content)
}

def Executor(node_name) {
    return {
        stage("Clean ${node_name}") {
            node(node_name) {
                //agent {node node_name}
                echo "ON NODE: ${node_name}."
            }
        }
    }
}

def makeAgentMaintainer() {
    def nodes = requestNodes()
    def agent_list = []
    for (e in nodes['computer']) {
        echo e.displayName
        if (!e.offline) {
            if (e.displayName != "master") {
                agent_list.add(e.displayName)
            }
        }
    }
    def CleanAgentsMap = agent_list.collectEntries {
        ["${it}" : Executor(it)]
    }
    return CleanAgentsMap
}

node {
    parallel makeAgentMaintainer()
}

You will need http_request plugin and do some approvals. In the Executor function you can define the commands, that you want to execute on every Agent.

Upvotes: 0

C&#233;dric Julien
C&#233;dric Julien

Reputation: 80761

Well, in Jenkins you can create groups of machine (either master or slaves), to do this :

  • click on the machine name on the first page of jenkins
  • enter in the node configuration menu
  • then, you can enter some labels in the Labels field. Let's add a mutli_platform label for example
  • go back to the first page of Jenkins
  • do it for each machine on which you need to run the job
  • go back to the first page of Jenkins
  • click on the job you want to run on multiple nodes
  • go in the configuration menu
  • check the Restrict where this project can be run and put the mutli_platform in it.

Then, your build will be able to run on the mutli_platform label.

For the second part, the multi-platform script, you can use ant builds, or python builds (with the python plugin).

EDIT: If you need to build on the 2 (or more) platforms, you should use a Matrix Job. You will be able to create a job and force it to run on every slave you need.

Upvotes: 21

Related Questions