Jenkins / Hudson environment variables

JenkinsEnvironment VariablesHudson

Jenkins Problem Overview


I am running Jenkins from user jenkins thats has $PATH set to something and when I go into Jenkins web interface, in the System Properties window (http://$host/systemInfo) I see a different $PATH.

I have installed Jenkins on Centos with the native rpm from Jenkins website. I am using the startup script provided with the installation using sudo /etc/init.d/jenkins start

Can anyone please explain to me why that happens?

Jenkins Solutions


Solution 1 - Jenkins

Michael,

Two things:

When Jenkins connects to a computer, it goes to the sh shell, and not the bash shell (at least this is what I have noticed - I may be wrong). So any changes you make to $PATH in your bashrc file are not considered.

Also, any changes you make to $PATH in your local shell (one that you personally ssh into) will not show up in Jenkins.

To change the path that Jenkins uses, you have two options (AFAIK):

  1. Edit your /etc/profile file and add the paths that you want there

  2. Go to the configuration page of your slave, and add environment variable PATH, with value: $PATH:/followed-by/paths/you/want/to/add

If you use the second option, your System Information will still not show it, but your builds will see the added paths.

Solution 2 - Jenkins

I kept running into this problem, but now I just add:

source /etc/profile

As the first step in my build process. Now all my subsequent rules are loaded for Jenkins to operate smoothly.

Solution 3 - Jenkins

You can also edit the /etc/sysconfig/jenkins file to make any changes to the environment variables, etc. I simply added source /etc/profile to the end of the file. /etc/profile has all all of the proper PATH variables setup. When you do this, make sure you restart Jenkins

/etc/init.d/jenkins restart

We are running ZendServer CE which installs pear, phing, etc in a different path so this was helpful. Also, we don't get the LD_LIBRARY_PATH errors we used to get with Oracle client and Jenkins.

Solution 4 - Jenkins

I tried /etc/profile, ~/.profile and ~/.bash_profile and none of those worked. I found that editing ~/.bashrc for the jenkins slave account did.

Solution 5 - Jenkins

The information on this answer is out of date. You need to go to Configure Jenkins > And you can then click to add an Environment Variable key-value pair from there.

eg: export MYVAR=test would be MYVAR is the key, and test is the value.

Solution 6 - Jenkins

On my newer EC2 instance, simply adding the new value to the Jenkins user's .profile's PATH and then restarting tomcat worked for me.

On an older instance where the config is different, using #2 from [Sagar's answer][1] was the only thing that worked (i.e. .profile, .bash* didn't work).

[1]: https://stackoverflow.com/a/5819768/444921 "Sagar's answer"

Solution 7 - Jenkins

I found two plugins for that. One loads the values from a file and the other lets you configure the values in the job configuration screen.

Envfile Plugin — This plugin enables you to set environment variables via a file. The file's format must be the standard Java property file format.

EnvInject Plugin — This plugin makes it possible to add environment variables and execute a setup script in order to set up an environment for the Job.

Solution 8 - Jenkins

Couldn't you just add it as an environment variable in Jenkins settings:

Manage Jenkins -> Global properties > Environment variables: And then click "Add" to add a property PATH and its value to what you need.

Solution 9 - Jenkins

This is how I solved this annoying issue:

I changed the PATH variable as @sagar suggested in his 2nd option, but still I got different PATH value than I expected.

Eventually I found out that it was the EnvInject plugin that replaced my PATH variable!

So I could either uninstall EnvInject or just use it to inject the PATH variable.

As many of our Jenkins jobs use that plugin, I didn't want to uninstall it...

So I created a file: environment_variables.properties under my Jenkins home directory.

This file contained the path environment value that I needed: PATH=$PATH:/usr/local/git/bin/.

From the Jenkins web interface: Manage Jenkins -> Configure System. In that screen - I ticked the Prepare jobs environment option, and in the Properties File Path field I entered the path to my file: /var/lib/jenkins/environment_variables.properties.

This way every Jenkins job we have receive whatever variables I put in this environment_variables.properties file.

Solution 10 - Jenkins

Jenkins also supports the format PATH+<name> to prepend to any variable, not only PATH:

Global Environment variables or node Environment variables:

Jenkins variable + notation

This is also supported in the pipeline step withEnv:

node {
  withEnv(['PATH+JAVA=/path/to/java/bin']) {
    ...
  }
}

Just take note, it prepends to the variable. If it must be appended you need to do what the other answers show.

See the pipeline steps document [here][2].

> You may also use the syntax PATH+WHATEVER=/something to prepend /something to $PATH

Or the java docs on EnvVars [here][3].

[2]: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#withenv-set-environment-variables "withEnv step" [3]: http://javadoc.jenkins-ci.org/hudson/EnvVars.html

Solution 11 - Jenkins

I only had progress on this issue after a "/etc/init.d/jenkins force-reload". I recommend trying that before anything else, and using that rather than restart.

Solution 12 - Jenkins

On my Ubuntu 13.04, I tried quite a few tweaks before succeeding with this:

  1. Edit /etc/init/jenkins.conf

  2. Locate the spot where "exec start-stop-server..." begins

  3. Insert the environment update just before that, i.e.

> export PATH=$PATH:/some/new/path/bin

Solution 13 - Jenkins

Add

/usr/bin/bash

at

> Jenkins -> Manage Jenkins -> configure System -> Shell->Shell > executable

Jenkins use the sh so that even /etc/profile doesn't work for me When I add this, I have all the env.

Solution 14 - Jenkins

Solution that worked for me

source ~/.bashrc

Explanation

I first verified Jenkins was running BASH, with echo $SHELL and echo $BASH (note I'm explicitly putting #!/bin/bash atop the textarea in Jenkins, I'm not sure if that's a requirement to get BASH). sourceing /etc/profile as others suggested was not working.

Looking at /etc/profile I found

if [ "$PS1" ]; then
...

and inspecting "$PS1" found it null. I tried spoofing $PS1 to no avail like so

export PS1=1
bash -c 'echo $PATH'

however this did not produce the desired result (add the rest of the $PATH I expect to see). But if I tell bash to be interactive

export PS1=1
bash -ci 'echo $PATH'

the $PATH was altered as I expected.

I was trying to figure out how to properly spoof an interactive shell to get /etc/bash.bashrc to load, however it turns out all I needed was down in ~/.bashrc, so simply sourceing it solved the problem.

Solution 15 - Jenkins

I tried all the things from above - didn't work for me.

I found two solution (both for SSH-Slave)

  1. Go to the slave settings

  2. Add a new environment variable

  3. PATH

  4. ${PATH}:${HOME}/.pub-cache/bin:${HOME}/.local/bin

The "${HOME}" part is important. This makes the additional PATH absolute. Relative path did not work for me.

Option II (pipeline-script)

pipeline {
    agent {
        label 'your-slave'
    }
    environment {
        PATH = "/home/jenkins/.pub-cache/bin:$PATH"
    }
    stages {
        stage('Test') {
            steps {
                ansiColor('xterm') {
                    echo "PATH is: $PATH"
                }
            }
        }
    }
}

Solution 16 - Jenkins

On Ubuntu I just edit /etc/default/jenkins and add source /etc/profile at the end and it works to me.

Solution 17 - Jenkins

Running the command with environment variable set is also effective. Of course, you have to do it for each command you run, but you probably have a job script, so you probably only have one command per build. My job script is a python script that uses the environment to decide which python to use, so I still needed to put /usr/local/bin/python2.7 in its path:

PATH=/usr/local/bin <my-command>

Solution 18 - Jenkins

What worked for me was overriding the PATH environment for the slave.

Set:   PATH 
To:    $PATH:/usr/local/bin

Then disconnecting and reconnecting the slave.

Despite what the system information was showing it worked.

Solution 19 - Jenkins

I have Jenkins 1.639 installed on SLES 11 SP3 via zypper (the package manager). Installation configured jenkins as a service

 # service jenkins
 Usage: /etc/init.d/jenkins {start|stop|status|try-restart|restart|force-reload|reload|probe}

Although /etc/init.d/jenkins sources /etc/sysconfig/jenkins, any env variables set there are not inherited by the jenkins process because it is started in a separate login shell with a new environment like this:

startproc -n 0 -s -e -l /var/log/jenkins.rc -p /var/run/jenkins.pid -t 1 /bin/su -l -s /bin/bash -c '/usr/java/default/bin/java -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --javaHome=/usr/java/default --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --httpPort=8080 --ajp13Port=8009 --debug=9 --handlerCountMax=100 --handlerCountMaxIdle=20 &' jenkins

The way I managed to set env vars for the jenkins process is via .bashrc in its home directory - /var/lib/jenkins. I had to create /var/lib/jenkins/.bashrc as it did not exist before.

Solution 20 - Jenkins

Here is what i did on ubuntu 18.04 LTS with Jenkins 2.176.2

I created .bash_aliases file and added there path, proxy variables and so on.

In beginning of .bashrc there was this defined.

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

So it's checking that if we are start non-interactive shell then we don't do nothing here.

bottom of the .bashrc there was include for .bash_aliases

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

so i moved .bash_aliases loading first at .bashrc just above non-interactive check.

This didn't work first but then i disconnected slave and re-connected it so it's loading variables again. You don't need to restart whole jenkins if you are modifying slave variables. just disconnect and re-connect.

Solution 21 - Jenkins

1- add to your profil file".bash_profile" file

it is in "/home/your_user/" folder

vi .bash_profile

add:

export JENKINS_HOME=/apps/data/jenkins  
export PATH=$PATH:$JENKINS_HOME

==> it's the e jenkins workspace

2- If you use jetty : go to jenkins.xml file

and add :

<Arg>/apps/data/jenkins</Arg>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelView Question on Stackoverflow
Solution 1 - JenkinsSagarView Answer on Stackoverflow
Solution 2 - Jenkinsbryan kennedyView Answer on Stackoverflow
Solution 3 - JenkinsdbiehlView Answer on Stackoverflow
Solution 4 - JenkinsmmacvicarView Answer on Stackoverflow
Solution 5 - JenkinsTJ BiddleView Answer on Stackoverflow
Solution 6 - JenkinsRob BarrecaView Answer on Stackoverflow
Solution 7 - JenkinsVicroView Answer on Stackoverflow
Solution 8 - JenkinsKiarash ZamanifarView Answer on Stackoverflow
Solution 9 - JenkinsofirbtView Answer on Stackoverflow
Solution 10 - JenkinsCJCombrinkView Answer on Stackoverflow
Solution 11 - JenkinsRene WoollerView Answer on Stackoverflow
Solution 12 - JenkinsStabledogView Answer on Stackoverflow
Solution 13 - Jenkinssumang_87View Answer on Stackoverflow
Solution 14 - JenkinsquickshiftinView Answer on Stackoverflow
Solution 15 - JenkinsMike MittererView Answer on Stackoverflow
Solution 16 - JenkinsArx CruzView Answer on Stackoverflow
Solution 17 - JenkinsJoshua RichardsonView Answer on Stackoverflow
Solution 18 - JenkinshookenzView Answer on Stackoverflow
Solution 19 - JenkinsPeter DotchevView Answer on Stackoverflow
Solution 20 - JenkinsJugeView Answer on Stackoverflow
Solution 21 - JenkinsFadidView Answer on Stackoverflow