How to trim parameters in Jenkins pipeline? - jenkins

I am setting up a parameterized Jenkins job with pipeline. I need to trim trim parameters any suggestions?
In New version of Jenkins i can able to get the parameters. But I can't modify the parameters. If i use setValue(), it gives me unsupportedException.

parameters {
string(defaultValue: "", description: '', name: 'tag or commit', trim: true)
}

Which type of parameter are You using? Is it user input or some kind of choice list? As workaround You can use another variable:
myVar = param1.trim()
then You can use myVar instead o param1

Related

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?

Choice parameter having strings having space

I am trying to write a groovy script for a job to have a choice parameter. One of the choices would a string with space in between ( i.e 'test env' ). Is there any way we can achieve that ?
Current code
parameters {
choice(name: 'environment_name', description: 'The environment name',choices: 'test\ntest env')
}
Is there any way we can achieve this in groovy ?
Just use a list for choices:
parameters {
choice(name: 'environment_name',
description: 'The environment name',
choices: ['test','test env'])
}

How do I reassign parameters in a Jenkins pipeline script?

I'm new to Jenkins and groovy scripting. I'm trying to reassign the parameters in the Jenkins script.
I tried the following
def reasignParams() {
if(params.B == '') {
params.B = params.A
}
}
pipeline{
parameters {
string(name: 'A', defaultValue: '1.1', description: "Master Value")
string(name: 'B', defaultValue: '', description: "Slave value")
}
}
After running the above Jenkins pipeline script (groovy), I ran into the following error
java.lang.UnsupportedOperationException
The alternative that I thought to this is as below
def reasignParams() {
if(params.B == '') {
def temp = params.A
# use temp variable instead of params.B; But this is inconvenient
}
}
I would like to learn if there is a way to reassign parameters in the Jenkins pipeline script? Any help would be greatly appreciated, Thanks in advance!
The params object in Jenkins Pipeline does not support write operations on its member variables. You can only initially assign them in the parameters directive (think of it like a constructor in that sense). If you want to reassign parameter values, then you do indeed need to make a deep copy like the following:
newParams = [:]
newParams.A = params.A

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.

In Jenkins, is there a timeout for Build with Parameters?

In Jenkins, I know that input can be combined with timeout (example), but what about Build with Parameters?
My (maybe incorrect) thought is to have "default" parameters set on a declarative pipeline Jenkinsfile, so that if a human runs it, he can enter the parameters, but when it runs periodically (e.g., daily at 12pm), the prompt is not required and the "default" parameters are used.
You are correct with regard to setting default values and can do this:
options {
timeout(time: params.timeoutTime, unit: params.timeoutUnit)
}
parameters {
string(name: 'timeoutTime', defaultValue: '30', description: '')
string(name: 'timeoutUnit', defaultValue: 'MINUTES', description: '')
}
When you trigger the build manually, it will use the parameters that you provide. For timer triggered builds, it will use the default values.
It seems like what you're looking for is the parameterized scheduler plugin perhaps? See my answer Here

Resources