Load Jenkins build parameters from SCM - jenkins

Is it possible to have Jenkins pull a prameterized build from SCM (Git)?
I'm currently using "Pipeline script from SCM" where Jenkins retrieves the pipeline script but not the build parameters e.g. "String Parameter", "Choice Parameter", etc.

Jenkinsfile (Jenkins pipelines) are capable of this.
https://jenkins.io/doc/book/pipeline/syntax/#parameters
Be aware that parameters are post processed. So the 1st build will just be "Build" not "Build with parameters". After the 1st build, it will change.
This can be alleviated by using default values and always referencing the params using the full params.PARAM_NAME syntax. Don't just reference it as PARAM_NAME as this will cause Jenkins to search for env.PARAM_NAME by default.

Yes, you can retrieve parameters from a Parametrized Build in a Pipeline script from SCM Jenkins job.
To access them in the Jenkins file, use env.[PARAMETER_NAME].
For example:
echo 'Param value: ' + env.SOME_PARAMETER

Related

How can I select a Jenkins pipeline file from a branch specified in a build parameter

I have a parameterized Jenkins pipeline build. One of the parameters (branch) is the branch to build. The pipeline file is stored in the branch.
In the Pipeline definition, when I use */${branch} as the branch to build in place of */main the ${branch} does not get replaced but shows up as a literal. If I hard code the branch it works as expected.
The ${branch} does get replaced as expected in the pipeline file. So the branch parameter is being set.
Is there a way to get the parameter value into the "Pipeline script from SCM" retrieval from git?
You can try another approach as following:
In pipeline configuration page, change pipeline from SCM to pipeline script
Put following pipeline script in input box
node('<Jenkins node label>') {
properties([
parameters([
// parameter for branch
])
])
git url: '', credentailId:'', branch: "${branch}"
load '<relative path to your Jenkinsfile>'
}
You can use the branch parameter, when you disable the "Lightweight Checkout".
See JENKINS-28447

Jenkins Pipeline: Param in batch call

I have a pipeline build working in Jenkins, and have made the project parameterized to pass some information into the pipeline script. Everything works well except for inserting a "String Parameter" into a call to a batch file.
string(name: 'SANDBOX_ID', defaultValue: 'Build', description: 'Build Identifier (BuildNNN)')
I can see the parameter added, and can populate it via "Build with Parameters". I tried using it like so:
steps {
bat 'FtpPublisher.exe -srcDir "%WORKSPACE%/Builds/WebGL/Build" -targetDir "/Builds/${params.SANDBOX_ID}"'
}
This always results in a new folder created on my FTP server with the name "${params.SANDBOX_ID}" instead of the actual SANDBOX_ID parameter.
Found answer here after some more searching: pass parameter from jenkins parameterized build to windows batch command
Fixed by using %SANDBOX_ID% instead of ${params.SANDBOX_ID}

Passing workspace url of Job A to Job B in Jenkins

I have two pipeline jobs Job A and Job B. I need to pass the workspace url of Job A (say /var/lib/jenkins/workspace/JobA) to be used by Job B. The main idea is I am trying to copy the contents of target folder which is generated due to maven build but I don't want to use Copy Artifacts Plugin or Archive Artifacts Plugin to achieve the same.
I have tried using the option "This job is parameterized" where Job A is the upstream of Job B but i am unable to so using that option.
Can anyone help to achieve the same ?
The WORKSPACE variable is an env variable from Jenkins and is pointing like / .
For eg.
If the job name is Job_A --> the workspace value will be <jenkins_path>/Job_A
For eg.
If the job name is Job_B --> the workspace value will be <jenkins_path>/Job_B
So you can't use the WORKSPACE var and expects the Job_B to point to Job_A workspace value.
The below can be used to get certain properties from the upstream job.
Jenkins - How to get and use upstream info in downstream
Even if you want to hard code it in the Job_B it will be fine(not recommended)
Also for this to work your node should be same for both the jobs
I have found a way to do the same and it is working fine.
I have made the Job B a parameterized job using "This project is parameterized" and used string parameter.
Then, in the pipeline script of Job A, i invoked the Job B by passing WORKSPACE env variable. Here is the declarative pipeline script for Job A:
pipeline {
agent any
stages
{
stage ('Build JobB')
{
steps {
build job: 'jobB', parameters: [string(name: 'UPSTREAM_WORKSPACE', value: "${env.WORKSPACE}")]
}
}
} }
Now, in Job B pipeline you can try to echo the variable UPSTREAM_WORKSPACE. This is how we can pass the workspace url and use it to copy the artifacts.

trigger parameterized build on other projects does not pass parameter

In a Jenkins pipeline, parameters defined in upstream job is not being passed to downstream job even "current build parameters" option is selected. When I try to echo the parameter value, it is null.
Example: I have checked "This is a parameterized build". There is a string parameter in upstream job called "version" with the default value as "abc". When I run echo $version on the downstream job, in console output I do not see a value being printed.
Environment info:
Jenkins Version: 2.89.3
O/S: CentOs 7
Jenkins installed plugin info:
Build with Parameters: 1.4
Parameterized Trigger plugin:2.35.2
In the downstream job you need to set a variable to accept the parameter being passed.
Example: If foo parameter was passed from upstream job. The downstream should have a parameter (string or one of your choice) same name as foo and default value as $foo.
Finally I found out that the way this parameter works in jenkins 1.x and 2.x versions is different. In the newer version of jenkins, all the down stream projects must be parameterized with the defined parameter and the empty value. However, in Jenkins 1.x was able to pass the parameter from upstream to downstream without defining the parameter in the upstream project. Using only "Current build parameter" was able to do the work but not anymore. Not sure If I am the only one finding it bizarre.

How can I pass “build-step parameters” to a downstream project (freestyle project with parameterized triggers)?

I have a Jenkins setup with two freestyle-projects linked via Jenkins upstream/downstream post-build triggers (Jenkins Parameterized Trigger plugin). The upstream project builds something that is uploaded somewhere. And the downstream project picks that new thing up and tests it.
At the moment the downstream project finds the build by date by looking for “the latest”. I want to change that.
I would like to change upstream to generate a random string as filename in the build step and pass that to downstream as parameter. How can I do that?
(I know how to generate a random string – the point is: how to pass build-step information down to the “downstream job”?)
In your upstream pipeline script, add this:
build job: 'your-downstream-job', parameters: [[$class: 'StringParameterValue', name: 'YOUR_STRING_PARAM', value: "${yourRandomGeneratedStringVariable}" ]]
And your your-downstream-job should be e.g. a normal job which is parameterized (check the tick at This project is parameterized), with the StringParameter YOUR_STRING_PARAM.
Then just read the value of this environment variable and you can go on.

Resources