Jenkins Password Param in Groovy - jenkins

I'm trying to add a passwordParam to my Jenkins groovy file as such.
The intention is to have the password picked up by a job at runtime
pipelineJob(jobName) {
displayName(displayString)
parameters {
choiceParam('ARTEFACT',artefactName,'Artefact to deploy?')
choiceParam('CLUSTER',cluster,'Cluster to push to')
stringParam('BRANCH','main','What branch should be used?')
passwordParam('proxyUser', 'password123', 'ProxyPassword')
}
But I get the error
ERROR: (job_delete_release.groovy, line 23) No signature of method: javaposse.jobdsl.dsl.helpers.BuildParametersContext.passwordParam() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [proxyUser, password123, ProxyPassword]
According to the documentation here it should be possible.
I'm not sure what I'm doing wrong or if I'm missing something really obvious.

It looks like you are mixing DSL and Declarative syntaxs.
What you are writing here is JobDSL, however your link is to declarative code.
In the section you are writing you can only use the parameters available to JobDSL which can be found here:
Click the three dots in the parameters on https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.jobs.MavenJob.parameters
So for the JobDSL I believe you would use nonStoredPasswordParam (note I have never used it)
https://jenkinsci.github.io/job-dsl-plugin/#path/javaposse.jobdsl.dsl.jobs.MavenJob.parameters-nonStoredPasswordParam
If you wanted your parameters in Declarative you would write them inside the pipeline section.
e.g.
pipeline {
agent any
parameters {
string(name: 'script_args', defaultValue: '--out_file --purge', description: 'Command line args to pass to script')
}

Related

jobDSL does not understand the type of parameters

While creating a Jenkins server, I use jobDSL to create jobs via pipeline.
Basically my created job ressembles this:
pipeline{
parameters{
string(name: "SAMPLE_PARAMETER")
}
stages{
stage("Does not matter here"){
//Does some work
}
}
}
When jobDSL tries to create a job out of this, I get an error saying that javaposse.jobdsl.dsl.helpers.BuildParametersContext.stringParam() can only work with (java.lang.string), (java.lang.string, java.lang.string) or (java.lang.string, java.lang.string, java.lang.string) and not with java.util.ArrayList
Can I force the type of "SAMPLE_PARAMETER", and if so how?
If this is not possible, how can I work around this?
You are already passing type of variable. But if you still want to pass datatype,
you can simply use it like this.
string SAMPLE_PARAMETER1 = params.SAMPLE_PARAMETER
or
def SAMPLE_PARAMETER1 = params.SAMPLE_PARAMETER
SAMPLE_PARAMETER1 = SAMPLE_PARAMETER1.toString()
As suggested by Matt Schuchard in the comment he posted, I was missing the agent section, adding
agent any
fixed my issue. I have no clue how it's related but it fixed it

How to fix Jenkins String Interpolation with downstream job?

I have a parent pipeline job that takes parameters and passes them to a downstream job. I've achieves this in multiple ways with no issue however I keep getting a string interpolation warning that I am trying to fix, but am unable to do so. Based on the documentation (https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation) in most cases using single quotes should work, however this passes the literal name rather than the value (e.g if I set my variable as SECRET_PWD and call it like '${SECRET_PWD}' it shows up as ${SECRET_PWD} on the downstream job instead of the value passed to the parameter.
Here's what I've tried to so far:
Parent Pipeline
pipeline {
agent any
parameters {
password(defaultValue: "", description: 'The admin password', name: 'SUPER_SECRET_ADMIN_PWD')
stages {
stage("Stage1") {
steps {
build job: "secret_job/${env.BRANCH}", propagate: true, wait: true, parameters: [
[$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD', value: "${params.SUPER_SECRET_ADMIN_PWD}" ]
]
}
}
This gives the following error in when calling the downstream job.
Warning: A secret was passed to "build" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [SUPER_SECRET_ADMIN_PWD]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
The parameter 'SUPER_SECRET_ADMIN_PWD' did not have the type expected by secret_job ยป secret_branch. Converting to Password Parameter.
Note: I am aware that the StringParameterValue is the reason for the first error. I have changed this in a few different ways to solve that but i still get the interpolation issue.
The other ways I've tried are:
password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This works but still interpolation issue
password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away
[$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD', value: '${params.SUPER_SECRET_ADMIN_PWD}' = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away
I've also used ${env.SUPER_SECRET_ADMIN_PWD} similar to ${params.SUPER_SECRET_ADMIN_PWD}
Note that I've changed my downstream job to use single quotes and i'm doing a simple sh script something like below with no interpolation errors (I have them before though).
stages{
stage("test"){
steps{
script{
sh '''
echo ${SUPER_SECRET_ADMIN_PWD}
'''
}
}
}
How do I go about solving interpolation and still passing the password parameter down to a downstream job?

Multiple Jenkins parameterized build value pass value to Jenkinsfile for declarative pipeline

Hi my Jenkins project is parameterised build. I have 3 variables. 1 choice and 2 string parameter. The choise perameter is do_you_want_to_deploy and string parameter is git_tag and git_branch. I want to know how can i pass this value to a jenkinsfile?
In freestyle project, I selecft 'Extra Variables' and then got Key and Value. So key i put deploy_location, value is ${do_you_want_to_deplo}. Key is which_tag, value is ${git_tag}. Key is which_ranch, value is ${git_branch}. I am performing for ansible. How can i add verbos -vvv as well? This for pipelin project. Below is my code
ansiblePlaybook(
vaultCredentialsId: 'VaultId',
inventory: 'host-inventory.yml',
playbook: 'myPlaybook.yml'
)
``
I also need pass same value to downstream project. How can this be done?
Hi my Jenkins project is parameterised build. I have 3 variables. 1
choice and 2 string parameter. The choise perameter is
do_you_want_to_deploy and string parameter is git_tag and git_branch.
I want to know how can i pass this value to a jenkinsfile?
In Jenkinsfile there is parameters block to define variables. As per your use case, parameters definition may look like below. Here, by choice in your explanation I was assuming you need a toggle but if you need a list of items then use choice parameter type.
pipeline {
...
parameters {
booleanParam(name: 'do_you_want_to_deploy', defaultValue: false, description: 'Description of do_you_want_to_deploy')
string(name: 'git_tag', defaultValue: '', description: 'Description of git_tag')
string(name: 'git_branch', defaultValue: '', description: 'Description of git_branch')
}
stages {
stage('Example') {
steps {
ansiblePlaybook(
...
)
}
}
}
}
In freestyle project, I selecft 'Extra Variables' and then got Key and
Value. So key i put deploy_location, value is ${do_you_want_to_deplo}.
Key is which_tag, value is ${git_tag}. Key is which_ranch, value is
${git_branch}. I am performing for ansible. How can i add verbos -vvv
as well?
Ansible plugin has an option extraVars that can be used to pass number of variables from the pipeline. There is another option named extras that takes a string and can be used to pass additional variables, switches etc.
Together, ansiblePlaybook may look like below,
ansiblePlaybook (
vaultCredentialsId: 'VaultId',
inventory: 'host-inventory.yml',
playbook: 'myPlaybook.yml',
extras: '-vvv',
extraVars: [
deploy_location: params.do_you_want_to_deploy,
which_tag: params.git_tag,
which_branch: params.git_branch
]
)
I also need pass same value to downstream project. How can this be
done?
As you can see from the example of ansiblePlaybook above, the parameters can be accessed via params object.

Jenkins Job DSL: Using parameters in groovyScript in job step

For my build job "generated-job-1" I need several parameters, which are passed in when the build (of the generated-job-1) is triggered via URL.
Here my Job Definition with parameters inside the SeedJob DSL:
job('generated-job-1'){
label ('master')
parameters{
stringParam('DEPLOY_URI', 'https://192.168.200.176/hyperManager', 'Provide the URL where DeploymentManager can be accessed.')
stringParam('REG_ID', '12', 'The id of the owner (Registration) of this deployment.')
}
steps {
groovyCommand(readFileFromWorkspace('stepscript.groovy')){
prop('name', 'value')
prop('DEPLOY_URI', $DEPLOY_URI)
}
}
}
I tried to use DEPLOY_URI, $DEPLOY_URI and ${DEPLOY_URI} and it the build fails with different error messages like
No such property: DEPLOY_URI for class: javaposse.jobdsl.dsl.helpers.step.GroovyContext
or
ERROR: (script, line 12) No such property: $DEPLOY_URI for class: javaposse.jobdsl.dsl.helpers.step.GroovyContext
or
ERROR: (script, line 12) No signature of method: javaposse.jobdsl.dsl.helpers.step.GroovyContext.$() is applicable for argument types: (script$_run_closure1$_closure3$_closure4$_closure5) values: [script$_run_closure1$_closure3$_closure4$_closure5#1a11cf0]
How can I define and pass those parameters to my step-script.groovy?
How could I use those parameters in other steps, such as shell or batchFile?
How do I access those parameters in my step-script.groovy, to work with the given data?
I searched for a while now and tried hard to get it working... No success.
Help really appreciated, as I am new to Job DSL and to Groovy.
Thanks in advance,
Anne
You need to put the variable name in quotes so that it gets evaluated when the generated job is executed, not when the DSL script runs.
job('generated-job-1') {
parameters {
stringParam('DEPLOY_URI', '...', '...')
}
steps {
groovyCommand(readFileFromWorkspace('stepscript.groovy')) {
prop('DEPLOY_URI', '$DEPLOY_URI')
}
}
}

How can I parameterize Jenkinsfile jobs

I have Jenkins Pipeline jobs, where the only difference between the jobs is a parameter, a single "name" value, I could even use the multibranch job name (though not what it's passing as JOB_NAME which is the BRANCH name, sadly none of the envs look suitable without parsing). It would be great if I could set this outiside of the Jenkinsfile, since then I could reuse the same jenkinsfile for all the various jobs.
Add this to your Jenkinsfile:
properties([
parameters([
string(name: 'myParam', defaultValue: '')
])
])
Then, once the build has run once, you will see the "build with parameters" button on the job UI.
There you can input the parameter value you want.
In the pipeline script you can reference it with params.myParam
Basically you need to create a jenkins shared library example name myCoolLib and have a full declarative pipeline in one file under vars, let say you call the file myFancyPipeline.groovy.
Wanted to write my examples but actually I see the docs are quite nice, so I'll copy from there. First the myFancyPipeline.groovy
def call(int buildNumber) {
if (buildNumber % 2 == 0) {
pipeline {
agent any
stages {
stage('Even Stage') {
steps {
echo "The build number is even"
}
}
}
}
} else {
pipeline {
agent any
stages {
stage('Odd Stage') {
steps {
echo "The build number is odd"
}
}
}
}
}
}
and then aJenkinsfile that uses it (now has 2 lines)
#Library('myCoolLib') _
evenOrOdd(currentBuild.getNumber())
Obviously parameter here is of type int, but it can be any number of parameters of any type.
I use this approach and have one of the groovy scripts that has 3 parameters (2 Strings and an int) and have 15-20 Jenkinsfiles that use that script via shared library and it's perfect. Motivation is of course one of the most basic rules in any programming (not a quote but goes something like): If you have "same code" at 2 different places, something is not right.
There is an option This project is parameterized in your pipeline job configuration. Write variable name and a default value if you wish. In pipeline access this variable with env.variable_name

Resources