Inject Jenkins text parameter as environment variables - jenkins

I have a Jenkins job that I would like to provide multiple environment variables using something like a text box in the 'Build with Parameters' stage.
Generally I have these variables at a node or in Properties Content and they don't need changing but for certain tests I need to set different environment variables. I looked into EnvInject and regular Properties but I was unable to get them to inject my text parameter which contained multiple variables, perhaps I had it poorly formatted though.
So is there any way I can set multiple environment variables at the build parameterization stage without editing the job every time I want to try out something new?

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

How to update a Jenkins Properties Global Environment Variable from within a pipeline stage in Jenkinsfile

Wanting to update a ".env" properties value, so that the next execution has a new value.
loggingUtils.info("${env.testVar}")
env.testVar = "cat"
loggingUtils.info("${env.testVar}")
Currently what happens is if I configure the "env.testVar" to have a value of "dog" from within jenkins the print statements will be:
dog
cat
but the next time I execute I want it to be
cat
cat
However, it is always just
dog
cat
Is there a way to achieve setting the environment variables so that future builds will have the new variable? I would prefer to do this without a plugin if possible
Builds (can be thought of as "instances of executions") in Jenkins are independent of each other.
If you are trying to tie builds together by transmuting information across builds I would encourage you to think about what you are really trying to do and suggest you might not doing Continuous Integration properly.
Each time you execute a build it starts from scratch. Continuous Integration always starts with what's in source control. Nothing derived should be committed to source control.
I would suggest environment configuration should normally by stored in config files in source control and applied as appropriate via parameters to a build. (i.e what environment do I want to deploy a given build to?).

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.

Is there a way to set multiple parameters with one choice in jenkins

I am trying to manage a jenkins build where a user may select to build either from master, or a specific branch. There are several variables that need to be set depending on what the user chooses. I've spent all day trying to find any reasonable way of doing this, and the best thing I've come up with is offering the user two dropdowns populated with low level parameter values.
I think you can just set up two jobs by using different branch, that will be better, since different branch might will has different datamodel, setup a different schema for each of them will make more sense.
You could use the environment script plugin and execute a shell script to define and populate your build variables.
Have a look at the Extensible Choice Parameter Plugin. The global choice parameter might be exactly what you want.
You make a single choice parameter and than add "Inject environment variables" step specifying MyBuildConfig\${ChosenParameter}.prop in Properties File Path. Create such a file for each of the choice options. Finally, specify additional variables you want to set together with particular chosen parameter in each of the created files.

Jenkins global variables

I'm trying to use global variables within Jenkins on Windows to "automagically" retrieve the proper code base from our SCM system, but in each case that I've tried the variable substitution is not happening.
I've set up some global variables, with default values, within "Configure System" and have tried to access them with $VARIABLE, ${VARIABLE} and %VARIABLE% as part of the Branch field for the Surround SCM plugin with no success whatsoever.
I've also installed the Global Variable String Parameter plugin with the same success rate (0%). Using a literal value works just fine, but no type of variable substitution seems to work at all and I'm sure that someone has come upon this before and resolved it.
I've tried searching for something similar to this but nothing really approaches this usage of globals, instead it is normally discussed as a function within an external script, or parameter passed to a batch file, etc.
I've run "set" as the first step and can see that the variable is available, but the substitution is just not happening. If it means I will have to script something, then so be it, as I am trying to make this extremely flexible and as headache free as possible, but that isn't seeming to be the case in this case thus far.
My problem is eerily similar to this post: How are environment variables used in Jenkins with Windows Batch Command?, but again, I'm not looking to script this as it is a MUCH simpler solution to use the variable values directly.
from https://wiki.jenkins-ci.org/display/JENKINS/Surround+SCM+Plugin
Troubleshooting
Please contact Seapine support with questions about the integration or
to report bugs or feature requests.
Set your Jenkins project to be parameterized. Create a string parameter GIT_BRANCH that will be your branch variable (for example).
Under Source Control Management, use your branch variable in the form $GIT_BRANCH
That’s it. When you run your project, you will be prompted to enter a value for your GIT_BRANCH parameter.

Resources