问题
In a declarative pipeline, I can specify the parameter that the pipeline expects right in the pipeline script like so:
pipeline {
parameters([
string(name: 'DEPLOY_ENV', defaultValue: 'TESTING' )
])
}
is it possible do to in a scripted pipline? I know I can do this :
BUT, IS IT POSSIBLE TO DO THIS:
node{
parameters([
string(name: 'DEPLOY_ENV', defaultValue: 'TESTING' )
])
}
回答1:
I found a solution by experimentation so want to share it:
node {
properties(
[
parameters(
[string(defaultValue: '/data', name: 'Directory'),
string(defaultValue: 'Dev', name: 'DEPLOY_ENV')]
)
]
)
....
}
回答2:
I highly recommend using the "Snippet Generator" for first time use of figuring this out. It will allow you to use a GUI that looks maybe exactly like the snapshot you shared above, to fill out the settings you want, and generate the appropriate syntax. For this question in-particular, select the sample step "properties: Set job properties" from the Snippet Generator
You can access the "Snippet Generator" from any Jenkins server by going to the /pipeline-syntax
endpoint.
For more info on the snippet generator see: https://jenkins.io/doc/book/pipeline/getting-started/#snippet-generator
回答3:
I did create it outside and it worked! Also, I'm using the boolean value and it's working like a charm on a scripted
version
// Parameters for the build
properties([
parameters([
booleanParam(name: 'DEPLOY_SHA', defaultValue: false),
])
])
podTemplate(name: ptNameVersion, label: ptNameVersion, containers: [
...
...
]
It shows the parameter

回答4:
You can use Jenkins job DSL plugin for generating jobs, pipelines, multi-branch pipelines... The DSL allows the definition of a job, and then offers a useful set of functions to configure common Jenkins items. A configure is available to give direct access to the config.xml before generating the job. With parameters block you can then easily define any type of parameters with their default values and description. Note that this plugin is also compatible with git and other version control systems.
jobDsl scriptText: ''' job('example') {
parameters {
stringParam('Parameter Name', 'Default Value' , 'Parameter Description')
}
scm {
perforceP4('p4_credentials') {
workspace {
manual('ws_name', '//depot/Tools/build/... //ws_name/build/...')
}
configure { node ->
node / workspace / spec / clobber('true')
}
}
}
}'''
References:
https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob-scm-perforceP4
https://wiki.jenkins.io/display/JENKINS/Job+DSL+Plugin
来源:https://stackoverflow.com/questions/53747772/is-it-possible-to-make-a-parameterized-scripted-pipeline-in-jenkins-by-listing-t