Jenkins - How to pass environmental variable from freestyle job to pipeline job - jenkins

I have a freestyle job in Jenkins and would normally pass parameters to another freestyle job using the Predefined parameters option.
example:
PROJECT=Myproject
PATH=/depot/workspace/
Previously I could access the above values through the KEY in the downstream job through the environment by using ${PROJECT} OR ${PATH}.
My problem now is that I have a pipeline job that needs to access the above values but when using ${PROJECT} OR ${PATH} it does not work.
So, in general how I want it to work is have the freestyle job run first and pass the parameters to the downstream pipeline job.

You might need to use "${params.PROJECT}" in your pipeline to access the parameters.

Related

Get value from parameterized remote trigger plugin

I tried to use parameterized remote trigger plugin to pass value to a remote job following https://github.com/jenkinsci/parameterized-remote-trigger-plugin/blob/master/README_JobConfiguration.md guide. In the upstream job I input a=b into the Parameters field, but in the downstream job in a Execute Windows batch command step, when I tried to get the value using echo %a% the output is empty. How can I get the value of a from downstream job?
You need to make parameterized downstream job having a as parameter. I think you missed this point.

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}")
]
}
...

Load Jenkins build parameters from SCM

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

Jenkins predefined parameters and downstream job

I have a question for downstream job in jenkins.
I want to use directly the value of a predefined parameters from upstream job in the git parameter of a downstream job.
How is this done?
Yes , just add the parameters to the downstream job. And it will do the trick

How to pass env vars to a MultibranchPipelineJob created by Jenkins Job DSL?

I am creating a MultibranchPipelineJob with Jenkins Job DSL. I want to pass some environment variables to the job, but I can't figure out how to do that from the documentation.
MultibranchPipeline Job no longer support parameters
You can use Folder Properties Plugin to set your Env variables, which can be accessed by all the jobs within that folder. https://plugins.jenkins.io/folder-properties/
However, the MultiBranch pipeline job has a lot of performance issues so we moved away from multibranch pipeline jobs. We wrote a DSL job that would act as a multibranch pipeline job - which will scan through the git branches and create Simple pipeline jobs as needed.
You pass them as parameters like this:
parameters {
stringParam("MyVariable1", "my-value1")
stringParam("MyVariable2", "${my-dynamic-value2}")
}
Then consume them in the job using parameters or environment (both work equally) as this:
echo "my vars are ${parameters.MyVariable1} or ${env.MyVariable2}"

Resources