Run a remote command on all Jenkins slaves via Masters's script console

后端 未结 4 1772
执念已碎
执念已碎 2021-01-02 04:29

I want to run same shell command (very simple shell commands like ls) on all the UNIX slaves which are connected to the master by using the master\'s script co

4条回答
  •  感动是毒
    2021-01-02 04:44

    The pipeline looks something like this:

    stages  {
          stage('Checkout repo') {
             steps  {
                //checkout what I need
             }
          }
          stage('Generate Jobs')  {
             steps  {
                jobDsl targets:'generate_projects.groovy',
             }
          }
          stage('Build Projects')  {
             steps  {
                build job: "build-all",
                propagate: true,
                wait: true
             }
          }
       }
    

    and then is file generate_projects.groovy, where the actually DSL generation is:

    for (agent in hudson.model.Hudson.instance.slaves) {
       if (!agent.getComputer().isOffline())  { // check that agent is not offline
          node = jenkins.model.Jenkins.instance.getNode(agent.name)  // get agent name
          agentIPs = node.computer.getChannel().call(new ListPossibleNames())
          agentIP = agentIPs[0]  // get agent IP
          
          //Create a job that will run on that specific agent
          jobName = FOLDER + '/' + agent.name     // need to create different names
          job(jobName)
          {
             label(agent.name)
             steps
             {
                 shell()
             }
          }
       }
    }
    

    Beside the above generation of the jobs, you'll need to keep a list of jobs that were generated and add all its elements in "build-all" pipeline job, that will look something like:

    parallel(
       b0: {build '' + agent.name'},
       b1: {build '' + agent.name'},
       b2: {build '' + agent.name'},
       .....
    )
    failFast: false
    

    So when you run the pipeline, a job for each agent will be created, and all new created jobs will run in parallel. I use it for updating setup scenario.

提交回复
热议问题