Parametrize Jenkins Pipeline for different jobs - jenkins

I want to use the same pipeline from scm, and build them in few jobs with different test tags to run(passed as simple String parameter)
I found that i can do this with:
parameters {
string(name: 'param', defaultValue: 'Hello', description: 'How should I greet the world?')
}
I thought that i can change default value for each job there:
Parametrize build
But it is overriden by code from pipeline everytime.
Is there a way to pass this parameter( or maybe pass parameter directly in job in different way)?
Or i have to create diferent pipeline files for each value of this parameter?
Thanks
Kamil

I would suggest you to remove
parameters {
string(name: 'param', defaultValue: 'Hello', description: 'How should I greet the world?')
}
from the pipeline script and to use This project is parameterized when creating pipeline jobs instead. This way the pipeline script is same for all jobs, but your jobs can have different parameters.

Related

How to get build number as a parameter for downstream job from upstream job with declarative pipeline code

I have Two jobs like one is CI jod & another one is CD. I want CI build number should use on CD number.. Can you please help me with declarative pipeline script to get build number as a parameter. here CI job is calling CD job.
Jenkins already provides a simple means to access the number of the current build using env.BUILD_NUMBER. So if you wanted to pass the build number of CI to the downstream job CD, you could do
build([
job : 'CD',
parameters: [
string(name: 'MAIN_BUILD_NUMBER', value: "${env.BUILD_NUMBER}")
]
])
Then in the CD job, declare a parameter like this:
parameters {
string(defaultValue: null, description: 'Build No', name: 'MAIN_BUILD_NUMBER')
}
You should then be able to use ${env.MAIN_BUILD_NUMBER} anywhere in your CD jobs' Jenkinsfile.

How to access pipeline name in the freestyle job

I have a pipeline which triggers a jenkins freestyle job.
How to access the pipeline name from the freestyle job.
I tried using ${BUILD_CAUSE} but it shows the value as UPSTREAMTRIGGER.
How to get the pipeline name?
JOB_NAME may contain the name of the job that you're after, unless it was triggered by an upstream job.
See a list of generic Jenkins environment variables that are available in most builds. The best way to find out what environment variables are available is to run sh 'printenv' in the steps.
If you need the name of the upstream job in a downstream job, I'd suggest setting a parameter on the downstream job and then referencing it when you trigger the job.
e.g. Upstream Pipeline job:
...
step {
build job: 'freestyle-downstream-job', parameters: [
string(name: 'upstream_job_name', value: "${JOB_NAME}")
]
}
...

Jenkins parameter does not exist on first run (multibranch)

I'm having a problem with a Jenkins multibranch pipeline, which is parameterized. The parameters are all declared in the Jenkinsfile.
The problem is that these parameters do not exist on the very first run of the job. So, the very first execution will fail with groovy.lang.MissingPropertyException. Any subsequent run is now aware of the parameters and will not fail.
Since this is a multibranch pipeline this happens for every new pull request or tracked branch. Is there any workaround to avoid this problem?
I tried setting the parameters in the UI as well, however there is no option on the pipeline configuration page to set parameters. Probably because this is a multibranch pipeline?
Cheers
This is a known issue with Parameters in Pipelines. To know which parameters are needed, Jenkins needs to execute the Jenkinsfile once. For example the parameters in the GUI are not available until after the first run of the pipeline.
To prevent errors, you could specify sensible default values like this:
pipeline {
agent any
parameters {
string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
}
stages {
stage('Example') {
steps {
echo "${params.Greeting} World!"
}
}
}
}

How do you pass parameter into a pipeline script but not make it a parameterized build in the jenkins UI

How do you pass parameter into a pipeline script but not make it a parameterized build in the jenkins UI.
This script will get used in more then one place and I would like to be able to pass some values into the script but not make it a parameterized build where the users can set them.
In its simplest form, here's how you would do that.
The job that you want to pass parameters to looks like this (scripted pipeline). Let's call this job "ParamJob".
print "Parameter passed: ${params.myParam}"
The job that you would kick "ParamJob" off with would look like this ..
build job: 'ParamJob' , parameters:[
string(name: 'myParam', value: 'Test parameter')
]
And the output of "ParamJob" is
Parameter passed: Test parameter

How can I return parameters in Jenkins pipeline based upon the job name?

I have two Jenkins pipeline (a)deploy and (b)test_deploy. I want to run both these pipeline from one Jenkinsfile as there functionality is same.
deploy pipeline runs on for all non-test environments
test_deploy pipeline will run only on test environments
I want to only show test environments as parameters in test_deploy pipeline. I want to add something like if/else conditions on choice parameters that will return environment names in parameters based upon the job name. How can I condition choice/parameters based upon job name?
You can add this at the top of your Jenkinsfile:
if (JOB_NAME == 'deploy') {
properties([
parameters([
choice(name: 'deployEnv', choices: ['deployEnv1', 'deployEnv2'], description: '')
])
])
} else if (JOB_NAME == 'test_deploy') {
properties([
parameters([
choice(name: 'testEnv', choices: ['testEnv1', 'testEnv2'], description: '')
])
])
}
This works in both Declarative and Scripted Pipelines, and populates the choice parameters based on the job names.

Resources