Updating jenkins job variable - jenkins

Parameterized variable are not getting updated in jenkins
I m using the conditional build-step plugin to update the jenkins job parameter by executing shell script its showing me the new value of variable as well but its not get reflected.

You can try the EnvInject Plugin. One of the features is a build step that allows you to "inject" parameters into the build job from a settings file.
Create a property for the email list in the env.properties file:
echo "variable=`value`"> env.properties
It will create the properties file in the job workspace directory.
env.properties
In shell script:
"$variable"

If I understand correctly, you are trying to change the value of a pre-defined parameter
from within a script that is run by the job.
This will not work, because of "scope" (or "call-stack"),
as a process (your script) cannot change the environment of a parent process (your Jenkins job).

Related

Dynamic Properties file in Jenkins for injecting Env variables

So this is my entire Use case:
I have a parameterized build job which accepts file parameters. After the build I need to send a mail with that file and the size of the file. For this, I'm trying to add the name and size of the file as an Env variable using EnvInject Plugin.
But EnvInject is in the Build Environment step. The file parameter gets stored in the Workspace of the build only in the Build step, not in Build environment. So, there will be an error like File not found.
Due to which, I'm trying a crooked way of defining a properties file somewhere on my local system.I'm mentioning this properties file in "Properties File Path" of Inject Environment variables. In the build step I'm adding FOO=BAR and other values in the file so that I can use those values as my env variables down the line, like when I configure my e-mail template in Post Build Actions.
Can this process be done easily? I was initially making the properties file in JENKINS_HOME. I just got to know that I'm not allowed to do that as in master-agent architecture, JENKINS_HOME will be different and build will fail.
PS-1. The workspace gets deleted after every build
2. Any other plugins which can be used? If possible, please suggest without installing some new plugin as I'm not Jenkins admin
One way of solving this problem is by creating 3 different jobs as follows -
job 1 --> should call below 2 jobs(job 2 & job 3)
job 2:
Build --> Trigger/call builds on other projects --> job 2(block until the triggerred projects finish their builds)
select"Build on the same node" from Add Parameters.
job 3:
Build --> Trigger/call builds on other projects --> job 2(block until the triggerred projects finish their builds)
select"Build on the same node" from Add Parameters.
Job 2 (Create this job as follows):
Execution API --> using "GET_FILE" option you can download the required details on to the current working directory of your job.
Execute shell -->
now within "Execute Shell", download "consoleText" using wget command.
process the "consoleText" using unix command prepare a key-value pairs and store it under /tmp folder. i.e. "/tmp/env.prop"
Job 3 (Create this job as follows):
Bindings:
select "Inject environment variable to the build process" and under 'Properties File Path' enter "/tmp/env.prop"
now you can use the variable which you created in Job2 in the current job without any issue.
please note that it is important to select "Build on the same node" in Job 1, because this will preserve the data and it allows other jobs to access this information.
Let me know if its not clear.

how to pass a value to a property file from jenkins

I have created a Jenkins job to run the automation scripts.
I am using config.properties file to pass the input parameters to run the script.
My config.property file contains below :
browserName=ie
url=http://google.com
Is it possible to set the above parameters from Jenkins job?
Is it possible to do like below :
browserName=${BROWSER_NAME}
url=${URL_NAME}
Can i pass "BROWSER_NAME" && "URL_NAME" value from Jenkins job. if yes, then how?
Please suggest. I am new to Jenkins job configuration.
what you can do, is inside your script file, change it to take parameters,
after that, in you job, you can add a parameters, to be filled by the Jenkins form like "Build with parameters"
Finally, in your run script shell window, add the command to run your script plus the paramters you have filled exemple :
==> $PARAM_ONE (param job jenkins);
script.sh $PARAM_ONE
Good Luck
Like Hatim said: "change your script to take parameters" and pass params to your shell script.
But i suppose you want your script to access config.properties and config.properties must be filled with jenkins params?
if so, then you can just echo your jenkins params to this file directly.
Set your params in jenkins.
If you want to use complex config than your approach is ok and you can execute shell to insert params from jenkins to a file simply by echoing them and then start your script:
But if your config.properties is so small, you'd better use script params to make your build config easier. To do that you should modify your .sh script to take parameters from jenkins:
browserName=$1;url=$2
And then just add your params in execute shell:

How to set a variable inside Jenkins Shell Command

I have a Jenkins build job. It has section for shell command where I read "version" of the current application that i am building.
Now, i want to set the Jenkins custom variable "VERSION" with the "version" in the same shell command section.
I need to pass this value as parameter to some other job being triggered after successful build of this job.
Now, I would pass the Jenkins variable VERSION to some other Job.
Please suggest how can I do this.
Use EnvInject Plugin to inject runtime variables into Jenkins build process so that you can use in other build steps or you can pass it on to other Job's as input parameter.
I could get some clue from Suresh's answer. I did the following steps and it worked well.
Step 1: Install Plugin “EnvInject Plugin”.
Step 2: Set the custom env variable and store that in env.properties
Step 3: Add POST Build Action, “Trigger parameterized build on other projects”
Step 4: Add the variables that the Post Build Job is expecting:
Here the BUILD_NUMBER and FILE_VERSION are two parameterized variables
that the next Job is expecting. FILE_VERSION is passed as parameters
from properties file which was stored in STEP 2.

Passing variable from shell script to jenkins

I trigger a shell script from Jenkins, This scripts get date and export it as a environment(Linux) variable $DATE. I need to use this $DATE inside same Jenkins job. I made job as parameter build. Created a string parameter as DATE value as DATE=$DATE. But it is not working.
Please suggest !!
You mention that you are exporting a DATE environment variable in an shell script, which is presumably being started via an "Execute shell" step.
The problem is, once the shell step has completed, that environment is gone — the variables will not be carried over to subsequent build steps.
So when you later try to use the $DATE value — whether in another build step, or as a parameter to another job — that particular environment variable will no longer exist.
What you can do instead is use the EnvInject plugin to export environment variables during a build. Variables set up using this plugin will be available to all subsequent build steps.
For example, you could write the DATE to a properties field in one build step:
echo DATE=$(date +%Y-%m-%d) > env.properties
Then you can add an "Inject environment variables for your job" build step, and enter env.properties in the "Environment Properties File Path" field.
That way, the DATE variable (and anything else in that properties file) will be exported and will be visible to the rest of the build steps.
You could use an assignment statement and sh's returnStdout to get the value in Jenkins without having to write to a properties file.
foo = sh(
returnStdout: true,
script: 'date'
)
Then later on in the Jenkinsfile you can use $foo like any other variable.
EDIT: This is for a pipeline job, not a freestyle job.
I had the same issue.
The solution that worked for me was:
env.ABC=bat(returnStdout: true,
script: ''' #echo off echo abc ''').trim()
The .trim() and #echo off is important if you want to reuse the variable in another batch script.

Jenkins EnvInject build step

I have a Jenkins job that is doing the following (amongst other things):
Read user input for ENVIRONMENT and SERVERTYPE
Inject environment variable AGENT (initially blank) as a build step to create
a new variable
Execute shell as a build step to populate AGENT, based on what was entered
in ENVIRONMENT and SERVERTYPE
Use AGENT as an input to a plugin as a post build action
The problem is that the value of AGENT doesn't seem to persist outside of the "execute shell" build step. When I try and pass it into the post build action plugin, it's still blank.
Can anyone point out what am I doing wrong? I have read the documentation, but can't seem to figure it out.
Your problem is that whatever variables you set in the shell script, they don't make it out.
This is true for any process: child process (your shell script) can never directly affect environment of parent process (Jenkins executor client).
If you need the data to persist, you need to store it outside the script; there are many options like uploading it to a server or storing it in a database, the most obvious and easiest option is to save it to a file.
You can even save the value to a "properties file" in the syntax supported by EnvInject and specify path in "Properties File Path" field.
You need to populate properties file with the values of the environment variables to be injected again, so they will survive until post-build actions. The properties file usually resides in the job's workspace.
For example use the following steps:
Build step "Execute shell" :
AGENT="My agent"
echo AGENT=$AGENT > my.properties
Build step: "Inject environment variables",
Field "Properties File Path":
$WORKSPACE/my.properties
Post-Build Actions: "Editable Email Notification", Field "Default Content":
Current Agent $AGENT
Or ${ENV, var="AGENT"}

Resources