Pipeline help please!

60 views
Skip to first unread message

Sam K

unread,
Sep 30, 2016, 2:54:32 PM9/30/16
to Jenkins Users
I have 4 sets of IP addresses separated by commas.  It'm converting the string to a list and using the parallel block to execute them in parallel.  

Problem: I am unable to get the IP addresses within the parallel blocks. As you can see the branch name is interpreted properly.  However, within the stage block, its not getting the value. 

What is the variable I'm missing?  Thanks!

Pipeline code: 

def PAIRS = '1.1.1.1 2.2.2.2, 3.3.3.3 4.4.4.4, 5.5.5.5 6.6.6.6, 7.7.7.7 8.8.8.8'
   
   node ('jenkins-slave-1') {
     def s = PAIRS.tokenize(',')
     echo "Got the IP Addresses" + s

     def branches = [:]

     for (ip in s) {

        echo "For Loop: " + ip 

        branches["${ip}"] = {

           stage ('pair') {
              echo "Inside stage got this IP: " + ip
              //echo "Inside stage:" + "${ip}"
              sleep 5
        }
      }

   }
   parallel branches    
   }    

Output:
[1.1.1.1 2.2.2.2] Inside stage got this IP:  7.7.7.7 8.8.8.8
[Pipeline] [1.1.1.1 2.2.2.2] sleep
[1.1.1.1 2.2.2.2] Sleeping for 5 sec
[Pipeline] [ 3.3.3.3 4.4.4.4] echo
[ 3.3.3.3 4.4.4.4] Inside stage got this IP:  7.7.7.7 8.8.8.8
[Pipeline] [ 3.3.3.3 4.4.4.4] sleep
[ 3.3.3.3 4.4.4.4] Sleeping for 5 sec
[Pipeline] [ 5.5.5.5 6.6.6.6] echo
[ 5.5.5.5 6.6.6.6] Inside stage got this IP:  7.7.7.7 8.8.8.8
[Pipeline] [ 5.5.5.5 6.6.6.6] sleep
[ 5.5.5.5 6.6.6.6] Sleeping for 5 sec
[Pipeline] [ 7.7.7.7 8.8.8.8] echo
[ 7.7.7.7 8.8.8.8] Inside stage got this IP:  7.7.7.7 8.8.8.8
[Pipeline] [ 7.7.7.7 8.8.8.8] sleep
[ 7.7.7.7 8.8.8.8] Sleeping for 5 sec

Sam K

unread,
Oct 1, 2016, 1:26:40 AM10/1/16
to Jenkins Users
Anyone?  

Jason Swager

unread,
Oct 1, 2016, 1:44:25 AM10/1/16
to Jenkins Users

Sam K

unread,
Oct 2, 2016, 1:10:26 AM10/2/16
to Jenkins Users
Thanks Jason.  I guess I'll have to figure something else out to get those values inside the loops.  :) 

Sam K

unread,
Oct 4, 2016, 1:53:32 PM10/4/16
to Jenkins Users
So looks like parallel is for running multiple hard-coded jobs in parallel.  No way to supply arguments to the jobs?  I even tried writing the values to a temp file and trying to read from it.  But looks like the for loop gets executed 4 times first with the last value in the temp file and the parallel block kicks in and reads the temp file all at once?  


On Friday, September 30, 2016 at 11:54:32 AM UTC-7, Sam K wrote:

corneil....@gmail.com

unread,
Oct 4, 2016, 2:32:33 PM10/4/16
to Jenkins Users

Look at https://jenkins.io/doc/pipeline/examples/

Assign ip at start of block.

branches ["$ip"] = {
    def ipAddress = ip
    // use ipAddress instead of ip


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3408003e-6528-450a-88e0-75746333337b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam K

unread,
Oct 4, 2016, 4:27:53 PM10/4/16
to Jenkins Users
Thanks Corneil, but I had already tried that and it did not work.  But your link sure helped and its now working as expected.  Let me post the working solution as a separate reply.  :) :) 

Sam K

unread,
Oct 4, 2016, 4:32:48 PM10/4/16
to Jenkins Users
Thanks everyone!  I got it working exactly as I wanted.  For any others struggling with the same, here is the solution.  Hope it helps someone!  :)

Pipeline job will take parameters like this:











The pipeline code:

def ippair = IPADDRESSES.tokenize(",")

// The map we'll store the parallel steps in before executing them.
def stepsForParallel = [:]

for (int i = 0; i < ippair.size(); i++) {
    // Get the actual string here.
    def s = ippair.get(i)
    def stepName = "echoing ${s}"
    
    stepsForParallel[stepName] = parallelstep(s)
}

parallel stepsForParallel

def parallelstep(inputString) {
    return {
        node {
            stage('Pair:' + inputString) {
                echo "Working on the pair:"  + inputString
                ip = inputString.tokenize()
                echo "The first IP is: " + ip[0]
                echo "The second IP is: " + ip[1]
            } //stage block
        } // node block
    } //return block
    
}

End output is a list of stages which is variable based on how many pairs of IP's were supplied as parameters.  Which is one I wanted exactly. 


Reply all
Reply to author
Forward
0 new messages