jenkinsjenkins-agent

How can i execute Jenkins job in 2 slaves


I am having Jenkins master and two slaves named slave1 and slave2. I want to execute some shell commands on slave1 and some shell commands on slave2 in the same job. How can i do this...?


Solution

  • You need to create a multi-configuration job in jenkins as step 1. In a multi configuration job you can run the same job on two or more slaves for a single run.

    1. you need to use slaves option under configuration matrix and select slave 1 and slave 2 from the list.
    2. Now a single run of your job runs all executes shell on two slaves.
    3. Hold on this is not what you want ? now you just made the same job to execute on two slaves.
    4. Now comes your next requirement that specific shells should execute on specific slaves.

    for this you need to use a if condition and check if the job is executing on intended slave if yes execute the shell else skip execution.

    1. Now how to find if my job is executing on which slave , you can use jenkins pre-defined environment variable NODE_NAME.

      [[ $NODE_NAME == slave1 ]] && echo code executes.

    2. you can write something similar to above and get the required thing done.