0

Here is my problem simplified :

I have a main job (pipeline job) and I have x job (freestyle). In my main job I build x job using the following :

code in main job -

res = build job: 'x', parameters: [string(name: 'JOBNAME',  value: string(name: 'JIRACHEF', value: "oldvalue")], quietPeriod: 2

Now in this job x I change the value of JIRACHEF parameter and I print to check if it actually changed.:

 os.environ["JIRACHEF"] = "newvalue"
 print os.environ["JIRACHEF"]

This works in job x console output. I presume as per the solution presented, this updated value should be now available in the main job so I do the following after in main job just after building x:

res = build job: 'x', parameters: [string(name: 'JOBNAME',  value: string(name: 'JIRACHEF', value: "oldvalue")], quietPeriod: 2    
print "$res.buildVariables" 

which should print "newvalue" but prints "oldvalue" thus making me believe it isn't actually passing the value upstream.

Note - I realize my job x is freestyle, but I have tried the above solution by making x pipeline job as well and still getting the same result - 'oldvalue'

Scooby
  • 3,371
  • 8
  • 44
  • 84
  • Possible duplicate of [Pipeline jobs - pass parameters upstream?](http://stackoverflow.com/questions/41513072/pipeline-jobs-pass-parameters-upstream) – hakamairi Jan 15 '17 at 09:18

1 Answers1

1

Main job - configuration: pipeline job

node {
    x = build job: 'test1', quietPeriod: 2
    build job: 'test2', parameters: [
        string(name: 'aValue1FromX', value: "$x.buildVariables.value1fromx"), 
        string(name: 'aValue2FromX', value: "$x.buildVariables.value2fromx")
        ], quietPeriod: 2
}

test1 - configuration: pipeline job

node {
    env.value1fromx = "bull"
    env.value2fromx = "bear"
}

test2 - configuration: pipeline job, parametrized, two parameters aValue1FromX and aValue2FromX both strings

node {
    echo "$env.aValue1FromX"
    echo "$env.aValue2FromX"
}
hakamairi
  • 4,464
  • 4
  • 30
  • 53
  • - I do see that the different jobs run on different nodes. – Scooby Jan 14 '17 at 23:38
  • Did you configure the downstream job to be parametrized and gave it parameters with the names of those that you pass from upstream? – hakamairi Jan 14 '17 at 23:43
  • yes, just like in edit one. I dont parameterize the first job but I do parameterize the downstream job to that with the name that I pass from first job. Do you want me to give more details in ticket ? – Scooby Jan 14 '17 at 23:49
  • Please check Edit 2 , its simple demonstration of your example but fails. Im wondering if you've tried this and it works for you ? – Scooby Jan 15 '17 at 00:22
  • Interesting, so you tried my example as it is without any modifications and with appropriate configuration and it fails? – hakamairi Jan 15 '17 at 09:05
  • And in general yes it works. That's how I came up with it, started testing with jenkins2 and pipeline jobs. – hakamairi Jan 15 '17 at 09:58
  • Im not sure how it works for you when even a simple example (edit 3) with your configuration fails. – Scooby Jan 15 '17 at 17:01
  • I don't see any edit 3. Moreover could you just simply provide whole code for all you 3 jobs, along with their type (you wrote freestyle with groovy in the beginning, I'm insisting on pipeline with pipeline scripts included in my answer) not just some random code parts? Your actual problem is far from being clear. – hakamairi Jan 15 '17 at 17:32
  • I removed all old edits and put a new simplified one, hopefully this should make absolute sense ? – Scooby Jan 15 '17 at 18:01
  • In general in the buildVariables of a job's result there are all the values like input parameters and others set in the pipeline job too using "env.". For them to work you have to use env.VARIABLE_NAME assignment in your pipeline script. They don't have to be input parameters to the job at all, it's enough just to do for example 'env.value1fromx = "bull" ' and it will be in the result. Basing on this I think that your choice to use "os.environ" causes the failure. But again, did you actually use my example in the first place to confirm it works? – hakamairi Jan 15 '17 at 20:49
  • I did. Your example works, but in my case the main job is pipeline whereas the upstream job is a freestyle job running a python script hence I use os.environ. – Scooby Jan 16 '17 at 01:43
  • I would advise to switch to pipeline to use this mechanism. – hakamairi Jan 16 '17 at 09:35
  • Doesnt answer my question but works if I switch to pipeline job for both hence accepting it and moving on (dont want to do the write to file and read solution) – Scooby Jan 16 '17 at 15:56