Jenkins rewrites URL to root when setting up behind HAProxy [duplicate] - jenkins

I'm trying to follow the directions here: https://wiki.jenkins-ci.org/display/JENKINS/Running+Jenkins+behind+Apache to set up my Jenkins server to appear at http://myhost/jenkins. It works, but the Jenkins website thinks http://myhost/ is the jenkins/ root.
I believe this problem is caused by the first warning flag on that web page, i.e. that my context path is not set correctly. However, I can't figure out where to set the context path. The instructions for ubuntu and windows are clear enough, but on Mac OS X 10.6, there is no jenkins.xml file, no /etc/default/jenkins file, and nothing of relevance I can see in ~/.jenkins/config.xml.
So, what am I missing? Where can I tell jenkins that its root is in /jenkins/ instead of /?

Paraphrasing from the document you mentioned;
You need to specify the context/prefix of the Jenkins instance, this can be done by modifying the Jenkins configuration as follows;
Either, set the context path by modifying the jenkins.xml configuration file and adding --prefix=/jenkins (or similar) to the entry.
Or Set the context path when using by adding --prefix=/jenkins to JENKINS_ARGS in /etc/default/jenkins (Ubuntu) or in an appropriate startup file.
So, how to find these things...
The Jenkins.xml file should be in the $JENKINS_HOME directory, I'm not sure if Mac OS has the "updatedb" and "locate " commands, but you could try doing updatedb && locate jenkins.xml
Also, have a look in the startup scripts; /etc/init.d if installed from a package, or add the JENKINS_ARGS to the environment properties for the User running Jenkins (append to ~user/.profile) or the arguments for the container running Jenkins.
Be aware that if your Jenkins installation (without the prefix argument) was running under:
http://myserver:8080/ => 200 Jenkins is here
adding --prefix=/ci/dashboard in the arguments will produce this behaviour:
http://myserver:8080/ => 404
http://myserver:8080/ci/dashboard => 200 Jenkins is now here

Not sure where to look in config.xml, but at http://myhost/jenkins/configure, there's an option called "Jenkins URL" that you can use to set that.

Just to provide some recent confirmation of the suggested approaches, on CentOS 7, with Jenkins 1.610, I was able to achieve this by changing jenkinsUrl in jenkins.model.JenkinsLocationConfiguration.xml to the desired one (e.g. http://127.0.0.1:8080/jenkins), adding
JENKINS_ARGS="--prefix=/jenkins"
inside /etc/sysconfig/jenkins, and restarting Jenkins.
FYI the Jenkins installation was made via Puppet, using this Puppet module.

Add prefix attribute to /etc/default/jenkins file:
JENKINS_ARGS="--webroot=/var/cache/jenkins/war --prefix=/jenkins --httpPort=$HTTP_PORT --ajp13Port=$AJP_PORT
Configure your web server (e.g. - nginx) to redirect /jenkins to localhost:8080;

Put this into /etc/apache2/other/jenkins.conf:
ProxyPass /jenkins http://localhost:8009/jenkins
ProxyPassReverse /jenkins http://localhost:8009/jenkins
ProxyRequests Off
<Proxy http://localhost:8009/jenkins*>
Order deny,allow
Allow from 127.0.0.1
</Proxy>
Then execute these commands:
sudo defaults write /Library/Preferences/org.jenkins-ci httpPort 8009
sudo defaults write /Library/Preferences/org.jenkins-ci prefix /jenkins
sudo launchctl stop org.jenkins-ci
The last command tells launchd to stop the running instance of Jenkins. And a new one will automatically be started because the launchd has been configured to always keep Jenkins running.

This is how I fixed it under Debian Wheezy running Jenkin 1.557
in /etc/default/jenkins , modify the JENKINS_ARGS line by adding "--prefix=$PREFIX"
JENKINS_ARGS=" ..... --prefix=$PREFIX"

you need to edit jenkins config file in directory
such like :
sudo vi /etc/default/jenkins and change var HTTP_PORT
next restart jenkins
sudo /etc/init.d/jenkins restart
hope this is helpful

I'm using CentOS7, add JENKINS_ARGS="--prefix=/jenkins" to /etc/sysconfig/jenkins and restart Jenkins worked. Then you can visit via ip:8080/jenkins

I'm not sure if people are still looking for this, but as I just ran across it, I figured I'd post my solution here.
By following the instructions at here, I was able to set the context located in Library/Preferences/org.jenkins-ci.plist to a more preferable address. The link has all the settings you can edit with an OS X native install.

I needed to configure Jenkins on a CentOS box via Puppet using the rtyler/jenkins module. Looking through the module code might suggest that HTTP_PORT and PREFIX should be the parameters in the config_hash but this did not work for me. What worked for me was something like the following Puppet configuration:
class { 'jenkins':
config_hash => {
'JENKINS_PORT' => { 'value' => '8085' },
'JENKINS_ARGS' => { 'value' => '--prefix=/jenkins' },
},
}
I was able to confirm that this updated the contents of "/etc/sysconfig/jenkins" (I believe this is the CentOS/RedHat file location).

For a Windows installation add the prefix within the <arguments> tag (jenkins.xml) and restart the service (Powershell Restart-Service jenkins). E.g.:
<executable>%BASE%\jre\bin\java</executable>
<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=8080 --webroot="%BASE%\war" --prefix=/jenkins</arguments>

Related

Jenkins Reverse proxy - Nginx

I set up Jenkins on my mac and use Nginx as reverse proxy.
I follw the doc( https://www.jenkins.io/doc/book/system-administration/reverse-proxy-configuration-nginx/ ) but stuck on
Set the context path by modifying the jenkins.xml configuration file and adding --prefix=/jenkins to the entry.
I can't find jenkins.xml in my folder (only config.xml)
And I don't know where to add --prefix=/jenkins, it only says add to the entry.
You can export this as an Environment variable as shown below before starting Jenkins. Or add this to the Jenkins startup script.
export JENKINS_OPTS="--prefix=/jenkins"

Jenkins: allow local checkout

It's the first time I'm using Jenkins. I created a new folder with just one file and created a git repository in it. Then I set up Jenkins with that repository.
What I get now is this error:
ERROR: Checkout of Git remote 'path\hello' aborted because it
references a local directory, which may be insecure. You can allow
local checkouts anyway by setting the system property
'hudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT' to true. Finished:
FAILURE
I tried to start jenkins using this command:
C:\Users\userName\.jdks\zulu11.56.19-ca-jdk11.0.15-win_x64\bin\java.exe -jar jenkins.war hudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true
But it didn't work.
How can I set this "allow local checkout" to true?
Define properties using option -D:
-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true
In my case I am running Jenkins on WSL2 (Ubuntu), I edited the file "jenkins" located at /etc/default, added the line:
JAVA_ARGS="-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true"
right below an existing line, like so:
# Allow graphs etc. to work even when an X server is present
JAVA_ARGS="-Djava.awt.headless=true"
JAVA_ARGS="-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true"
After saving the file, did a "service jenkins restart" and it worked for me, now whenever Jenkins is started I don't have to specify the option manually.
I have faced the same issue the solution was:
-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true needs to be BEFORE -jar jenkins.war in the command line call.
The solution is provided from the below recourse:
https://community.jenkins.io/t/checkout-of-git-remote-aborted-because-it-references-a-local-directory/4110
For Ubuntu edit config:
sudo gedit /lib/systemd/system/jenkins.service
Add line:
Environment="JAVA_OPTS=-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true"
Restsart service:
service jenkins restart
systemctl daemon-reload
Check config:
systemctl cat jenkins
For RedHat/CentOS, edit config file at: /etc/sysconfig/jenkins
Find: JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"
Add below:
JENKINS_JAVA_OPTIONS="-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true"
Then restart: sudo systemctl restart jenkins

Change JENKINS_HOME on Red Hat Linux?

I used this procedure to install Jenkins:
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+RedHat+distributions
After it was up and running I discovered the /var/lib/jenkins partition on my server is very small. I want to move it, but I do not want to change the user that it runs under. I am new to Linux and I'm stumped. How do I move it for example to my Home/Public folder? The "Jenkins" user doesn't seem to have a Home folder. Its running as a daemon on startup, so I have no idea where to configure those settings.
Can I create a Home folder for the Jenkins user? How?
I read this article:
https://wiki.jenkins-ci.org/display/JENKINS/Administering+Jenkins
but do not understand HOW to "set the new Jenkins home". I have used the export command, and restarted the service, but the old path still shows up in the Manage Jenkins screens.
I've read the 2-3 similar questions on stackoverflow also, but there's always a big missing piece for me. Where to find that file where I change the path permanently?
Here's an easy way to solve your problem. First, move the Jenkins directory from /var/lib/jenkins to /home/jenkins. Then create a symlink at /var/lib/jenkins pointing to /home/jenkins. And of course, stop the Jenkins service before doing that.
sudo service jenkins stop
sudo mv /var/lib/jenkins /home
sudo ln -s /home/jenkins /var/lib/jenkins
sudo service jenkins start
I managed to change the home location for Jenkins by modifying content of /etc/sysconfig/jenkins file as follows:
JENKINS_HOME="/home/jenkins"
Okay, I reread your question a little bit more closely, lets see if we can figure this out. I am going to list some info that you may or may not know.
The jenkins installation and jenkins home are not the same thing. One is where the war file and other parts that jenkins needs to run live. jenkins_home is where your data is stored. By default, jenkins_home lives in ~/.jenkins. When you start jenkins, it looks for an environment variable to tell it where to find those files.
Jenkins runs as a seperate user, which, by default, is jenkins. This way it doesn't get in the way of you. The jenkins user will not have access to YOUR home directory, so that would be a poor solution. Ideally, it would have its own home directory, /home/jenkins. Your home directory could then be /home/jenkins/.jenkins. You say that folder doesn't exist- if you don't have access to it to create it yourself, that is perfectly fine, you can specify ANY folder. However, the jenkins user must have ownership of that folder to read and write to it.
It looks like Jenkins on redhat will be running with tomcat by default. The documentation for how to set environment variables for tomcat is https://wiki.jenkins-ci.org/display/JENKINS/Tomcat
This all gets set up with a script.https://wiki.jenkins-ci.org/display/JENKINS/JenkinsLinuxStartupScript seems to be the one that is used for this purpose. Even if you don't know anything about shell scripting, this isn't too hard... lines with a # are comments. The first line
JENKINS_USER=jenkins
sets the name of the user account jenkins will be using. Look down a littlle further, and you'll see the line
export JENKINS_BASEDIR=/home/jenkins
export CATALINA_OPTS="-DJENKINS_HOME=$JENKINS_BASEDIR/jenkins-home -Xmx512m -Djava.awt.headless=true"
This lets you set a directory to where jenkins should live, and then sets the jenkins_home directory to that /jenkins-home.
For your application, you may want to do something like this
export CATALINA_OPTS="-DJENKINS_HOME=/var/jenkinsmount/home -Xmx512m -Djava.awt.headless=true"
That would then store all of your build data (which is the part that grows!) at /var/jenkinsmount/home ... while leaving the rest of your files in their current location.
I haven't used it on redhat, but hopefully I explained enough for you to actually understand what is going on so that you can get it going!
Other INFO:
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Unix+daemon
I have faced the same issue and question.
Connecting some dots I could fix my Jenkins after I moved Jenkins to a new location due to the same issue -space in disk under /var/lib/jenkins.
Here is the procedures that I had to follow to get it working taking in consideration that I am pointing Jenkins to a non-default port. (I have applied this process into 2 servers)
First, move the Jenkins directory from /var/lib/jenkins to /opt/jenkins
sudo service jenkins stop
sudo mv /var/lib/jenkins /opt/
Now you can change your workspace and build directory to any other location on your machine.
Jenkins provides 3 predefined variables that can be used to specify the new location:
JENKINS_HOME — Jenkins home directory
ITEM_ROOTDIR — Root directory of a job for which the workspace is allocated
ITEM_FULLNAME — ‘/’-separated job name, like “foo/bar”
sudo su jenkins (access as Jenkins user)
JENKINS_HOME=/opt/jenkins
ITEM_ROOTDIR=/opt/jenkins
ITEM_FULLNAME=/opt/jenkins
exit (exit Jenkins user)
Now, edit the jenkins config
/opt/jenkins$ sudo nano /etc/default/jenkins
Modify the following line
#jenkins home location
#JENKINS_HOME=/var/lib/$NAME (here is the default)
JENKINS_HOME=/opt/jenkins (that is our new location)
change the home directory of a user
sudo usermod -d /opt/jenkins/ jenkins
sudo service jenkins start
If anyone is having issues with space and you have to relocate your jenkins, just wanted to reiterate that::
sudo service jenkins stop
sudo mv /var/lib/jenkins /home
sudo ln -s /home/jenkins /var/lib/jenkins
sudo service jenkins start
works great, so thanks to the person who posted that answer a few years ago!
Richard Chen's location is where I found the jenkins file on my CentOS 6.6 system.
sudo service jenkins stop
mv /var/lib/jenkins /home/mylocation/
(made sure the new location had correct ownership and group-- Jenkins)
modified the content of the file /etc/sysconfig/jenkins as follows:
JENKINS_HOME="/home/mylocation"
sudo service jenkins start
If all these don't work, then the only solution that works is to edit the following file
/lib/systemd/system/jenkins.service
then need to execute reloading of configs by running
systemctl daemon-reload
after this restarting the service would reflect the change.
Some commands work for me as below:
Step 1: Stop jenkin service and moving folder
systemctl stop jenkins
mv /var/lib/jenkins /whatever/folder
sudo chown jenkins -R /whatever/folder
Step 2: Modify jenkins home location in /etc/default/jenkins
JENKINS_HOME=/whatever/folder/$NAME
Step 3: Restart jenkins service
systemctl start jenkins
[A] Find your current jenkins home folder, if you are not aware where it is ?
Jenkins -> Manage Jenkins -> Configure System -> Check label 'Home directory'
[B] To move current Jenkins home folder to a new directory. Follow below steps :-
Stop Jenkins service - by killing the process
Follow one of below approach to set new home folder for JENKINS.
a) By default Jenkins home directory is set to ~/.jenkins
b) "JENKINS_HOME" environment variable setup in operating system.
c) "JENKINS_HOME" entry in JNDI environment.
d) "JENKINS_HOME" system property to the servlet container.
Tomcat context descriptor of the servlet, you can set below field in apache-tomcat-8.5.28/conf/context.xml :
<Context ...>
<Environment name="JENKINS_HOME" value="/path/to/jenkins_home/" type="java.lang.String"/>
</Context>
e) If jenkins.war file is deployed in a tomcat server , then even appending below content in bin/catalina.sh will setup JENKINS_HOME.
CATALINA_OPTS="-DJENKINS_HOME=/path_to/jenkins_home/"
Manually copy Jenkins home folder content from old to new home
folder. (use cp command) . Instead of moving, copy step is advised to keep one backup. Later you can delete old workspace.
Now start Jenkins, then It will pick the new home directory
from the path mentioned in JENKINS_HOME variable.
Note: - Just by setting above variable "JENKINS_HOME" to a different path will not copy
the files from current Jenkins home path to new one automatically. This copy step - you have to do it yourself, Manually.

Jenkins website root path

I'm trying to follow the directions here: https://wiki.jenkins-ci.org/display/JENKINS/Running+Jenkins+behind+Apache to set up my Jenkins server to appear at http://myhost/jenkins. It works, but the Jenkins website thinks http://myhost/ is the jenkins/ root.
I believe this problem is caused by the first warning flag on that web page, i.e. that my context path is not set correctly. However, I can't figure out where to set the context path. The instructions for ubuntu and windows are clear enough, but on Mac OS X 10.6, there is no jenkins.xml file, no /etc/default/jenkins file, and nothing of relevance I can see in ~/.jenkins/config.xml.
So, what am I missing? Where can I tell jenkins that its root is in /jenkins/ instead of /?
Paraphrasing from the document you mentioned;
You need to specify the context/prefix of the Jenkins instance, this can be done by modifying the Jenkins configuration as follows;
Either, set the context path by modifying the jenkins.xml configuration file and adding --prefix=/jenkins (or similar) to the entry.
Or Set the context path when using by adding --prefix=/jenkins to JENKINS_ARGS in /etc/default/jenkins (Ubuntu) or in an appropriate startup file.
So, how to find these things...
The Jenkins.xml file should be in the $JENKINS_HOME directory, I'm not sure if Mac OS has the "updatedb" and "locate " commands, but you could try doing updatedb && locate jenkins.xml
Also, have a look in the startup scripts; /etc/init.d if installed from a package, or add the JENKINS_ARGS to the environment properties for the User running Jenkins (append to ~user/.profile) or the arguments for the container running Jenkins.
Be aware that if your Jenkins installation (without the prefix argument) was running under:
http://myserver:8080/ => 200 Jenkins is here
adding --prefix=/ci/dashboard in the arguments will produce this behaviour:
http://myserver:8080/ => 404
http://myserver:8080/ci/dashboard => 200 Jenkins is now here
Not sure where to look in config.xml, but at http://myhost/jenkins/configure, there's an option called "Jenkins URL" that you can use to set that.
Just to provide some recent confirmation of the suggested approaches, on CentOS 7, with Jenkins 1.610, I was able to achieve this by changing jenkinsUrl in jenkins.model.JenkinsLocationConfiguration.xml to the desired one (e.g. http://127.0.0.1:8080/jenkins), adding
JENKINS_ARGS="--prefix=/jenkins"
inside /etc/sysconfig/jenkins, and restarting Jenkins.
FYI the Jenkins installation was made via Puppet, using this Puppet module.
Add prefix attribute to /etc/default/jenkins file:
JENKINS_ARGS="--webroot=/var/cache/jenkins/war --prefix=/jenkins --httpPort=$HTTP_PORT --ajp13Port=$AJP_PORT
Configure your web server (e.g. - nginx) to redirect /jenkins to localhost:8080;
Put this into /etc/apache2/other/jenkins.conf:
ProxyPass /jenkins http://localhost:8009/jenkins
ProxyPassReverse /jenkins http://localhost:8009/jenkins
ProxyRequests Off
<Proxy http://localhost:8009/jenkins*>
Order deny,allow
Allow from 127.0.0.1
</Proxy>
Then execute these commands:
sudo defaults write /Library/Preferences/org.jenkins-ci httpPort 8009
sudo defaults write /Library/Preferences/org.jenkins-ci prefix /jenkins
sudo launchctl stop org.jenkins-ci
The last command tells launchd to stop the running instance of Jenkins. And a new one will automatically be started because the launchd has been configured to always keep Jenkins running.
This is how I fixed it under Debian Wheezy running Jenkin 1.557
in /etc/default/jenkins , modify the JENKINS_ARGS line by adding "--prefix=$PREFIX"
JENKINS_ARGS=" ..... --prefix=$PREFIX"
you need to edit jenkins config file in directory
such like :
sudo vi /etc/default/jenkins and change var HTTP_PORT
next restart jenkins
sudo /etc/init.d/jenkins restart
hope this is helpful
I'm using CentOS7, add JENKINS_ARGS="--prefix=/jenkins" to /etc/sysconfig/jenkins and restart Jenkins worked. Then you can visit via ip:8080/jenkins
I'm not sure if people are still looking for this, but as I just ran across it, I figured I'd post my solution here.
By following the instructions at here, I was able to set the context located in Library/Preferences/org.jenkins-ci.plist to a more preferable address. The link has all the settings you can edit with an OS X native install.
I needed to configure Jenkins on a CentOS box via Puppet using the rtyler/jenkins module. Looking through the module code might suggest that HTTP_PORT and PREFIX should be the parameters in the config_hash but this did not work for me. What worked for me was something like the following Puppet configuration:
class { 'jenkins':
config_hash => {
'JENKINS_PORT' => { 'value' => '8085' },
'JENKINS_ARGS' => { 'value' => '--prefix=/jenkins' },
},
}
I was able to confirm that this updated the contents of "/etc/sysconfig/jenkins" (I believe this is the CentOS/RedHat file location).
For a Windows installation add the prefix within the <arguments> tag (jenkins.xml) and restart the service (Powershell Restart-Service jenkins). E.g.:
<executable>%BASE%\jre\bin\java</executable>
<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=8080 --webroot="%BASE%\war" --prefix=/jenkins</arguments>

Jenkins / Hudson environment variables

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?
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.
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.
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.
I tried /etc/profile, ~/.profile and ~/.bash_profile and none of those worked. I found that editing ~/.bashrc for the jenkins slave account did.
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.
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.
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 was the only thing that worked (i.e. .profile, .bash* didn't work).
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.
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.
Jenkins also supports the format PATH+<name> to prepend to any variable, not only PATH:
Global Environment variables or node Environment variables:
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.
You may also use the syntax PATH+WHATEVER=/something to prepend /something to $PATH
Or the java docs on EnvVars here.
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.
On my Ubuntu 13.04, I tried quite a few tweaks before succeeding with this:
Edit /etc/init/jenkins.conf
Locate the spot where "exec start-stop-server..." begins
Insert the environment update just before that, i.e.
export PATH=$PATH:/some/new/path/bin
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 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.
I tried all the things from above - didn't work for me.
I found two solution (both for SSH-Slave)
Go to the slave settings
Add a new environment variable
PATH
${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"
}
}
}
}
}
On Ubuntu I just edit /etc/default/jenkins and add source /etc/profile at the end and it works to me.
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>
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.
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.
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>
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.
If your pipeline is executed on the remote node that is connected via SSH, then actually Jenkins runs agent application that performs incoming actions.
By default zsh shell is used, not the bash (my Jenkins has version 2.346.3).
Furthermore jenkins-agent runs non-login shell which makes default PATH values even if you put some configuration to .zshrc. It will be skipped.
My choice is to put the following shebang at a script start
#!/bin/bash -l
-l option makes bash to run in the login mode and in this case bash performs configurations specified in /etc/profile and ~/.bash_profile.
If you run script in Jenkins pipeline it will look like:
steps {
sh '''#!/bin/bash -l
env
'''
}

Resources