How to set a persistent global variable by scripted pipeline? - jenkins

I need to pass a variable from a pipeline to another one without triggering the build of the second one.
Basically, the first pipeline should set the value of this variable and terminate. When I run the second pipeline, it should be able to get this value.
So far, I used a properties' file but I'd like to know if there is a way to avoid it. Is it possible?

I think the EnvInject plugin is what you need.
As alternative solution:
You could also write to a file from the first job and source that file from your second job if it is started. The location must be persistent, do not use the workspace dir.

Related

Jenkins: Pass environment variable to the job parameter

Since I have the same static rarely changed parameters used by several jobs I decided to put it somewhere in one place of my Jenkins and use it across jobs.
The first thought that came to my mind was to move my 'static data' to the environment variables and get it using Active choice reactive parameter plugin which allows running simple groovy scripts on the job parameters page.
Please note that I know how to get environment parameters in the pipeline, but I do really need to have this data on the build with parameters screen, e.g. once I clicked build with parameters - I need my groovy code inside Active choice reactive parameter was able to read this environment variable and display as a parameter to the user.
A simple example of this need:
The environment variable contains the list of servers, the job is going to perform deployment of the application to the selected server. In this case, I want to be able to write something like this in the groovy script section of Active choice reactive parameter:
return[${env.SERVERS_LIST}]
Unfortunately the example above doesn't work. I wasn't able to find any working solution for this yet.
Well, after a few more tries I finally found a solution.
Instead of trying to read the environment variable in the pipeline manner the simple
return [SERVERS_LIST]
works perfect

Injecting more than one properties file into a Jenkins job

Right now I'm using EnvInject plugin to insert my environment variables through a properties file into my Jenkins job.
However, now I have a second job which needs the same environment variables as the first job and than some more additional variables which I would like to load via another properties file.
I know, there is a possibility to insert the values via Properties Content Edit field of the EnvInject-plugin, but I would like to keep it in a file, so it can be shared between jobs. But there seems to be no possibility to add a second properties file to EnvInject-plugin.
Is there any way to inject more than one properties file into a job or any other plugin, that could handle my scenario?
There is a simple way to get around the limitation you have.
You should load each file in the Build section, as a build step.
Use the Inject environment variables build step, and load each file you want. You can add multiple files by setting up multiple build steps of this type.
This works well for me on a similar need.
You can use Config File Provider Plugin to config some shell scripts.
You can add multiple files and then execute them.

How to configure parameters with time stamp

I have a job ( parameterized build) I am using BUILD_ID as a parameter and I need to add time stamp for the every BUILD_ID.
How to add time-stamp to the parameter.
I need the BUILD_ID value like Test_Build_{time-stamp}.
Kindly help me
There are numerous ways to do this.
Probably your best bet is the Dynamic Parameter Plugin. On the plugin page, one of its main examples does almost exactly what you described.
The Active Choices Plugin allows you to run groovy code to generate your parameter value(s). Here's a screenshot of the plugin setup along with the groovy code:
The EnvInject Plugin allows you to insert environment variables from a properties file as a build step. Using this method you would basically have to have 2 build steps, one to create the properties file and another to read it.
Or using the Groovy Plugin, you can add a groovy script build step to modify the parameter value.
I'm sure there a number of other ways to accomplish this as well.
Install Zen Timestamp plugin and use variable $BUILD_TIMESTAMP

Retrieve a variable from child job in jenkins

I have a job that is reused/called from two parent jobs, I need to retrive a variable from this job to use it in each parent job.
The shell variable is a list of artifacts with size and md5sum.
The goal is to use this list in Editable email notification.
how I can retrieve it, let's call it ${artifacts_list}.
Thank you,
Although i have not tested it, you can try this approach: You can echo the variables and their values in a file (name=value) and read the file in your parent job. For making those variables available in your parent job's environment, you might need EnvInject Plugin
A cumbersome way is to use the Jenkins API to read the JSON of the other job's specific run, and extract the parameters from it.
An easier way is to:
Export job's parameters to file (use Export Parameters Plugin for that, or simply echo out the required param in format param=value like the other answer suggested)
Archive the exported parameters file at the end of the build using "Archive Artifacts" post-build action
In the downstream job, use Copy Artifacts plugin to bring in that file
Use EnjInject plugin to read the file and make it available as environment variables.

jenkins: saving parameters of parameterized build for reuse

I have a parametrized Jenkins task. After the task execution I can see the parameters used for the execution.
I want to reuse parameters of any previous build of the task for following launches.
Brief googling did not help me.
Could you please advise me some solutions for my task?
Use the Rebuild Plugin -
It allows you to rerun any previous run of a job,
with the same parameters that were used in that run.
You can even change some of the parameters, if needed.
You could do the following:
Save the parameters to a file in properties file format (e.g. using some of the script execution builders)
Save this file to a known location within the job
Use the Parameterized Trigger Plugin to reuse the parameters

Resources