Setting environment variables for Jenkins slaves - jenkins

New to jenkins and looking to create a job. I need to use the IP of my Jenkins Instance in part of my Java Code. Is there an existing environment variable that I can use or do I have to add one myself? If so, how exactly can I do this? The slaves are ec2 instances. I looked at some similar questions posted here but it seems they are talking about the master rather than the slaves. Thanks!
EDIT: To confirm, external requests will be sent to the IP that I set in my Java code

I do not think there is any such environment variable in the default list of jenkins Environment variables.
One way to set IP as environment variable on a slave is:
Go to the slaves configuration page
Under "Node properties" there is a section called "Environment Variables"
Add a new Environment variable, e.g. IP_ADDRESS with value = the ip of that slave.
You should be able to access it from jenkins job, just like any other environment variable. e.g. in a Shell build step ${IP_ADDRESS}

Related

Jenkins slave override environment variable set by master when execute multibranch pipeline job

I have setup Jenkins with a master node running Ubuntu and a slave node. Currently Jenkins use to build Android app (master and slave have different ANDROID_SDK_ROOT environment variables).
For slave I have configured environment variables as below:
The free style project run OK and the slave able to pickup the environment variable. A problem happens when I run multibranch pipeline job. It seems the job cannot pickup the environment variable always show error:
SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at
What is the root cause and how to make multibranch pipeline able to pick up correct environment variable?
Updated: I detect the problem because slave cannot override environment variable value set by master. But I don't know how to make slave able to override environment variable set by master.

Jenkins agent incorrect PATH environment variable

I'm using a remote agent/slave to build my project in Jenkins via SSH.
Although the correct PATH environment variable is available when SSH'ing to it with the same user, it's not available when Jenkins tries to use the agent for building.
With the pipelines DSL, I was able to add it to my environment at runtime.
environment {
PATH = "/usr/local/bin:$PATH"
}
But I want this location in the PATH variable at all time, without this configuration. Any pointers on how to configure this for my agent/slave; whether it is in the jenkins node configuration or on the machine itself?
Just for anybody who is having the same issue.
When adding a new node in Jenkins, the master caches the environment variables of this node, but doesn't update it afterwards, to avoid breaking the configuration.
If you update the environment variables on the node itself, this changes won't be available for builds from the Jenkins master. You have to re-add the node or add environment variables in the configuration of the node.

using environment variables from a jenkins slave

i have a little problem.
Our setup consists of 1 Master Jenkins and 2 Slaves, both Slaves use a different SVN Location String, which we saved in an Environment Variable... but both of them start the same .dll for a Test - Now my problem is that when i use %SVN_Location% that it takes the Environment Variables from the computer i run the build (the master).
So my question is there a way to tell him somehow to execute the %SVN_Location% not on the computer where the build starts, but on the computer where the slave runs
Use EnvInject to record the value you want to file
Use Copy To Slave to move the file to slave
User EnvInject on slave to load value to environment variables, before the SCM step
I managed it myself after some more Research to actually get the Environment Variable directly from the Slave and use it as a Parameter for the Master Node.
With the Plugin Dynamic Parameter by using a Dynamic Parameter and the Default Value Script: System.getenv("SVN_Location_TP1") and the checkbox Remote Script checked.
When starting the build with parameters now, it automatically loads via remote script the Environment Variable from the slave and uses it as a parameter for the jenkins execution, which can be used with %SVN_Location_TP1% (in my example)

How to make Jenkins use already existing environment variables set on a slave node?

I am making use of a batch script that is supposed to run on a slave node, which makes use of Sahi. The environment variable for Sahi is set as 'SAHI_HOME' on the node.
When I run the batch I figure out, it is not able to locate Sahi classes.
How do I enforce Jenkins to make use of environment variables set on the slave? I mean is there any way to fetch environment variables set on a slave node?
We got around this issue by installing and updating Sahi automatically. There is a nice Jenkins Plugin: https://wiki.jenkins-ci.org/display/JENKINS/Custom+Tools+Plugin
You just need to place a Sahi Zip somewhere for Jenkis to access. The custom tool plugin automatically unpacks archives and creates a toolname_HOME environment variable.
Just name your tool SAHI and you have Sahi and $SAHI_HOME on every job and node you need.
Regards
Wormi
I ran into a similar issue with my AIX slaves. The issue is that the .profile file is not executed when a non-interactive shell is started. Therefore, you have several options.
Make sure that the environment variable is set in the environment file (in AIX, I can set the ENV variable to a filename that will be executed for both interactive and non-interactive shells.) I think the .kshrc file might qualify too.
Set the environment variable in the node configuration
set the environment variable in the master configuration
set the environment variable in the job (needs env inject plugin)
set the environment variable explicitly in the bash script

Is there a env variables for slave node home in JENKINS?

We know there's a environment variable named JENKINS_HOME so we can use it anywhere as $JENKINS_HOME.
But now when I running projects on slave nodes, I need to use the home directory of jenkins (which named "remote FS root" when defining a slave node) on slave node as a variable. And I found $JENKINS_HOME always be the home directory of jenkins on master node even I'm running projects on slave node.
Anyone can help? Thank you!
Old question, but seems never answered
The variable is not directly exposed in the job's environment, but you can figure it out ...
By default (ie: you did not set a custom workspace),
WORKSPACE=<RemoteFS>/<JOB_NAME>
and JOB_NAME includes folders if you use them.
ie: WORKSPACE=/path/to/remoteFSroot/myfolder/jobname
and JOB_NAME=myfolder/jobname
You can manipulate from there.
But, there should be no need to store the data inside the remoteFS root and is even probably a bad idea, since you should be able to delete all the workspaces w/o impacting your data. Just store it in another directory (or even an NFS shared across all you slaves) and reference with a complete path.
If you see JENKINS_HOME environment variable, it just a side-effect of the script you use to start Jenkins master. You cannot trust it to be always available.
Maybe you could explain why you think you need to know the slave home directory?
Using the Jenkins pipeline plugin, I retrieve the remote root directory of the slave node where the job is running at with
run.getEnvironment(listener).get("BASE")

Resources