Raji
Raji

Reputation: 887

Run Two Jenkins Job in Parallel in a Pipeline with a gap of 2 minutes

I have two Jenkins jobs and I need to run them in parallel but with a gap of 1 or 2 mins. I mean, once 'Job1' starts then after a gap of 1 minute, 'Job2' should start. 'Job1' takes approximately 6-7 mins to get over. I am able to run the jobs parallely but I am not able to put a gap of a minute or two between them. Here is my code:

node('LINUX_TEST_BOX'){ 
        parallel 'Parallel_1': {
          node('LINUX_TEST_BOX') {
              stage 'P1' 
                  build 'Job1'

          }
        }, 
             'Parallel_2': {
          node('LINUX_TEST_BOX') {
              stage 'P2' 
                  build 'Job2'


          }
  }
}

The above code runs fine. How do I make Job2 to run after 2 mins, once Job1 starts? There are totally two things I am looking for.

  1. How to give a gap between these two parallel jobs? (Earlier I was helped in the sequential job with 'sleep' command, but in parallel not sure how to do)
  2. I want both the jobs to run on the same node. Right now I am using a label in the node called as 'LINUX_TEST_BOX'. This label is holding 4 machines in it and can choose any machine to run. Is there any way, I can make sure that both jobs run on the same machine? (Maybe once Job1 starts, should I read its api and use that in Job2. So I need to make Job2 be created instantly? I am not sure about this. Want to know if there is any better way.)

Thank you!

Upvotes: 0

Views: 3680

Answers (1)

Spencer Malone
Spencer Malone

Reputation: 1509

If they need to run on the same machine and don't require isolation, you might not need the multiple node calls. Parallel doesn't explicitly require that, so you can reduce it to just...

node('LINUX_TEST_BOX'){ 
        parallel 'Parallel_1': {
            stage 'P1' 
            build 'Job1'
        }, 
          'Parallel_2': {
            stage 'P2' 
            build 'Job2'
          }
  }
}

Alternatively, there is an environment variable called NODE_NAME that you can pass into the node function in the parallel sections that should work.

node('LINUX_TEST_BOX'){ 
        parallel 'Parallel_1': {
          node(env.NODE_NAME) {
              stage 'P1' 
                  build 'Job1'

          }
        }, 
             'Parallel_2': {
          node(env.NODE_NAME) {
              stage 'P2' 
                  build 'Job2'


          }
  }
}

To get a list of other env vars, go to /pipeline-syntax/globals#env. There is one to get the labels of your node as well.

Upvotes: 1

Related Questions