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

后端 未结 4 1773
执念已碎
执念已碎 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 05:00

    Until the end, I don't use * to search the agents, but instead I'm reading and parsing their names. For example, if I want to run a job on every agent that has LINUX in name, I will do next:

    for (aSlave in hudson.model.Hudson.instance.slaves)
    {
       /* take into account just agents with LINUX in name*/
       AGENT_NAME = aSlave.name
       if ( AGENT_NAME.contains('LINUX') )
       {
          /* you can check also if the agent is online or other attributes */
    
          /* Add agent name as label of the agent */ 
          AGENT_LABELS = aSlave.getLabelString() + " " + AGENT_NAME
          aSlave.setLabelString(AGENT_LABELS)
    
    
          /* For each found agent, create a job that will run on it */
          job('My_job_name_' + AGENT_NAME)
          {
              label(AGENT_NAME)
              steps {
                   /* Do whatever you want here. 
                      This job will run just on this specific agent (due to label set) */
               }
          } 
       } /* end if */
    } /* end for */  
    
    /* If you want to run all jobs in parallel (every job on a specific agent), you can save all found agents in a list and then create one more pipeline job that will contain next line :
    
       ' parallel {
          b0: {build 'My_job_name_' + AGENT_NAME_LIST[0]},
          b1: {build 'My_job_name_' + AGENT_NAME_LIST[1]},
          ....
        }
        fastfail: false '
    
    
    

提交回复
热议问题