I found a solution.
I put "input" in "script".
value2, value3, value4 and value5 are "predicted value".
It's fine for me.
---
env['value1'] = 'initial value'
pipeline {
agent none
stages {
stage('prediction') {
agent {
label 'master'
}
steps {
script {
env['value1'] = sh(script: 'echo predicted value',
returnStdout: true).trim()
}
}
}
stage('execution') {
steps {
script {
def values = input(
message: 'setting',
parameters: [
string(name: 'value2', defaultValue: env['value1']),
string(name: 'value3', defaultValue: env.value1)
]
)
build(job: 'job', parameters: [
string(name: 'value4', value: values.value2),
string(name: 'value5', value: values.value3)
])
}
}
}
}
}
---
--
Kiyoshi Ohgishi
2020-12-06 17:41 GMT+09:00, Kiyoshi Ohgishi <[email protected]>:
> Thank you for your advice.
> I tried the following script.
>
> ----
> env['value1'] = 'initial value'
> pipeline {
> agent none
> stages {
> stage('prediction') {
> agent {
> label 'master'
> }
> steps {
> script {
> env['value1'] = sh(script: 'echo predicted value',
> returnStdout: true).trim()
> }
> }
> }
> stage('execution') {
> input {
> message 'setting'
> parameters {
> string(name: 'value2', defaultValue: env['value1'])
> string(name: 'value3', defaultValue: env.value1)
> }
> }
> steps {
> build(job: 'job', parameters: [string(name: 'value4',
> value: env.value1), string(name: 'value5', value: env['value1'])])
> }
> }
> }
> }
> ----
>
> Both 'value2' and 'value3' prompted were 'initial value'.
> Both parameters 'value4' and 'value5' were 'predicted value'.
>
> ' defaultValue' seems to be evaluated only when loading.
>
> 2020年11月30日月曜日 1:56:10 UTC+9 [email protected]:
>
>> I don't understand why your example do not work, but I was recently
>> confronted to this bug : https://issues.jenkins.io/browse/JENKINS-42360
>> This changed my way of thinking : stop use groovy var, use environment,
>> everywhere, always.
>>
>> Maybe you could try to set your variable as env var : env['VALUE_NAME'] =
>>
>> 'value'
>> and use it with env.VALUE_NAME.
>>
>> I think that using env var is a more "standard" way of doing,
>> especially in declarative pipelines.
>> I even use a "setEnv" function in /vars to ease the process without the
>> need of script closure.
>>
>> Hope this helps
>>
>>
>> On Sun, Nov 29, 2020 at 3:39 PM Kiyoshi Ohgishi <[email protected]>
>> wrote:
>>
>>> Thank you for your advice.
>>> I tried the following script.
>>>
>>> import groovy.transform.Field
>>> @Field def value1 = 'initial value'
>>>
>>> But the prompted value is "initial value" , which is the same value as
>>> before.
>>>
>>> 2020年11月26日木曜日 23:05:50 UTC+9 [email protected]:
>>>
>>>> You can try with environment variables or the Field annotation as shown
>>>>
>>>> below
>>>>
>>>> import groovy.transform.Field
>>>> @Field def value1 = 'initial value'
>>>>
>>>>
>>>> On Wednesday, 25 November 2020 at 19:00:24 UTC Kiyoshi Ohgishi wrote:
>>>>
>>>>> I want to change from scripted pipeline script to declarative pipeline
>>>>>
>>>>> script.
>>>>> But I have one problem defaultValue of input cannot change value in
>>>>> case of declarative pipeline script.
>>>>>
>>>>> Below is the scripted pipeline script I want to change from.
>>>>> ----
>>>>> def value1 = 'initial value'
>>>>> node {
>>>>> label 'master'
>>>>> stage('prediction') {
>>>>> value1 = sh(script: 'echo predicted value', returnStdout:
>>>>> true).trim()
>>>>> }
>>>>> }
>>>>> def setting
>>>>> stage('setting') {
>>>>> setting = input(parameters: [string(name: 'value2', defaultValue:
>>>>> value1)])
>>>>> }
>>>>> stage('execution') {
>>>>> build(job: 'job', parameters: [string(name: 'value2', value:
>>>>> setting)])
>>>>> }
>>>>> ----
>>>>> The prompted value in input step is "predicted value" as I exected.
>>>>>
>>>>> Below is the failing declarative pipeline script I tried.
>>>>> ----
>>>>> def value1 = 'initial value'
>>>>> pipeline {
>>>>> agent none
>>>>> stages {
>>>>> stage('prediction') {
>>>>> agent {
>>>>> label 'master'
>>>>> }
>>>>> steps {
>>>>> script {
>>>>> value1 = sh(script: 'echo predicted value',
>>>>> returnStdout: true).trim()
>>>>> }
>>>>> }
>>>>> }
>>>>> stage('execution') {
>>>>> input {
>>>>> message 'setting'
>>>>> parameters {
>>>>> string(name: 'value2', defaultValue: value1)
>>>>> }
>>>>> }
>>>>> steps {
>>>>> build(job: 'job', parameters: [string(name: 'value2',
>>>>> value: value2)])
>>>>> }
>>>>> }
>>>>> }
>>>>> }
>>>>> ----
>>>>> The prompted value is "initial value" as I don't expect.
>>>>>
>>>>> What I want to make with a script is the following three steps.
>>>>> 1. get "predicted value" using shell script in the git repository.
>>>>> 2. prompt "predicted value" and decide value.
>>>>> 3. run the job with decided value.
>>>>>
>>>>> How to change defaultValue of input in case of declarative pipeline
>>>>> script?
>>>>>
>>>>> Kiyoshi Ohgishi
>>>>>
>>>> --
>>> 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 [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/jenkinsci-users/58c0b7e4-4083-40b2-940a-ff32b8b11c53n%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/jenkinsci-users/58c0b7e4-4083-40b2-940a-ff32b8b11c53n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> --
>> Yannick LACAUTE
>> Consultant indépendant
>> 39 rue de Wattignies, 75012 Paris
>> Tel : 06 16 85 95 76
>>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-users/90f1d477-e049-41c1-92e0-218915b02372n%40googlegroups.com.
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/CA%2BfPgL9ToGeNfHaqmQWK0zeVKeosoZPK%2BZyv1ug3AXHQeumuZg%40mail.gmail.com.