How to setup parameters into jenkins pipeline script - jenkins

We have a jenkins jobs that run autotests with parameters:
HOST;
EXPEIMENT;
TAKE_NEW_SCREENSHOT;
XML_NAME.
All of this parameters have default values,
see screenshot before running parametrizing job:
I need to run several jobs simultaneously with only 2 parameters: HOST and EXPERIMENT.
I created next pipeline-script:
def tasks = [:]
parameters {
string(name: 'HOST', defaultValue: 'www', description: 'host: www, dev3, etc',)
string(name: 'EXPERIMENT', defaultValue: 'withoutExperiment',)
}
tasks['Actions MyBox'] = {
build job: 'MyDocs_Actions_And_Manage_Buttons_MyBox_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_actions_buttons_mybox_tests')
]
}
tasks['DashBoard General'] = {
build job: 'DashBoard_General_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_dash_board_general_tests')
]
}
tasks['Actions InBox'] = {
build job: 'MyDocs_Actions_Buttons_InBox_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_actions_buttons_inbox_tests')
]
}
parallel tasks
and specified parameters in "General" pipeline configuration:
But when I run this pipeline item with parameter value != default value, for example specify HOST = dev12,
anyway all jobs running simultaneously with default parameter values and build shows null specified parameter,
Help me please define a problem.

You're passing hardcoded values to your tasks. For example, you defined
tasks['Actions MyBox'] = {
build job: 'MyDocs_Actions_And_Manage_Buttons_MyBox_Tests', parameters: [
string(name: 'HOST', value: 'www'),
string(name: 'EXPERIMENT', value: 'withoutExperiment'),
booleanParam(name: 'TAKE_NEW_SCREENSHOT', value: false),
string(name: 'XML_NAME', value: 'my_docs_actions_buttons_mybox_tests')
]
}
In this case all parameters are hardcoded and each time when pipeline is executed the value of HOST will be www. And that's why you have null in the HOST parameter description in build execution info (because you're not specifying it in build job command).
So, you need to use something like string(name:'HOST', value: "${params.HOST}")

Related

changing the Jenkins pipeline to use password parameter or Credentials parameter instead od Hudson password

Below is the pipeline script for my job:
pipeline {
agent any
stages {
stage('Runbook database deploy job') {
steps {
withCredentials([
usernamePassword(credentialsId: 'RUNBOOK_RDS_DBADMIN', passwordVariable: 'DB_APPTEAM_PASS', usernameVariable: 'DB_APPTEAM_USER'),
usernamePassword(credentialsId: 'nexus_download_user', passwordVariable: 'NEXUS_APPTEAM_PASS', usernameVariable: 'NEXUS_APPTEAM_USER')
]) {
build job: 'DBA-TSS/db_aurora-mysql_DEPLOY/', parameters: [
string(name: 'INSTALL_TYPE', value: "${INSTALL_TYPE}"),
string(name: 'SDLC', value: "${SDLC}"),
string(name: 'DB_ARTIFACT', value: "${DB_ARTIFACT}"),
string(name: 'DEV_NEXUS_ID', value: NEXUS_APPTEAM_USER),
[$class: 'hudson.model.PasswordParameterValue', name: 'DEV_NEXUS_PASSWORD', value:hudson.util.Secret.fromString(NEXUS_APPTEAM_PASS)],
string(name: 'DEV_NEXUS_REPO', value: "${DEV_NEXUS_REPO}"),
string(name: 'DB_USERNAME', value: DB_APPTEAM_USER),
[$class: 'hudson.model.PasswordParameterValue', name: 'DB_PASSWORD', value:hudson.util.Secret.fromString(DB_APPTEAM_PASS)],
string(name: 'REQUESTER_EMAIL', value: "${REQUESTER_EMAIL}")
]}
}
}
}
}
And now as hudson is not supported , we want to replace it with Credentials id or password parameter to pass it to downstream job for password variable. can you please help us on this.

How to pass parameter value to stage build in Jenkins pipeline?

I have a pipeline created which takes the parameter value from the user. I want to trigger a jenkin job using this parameter.
How can I pass the parameter value to the build's parameter.
Here is my code:
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: ${params.EMail}), string(name: 'SYSTEM', value: ${params.SYSTEM})]
}
}
}
}
In the above code, I am taking email and system details from the user. Then I want to trigger my job "xyz" which would require these parameter.
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: params.EMail),
string(name: 'SYSTEM', value: params.SYSTEM)]
}
}
}
}

Pass (same) parameters to multiple build jobs in a Jenkins pipeline

We have multiple jobs, 'primary', 'secondary' and 'backup' - All need to have the same parameters (release versions i.e '1.5.1') - There at around 15 of them.
parameters{
string(name: 'service1', defaultValue: 'NA', description: 'Verison' )
string(name: 'service2', defaultValue: 'NA', description: 'Verison' )
string(name: 'service3', defaultValue: 'NA', description: 'Verison' )
}
My pipeline is like the below, how can I use the same above paramaters for all 3 build jobs without having to specify the parameters three times?
//This will kick of the three pipeline scripts required to do a release in PROD
pipeline {
agent any
stages
{
stage('Invoke pipeline primary') {
steps {
build job: 'primary'
}
}
stage('Invoke pipeline secondary') {
steps {
build job: 'secondary'
}
}
stage('backup') {
steps {
build job: 'backup'
}
}
}
}
I've found this answer here, but this seems to use groovy syntax and i'm not sure if this can also be used in a declarative pipline like the above?
When I tried it, I get the below:
Running on Jenkins in PipelineTest
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Invoke pipeline primary)
[Pipeline] build
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: No item named null found
Finished: FAILURE
When I run this primary pipeline by itself, it runs as expected.
Thanks!
Edit: Tried the answer provided by #hakamairi but get the below, I'm not great at DSL but tried a few different variations and none worked / all had similar types of errors around expecting a ParamValue.
//This will kick of the three pipeline scripts required to do a release in PROD
pipeline {
agent any
parameters{
string(name: 'service1', defaultValue: 'NA', description: 'Version' )
string(name: 'service2', defaultValue: 'NA', description: 'Version' )
}
stages
{
stage('Invoke pipeline PrimaryRelease') {
steps {
build job: 'PythonBuildTest', parameters: params
}
}
}
}
Error:
java.lang.UnsupportedOperationException: must specify $class with an
implementation of interface java.util.List at
org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:503)
at
org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:402)
at
org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:361)
at
org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:284)
at
org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:201)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:208)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153)
at
org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at sun.reflect.GeneratedMethodAccessor956.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) at
groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213) at
groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022) at
org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
at
org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:133)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
at
org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
at
org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
at
com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
Caused: java.lang.IllegalArgumentException: Could not instantiate
{job=PythonBuildTest, parameters={service1=NA,
I think you can use the parameters on the pipeline level and just pass the parameters in build calls.
//This will kick of the three pipeline scripts required to do a release in PROD
pipeline {
agent any
parameters{
string(name: 'service1', defaultValue: 'NA', description: 'Verison' )
string(name: 'service2', defaultValue: 'NA', description: 'Verison' )
string(name: 'service3', defaultValue: 'NA', description: 'Verison' )
}
stages
{
stage('Invoke pipeline primary') {
steps {
build job: 'primary', parameters: ([] + params)
}
}
stage('Invoke pipeline secondary') {
steps {
build job: 'secondary', parameters: ([] + params)
}
}
stage('backup') {
steps {
build job: 'backup', parameters: ([] + params)
}
}
}
}
I mostly use scripted approach and something like the below works:
def all_params = [
string(name: 'service1', defaultValue: 'NA', description: 'Version' ),
string(name: 'service2', defaultValue: 'NA', description: 'Version' ),
string(name: 'service3', defaultValue: 'NA', description: 'Version' ),
]
properties([parameters(all_params)])
It should be possible to wrap the above code in a script block and use it in a declarative pipeline as well.

Execute only selected jobs using Jenkins pipe line

I have this jenkins pipe line which has multiple stages. Inside these stages, there are multiple jobs being executed.
When I build the job I'd like to have a set of check boxes and the pipe line should build only what I've checked inside the pipeline stages. Is there any plugins or methods I can use to achieve this?
Sample pipeline code.
As per below example, there are jobs called job_A1, job_B1, job_C1, job_D1, job_A2, job_B2, job_C2 and job_D2. If I click Build with parameters, it should prompt me check boxes and I should be able to check any job I want so that the pipe line will build only the ones I checked.
Thanks in Advance.
pipeline {
agent {label 'server01'}
stages {
stage('Build 01') {
steps {
parallel (
"BUILD A1" : {
build job: 'job_A1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD B1" : {
build job: 'job_B1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD C1" : {
build job: 'job_C1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD D1" : {
build job: 'job_D1',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
)
}
}
stage('Build 02') {
steps {
parallel (
"BUILD A2" : {
build job: 'job_A2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD B2" : {
build job: 'job_B2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD C2" : {
build job: 'job_C2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
"BUILD D2" : {
build job: 'job_D2',
parameters:[
string(name: 'PARAM01', value: "$PARAM01"),
string(name: 'PARAM02', value: "$PARAM02")
]
},
)
}
}
}
}
Thanks #mbn217 for your answer, but ExtendedChoice parameter didn't help much in my scenario.
Anyway, I could do it using boolean parameters and calling it inside the pipeline using the script tag.
Example pipeline script
stage ('BUILD A') {
steps {
script {
if (params.get('boolA',true)) {
build job: '_build_A', parameters: [string(name: 'param1', value: "$param1"),string(name: 'param2', value: "$param2")]
} else {
echo "A is not selected to build"
}
}
}
}
stage ('BUILD B') {
steps {
script {
if (params.get('boolB',true)) {
build job: '_build_B', parameters: [string(name: 'param1', value: "$param1"),string(name: 'param2', value: "$param2")]
} else {
echo "B is not selected to build"
}
}
}
}
You can use ExtendedChoiceParameter to accomplish what you want. Basically you will nee to parametrize job Names too using this jenkins plugin.
You can use a list of checkboxes as shown in the screen shot

Jenkins: How to use jenkins pipeline Multiple parameters

I have a Jenkinsfile instance using ansible-playbook to deploy webmachine.
I need to specified ansible-playbook parameters more than one at once.
I got
WorkflowScript: 25: Multiple occurrences of the parameters section
my jenkinsfile like this,
pipeline {
agent none
stages {
stage('docker-compose up') {
input {
message "Should we continue?"
ok "Yes, do it!"
parameters {
string(name: 'KIBANA_TAG', defaultValue: '', description: 'input tag for ansible command.')
}
parameters {
string(name: 'FLUENT_TAG', defaultValue: '', description: 'input tag for ansible command.')
}
parameters {
string(name: 'ES_TAG', defaultValue: '', description: 'input tag for ansible command.')
}
parameters {
string(name: 'HOST', defaultValue: '', description: 'input tag for ansible command.')
}
}
steps {
sh "rd6-admin#qa ansible-playbook /tmp/qa/docker-compose-up.yml -e fluent_tag=${params.FLUENT_TAG} -e kibana_tag=${params.KIBANA_TAG} -e es_tag=${params.ES_TAG} -e host=${params.HOST}"
}
}
}
}
what part should i fix?
The comma separator is not working in version 2.222.1. I removed the comma and it is now working.
parameters {
string(name: 'KIBANA_TAG', defaultValue: 'default', description: 'input tag for ansible command.')
string(name: 'FLUENT_TAG', defaultValue: 'default', description: 'input tag for ansible command.')
string(name: 'ES_TAG', defaultValue: 'default', description: 'input tag for ansible command.')
string(name: 'HOST', defaultValue: 'default', description: 'input tag for ansible command.')
}
parameters {
string(name: 'KIBANA_TAG', defaultValue: 'default', description: 'input tag for ansible command.')
string(name: 'FLUENT_TAG', defaultValue: 'default', description: 'input tag for ansible command.')
string(name: 'ES_TAG', defaultValue: 'default', description: 'input tag for ansible command.')
string(name: 'HOST', defaultValue: 'default', description: 'input tag for ansible command.')
}
Try this. Multiple occurrences of the parameters section means that there is only one parameters{} allowed and you have to place your parameters inside there.

Resources