Issue
am using a Jenkin's Job DSL pipelineJob to create a new job, and I need to pass three vparameters to the new job. Here is my DSL code:
pipelineJob("cronjob/${ACTION}_${ENVRIONMENT_NAME}_environment_CRONJOB") {
parameters {
stringParam("ENVRIONMENT", "${ENVRIONMENT_NAME}")
stringParam("INSTANCE", "${INSTANCE_NAME}")
}
triggers {
scm("${SCHEDULE}")
}
definition {
cpsScm {
scm {
git {
remote {
url("<my github URL>")
credentials("my_credential_Id")
}
branch('*/develop')
}
}
scriptPath("myhome/code/single-${ACTION}")
}
}
disabled()
}
Here ACTION, ENVRIONMENT_NAME, and INSTANCE_NAME are the Active Choice Parameters of the DSL job. It creates a new job, and the parameters have the correct values from this job.
The myhome/code/single-${ACTION}:
pipeline {
agent any
}
stages {
stage('Run inflate') {
steps {
script {
if (env.ENVIRONMENT != "Select a environment" && env.INSTANCE == "Select a instance") {
echo "Now for env.... ${env.ENVIRONMENT}"
ansiblePlaybook become: true,
colorized: true,
credentialsId: 'my_credential_ID',
extras: "-e environment_name=${env.ENVIRONMENT}",
installation: 'ansible',
inventory: 'ansible/hosts',
playbook: "ansible/scripts/single-action.yml"
}
}
}
}
}
}
When I run the created job, it does not take the values assigned to the parameters, and part of the output is:
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Run inflate)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Now for env.... null
[Pipeline] echo
The env.ENVIRONMENT
is null, instead of test_1
as shown in the job. Because the parameters do not have values, the Ansible playbook failed next.
How can I make the job to pick up the parameters values?
Thanks!
Solution
Turns out there was a typo in the pipelineJob code. The correct one should be:
pipelineJob("cronjob/${ACTION}_${ENVIRONMENT_NAME}_environment_CRONJOB") {
parameters {
stringParam("ENVIRONMENT", "${ENVIRONMENT_NAME}")
stringParam("INSTANCE", "${INSTANCE_NAME}")
}
triggers {
scm("${SCHEDULE}")
}
definition {
cpsScm {
scm {
git {
remote {
url("<my github URL>")
credentials("my_credential_Id")
}
branch('*/develop')
}
}
scriptPath("myhome/code/single-${ACTION}")
}
}
disabled()
}
and it works!
Answered By - Philip Shangguan
Answer Checked By - Marie Seifert (JavaFixing Admin)