How to change defaultValue of input in case of declarative pipeline script?

298 views
Skip to first unread message

Kiyoshi Ohgishi

unread,
Nov 25, 2020, 2:00:24 PM11/25/20
to Jenkins Users
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

Victor Martinez

unread,
Nov 26, 2020, 9:05:50 AM11/26/20
to Jenkins Users
You can try with environment variables or the Field annotation as shown below

import groovy.transform.Field
@Field def value1 = 'initial value'


Kiyoshi Ohgishi

unread,
Nov 29, 2020, 9:38:45 AM11/29/20
to Jenkins Users
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 victormar...@gmail.com:
Message has been deleted

Kiyoshi Ohgishi

unread,
Dec 6, 2020, 3:41:59 AM12/6/20
to Jenkins Users
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 yannick...@gmail.com:
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

--
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/58c0b7e4-4083-40b2-940a-ff32b8b11c53n%40googlegroups.com.


--
Yannick LACAUTE
Consultant indépendant
39 rue de Wattignies, 75012 Paris
Tel : 06 16 85 95 76

Kiyoshi Ohgishi

unread,
Mar 7, 2021, 11:43:34 AM3/7/21
to Jenkins Users
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
>>> <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 jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-users/90f1d477-e049-41c1-92e0-218915b02372n%40googlegroups.com.
>
Reply all
Reply to author
Forward
0 new messages