Jenkins Pipeline: build job with default paramter value if value not supplied - jenkins

I have a Jenkins pipeline job that accepts 1 parameter with default value.
This job is building another job passing the parameter value.
Code of parent job:
node
{
stage ('build job_1')
{
build job: 'job_1',
parameters: [[$class: 'StringParameterValue', name: 'DROP_LOC', value: 'default_value']]
}
}
Expected behaviour:
If parent job get DROP_LOC parameter value, it should use it to build job_1
If parent job has no DROP_LOC parameter value, it should use the default value to build job_1
Please help how to do that? What is the correct code?

You can just use DROP_LOC variable directly
stage ('build job_1')
{
build job: 'job_1',
parameters: [[$class: 'StringParameterValue', name: 'DROP_LOC', value:DROP_LOC]]
}

Related

Jenkins groovy script parameter get from another job

I have one pipeline and one other job. i want to pass parameter.
This is my groovy script which is inside pipeline job.
pipeline {
agent any
stages {
stage('release') {
steps {
echo 'This is release!'
echo branch
build job: projectname , parameters: [[$class: 'StringParameterValue', name: 'branch', value: branch]]
}
}
So this branch i want to pass into build job. echo branch also printing perfectly.
And this is how i tried to get my branch name from release job
This trigger an error.
org.tmatesoft.svn.core.SVNException: svn: E160005: Target path '/${branch}' does not exist
It does not resolve to the branch name which i want
This should work:
build job: projectname , parameters: [string(name: 'branch', value: "${branch}")]

Passing parameters down a Jenkins pipeline

I cannot get Jenkins to pass a string Parameter down the pipeline.
When I run the pipeline, I input the string value for $ServiceName and the job continues but it doesn't pass this param to the first job in the pipe (NEWSERVICE - Add New). In the jenkins file in the 'build' stage I've tried params.ServiceName, $params.ServiceName, env.ServiceName, $env.ServiceName, $env:ServiceName. No luck.
I need to pass the param to a Powershell build process in the NEWSERVICE job (which currently just echos the Param with $env:ServiceName - but it's always empty) Any help would be vastly appreciated.
pipeline {
agent any
parameters{
string(name: 'ServiceName',
defaultValue: '',
description: '',)
}
stages {
stage('Add new Service'){
steps {
build(job: "NEWSERVICE - Add New", parameters: [string(name: 'ServiceName', value: params.ServiceName)])
}
}
}
}
In Pipeline you need to pass string parameters like this:
parameters: [
[$class: 'StringParameterValue', name: 'ServiceName', value: ServiceName]
],
Refer this to understand different type of variable passing while calling a job from Jenkinsfile.
https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
If you are looking to use a Jenkins env var in a powershell script - which i do all the time! - the env var has to be set up as such in Jenkins first. In other words,
env.myVar in a Jenkins context will be visible as $env:myVar in a PowerShell context. But you need to set it as an env var, local Jenkins variables won't be visible to a child script (unless passed in as a parameter). I have a detailed writeup here: https://stackoverflow.com/a/62538778/556078

How to pass choice parameter to call a job inside jenkins pipeline

How can I pass choice parameters for the downstream job when called inside a stage in the jenkins pipeline?
I tried the below solutions but none worked:
stage('build job') {
steps{
script{
build job: 'test',
parameters: [
choice(choices: "option1\noption2\noption3\n", description: '', name: 'choiceParam')
]
}
}
}
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is using symbol ‘choice’
Tried these as well:
parameters:
[
[$class: 'ChoiceParameterValue', name: 'choiceParam', value: "1\n\2\n3\n"],
]
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is named ChoiceParameterValue
I actually want to pass the choice parameter as a build parameter like "$choiceParam" for value so that I can just update the jenkins job configuration instead of always updating the values in the pipeline script
Can someone please help me with this
Thanks
When you are building a job via the Build step, you are kicking it off so you need to have "selected" a value.
In this instance you would pass in the desired 'String' choice. Not a list of choices. i.e. "1"
We create our list of params and then pass that in. So: our current job has these input params:
choice(name: 'ENV', choices: product, description: 'Env'),
choice(name: 'ENV_NO', choices: envParams(product), description: 'Env No'),
We pass these downstream by setting them:
List<ParameterValue> newParams = [
new StringParameterValue('ENV', params.ENV),
new StringParameterValue('ENV_NO', params.ENV_NO),
]
build(job: "job", parameters: newParams, propagate: false)

Is there an option to call Jenkinsfile from Jenkins job (like how we specify script path in pipeline)?

If the above is not possible, is there a way to write groovy script to call Jenkinsfiles within Jenkins job
You can call Job B from Job A as a downstream job in pipeline and even pass parameters to it like so:
build job: 'CloudBees/Folder/To/JobB', propagate: false, wait: false, parameters: [[$class: 'StringParameterValue', name: 'PARAM_NAME', value: "${env.SOMEVALUE}"]]
I use this to only call Job B from Job A at the end of Job A in a post build success clause. The success clause only executes if Job A still has a job status of SUCCESS at that point.
post {
success {
script {
build job: 'CloudBees/Folder/To/JobB', propagate: false, wait: false, parameters: [[$class: 'StringParameterValue', name: 'PARAM_NAME', value: "${env.SOMEVALUE}"]]
}
}
}

triggering another jenkins job from jenkinsfile in pipeline

I am trying to invoke a separate jenkins job whose direct job url is https://jenkins.example.com/job/jobName/. This job runs with one parameter name "branch", whose value is "Master".
Below is how I am giving in my Jenkinsfile, but when I run it, gives me error
ERROR: No item named https://jenkins.example.com/job/jobName found
if ("${params.buildParam}" == 'test' || !params.buildParam ){
stage('Test') {
def job = build job: 'https://jenkins.example.com/job/jobName/', parameters: [[$class: 'StringParameterValue', name: 'branch', value: 'Master']]
}
}
The build step takes a job name as parameter, not a URL. So try
build job: '/jobName'
to refer using the absolute path. Depending on where your pipeline job is, you might use something like the following as well:
build job: '../../jobName/'
btw. you can avoid string interpolation here:
if (params.buildParam == 'test' ...)

Resources