How to add PATH to slave using Slave SetupPlugin?

I have 2 RHEL installations in a Master / Slave configuration using Jenkins ver. 1.609.2

The slave runs through the SSH Slaves 1.10 plugin.

I am trying to use Slave Setup Plugin v 1.9 to install the tools that my slave machine will need to run assemblies. In particular, I am installing sqlplus.

Here is the script that I run to try installing sqlplus:

if command -v sqlplus >/dev/null; then echo "sqlplus already setup. Nothing to do." else #Create directory for sqlplus and unzip it there. mkdir /jenkins/tools/sqlplus tar -xvf sqlplussetup/instantclient-basiclite-linux.x64-12.1.0.2.0.tar.gz -C /jenkins/tools/sqlplus || { echo 'unzip failed' ; exit 1; } tar -xvf sqlplussetup/instantclient-sqlplus-linux.x64-12.1.0.2.0.tar.gz -C /jenkins/tools/sqlplus || { echo 'unzip failed' ; exit 1; } cd /jenkins/tools/sqlplus/instantclient_12_1 #Create links for the Oracle libs ln -s libclntsh.so.12.1 libclntsh.so || { echo 'Could not create link' ; exit 1; } ln -s libocci.so.12.1 libocci.so || { echo 'Could not create link' ; exit 1; } #Add two lines to .bashrc only if they don't already exist. Export LD_LIBRARY_PATH and add sqlplus to PATH. grep -q -F 'export LD_LIBRARY_PATH=/jenkins/tools/sqlplus/instantclient_12_1:$LD_LIBRARY_PATH' /home/jenkins/.bashrc || echo 'export LD_LIBRARY_PATH=/jenkins/tools/sqlplus/instantclient_12_1:$LD_LIBRARY_PATH' >> /home/jenkins/.bashrc grep -q -F 'export PATH=$PATH:/jenkins/tools/sqlplus/instantclient_12_1' /home/jenkins/.bashrc || echo 'export PATH=$PATH:/jenkins/tools/sqlplus/instantclient_12_1' >> /home/jenkins/.bashrc #Export variables so they can be used right away export LD_LIBRARY_PATH=/jenkins/tools/sqlplus/instantclient_12_1:$LD_LIBRARY_PATH export PATH=$PATH:/jenkins/tools/sqlplus/instantclient_12_1 echo "sqlplus has been setup." fi 

This script runs successfully and everything works until I try to run the build and execute the sqlplus command. Build failed because sqlplus not a recognized command.

My main question is: What is the correct way to automatically add an environment variable when the slave starts?

Please note that I am looking for a way to automate . I do not want to go into the configuration screen for my slave device, check the checkbox and specify the environment variable. This is counterproductive to what I am trying to achieve, which is a subordinate that you can immediately use to build after connecting.


I really understand why my script is not working. When Jenkins starts the slave, it first makes an SSH connection and then launches my setup script using the command

 /bin/sh -xe /jenkins/tmp/hudson8035138410767957141.sh 

Where the contents of hudson8035138410767957141.sh is my script on top. Obviously, export will not work . I was hoping adding the export to the .bashrc would work, but that would not work. I think this is because this script is executed after the ssh connection is established and therefore .bashrc has already been read.

The problem is that I cannot find a way around this limitation.

+5
source share
1 answer

Bash does not read any of its startup files ( .bashrc , .profile , etc.) for non-interacting shells for which the --login parameter is not explicitly specified - why exporting does not work.

So, solution β€œA” is to preserve the bashrc magic that you offer above and add the --login parameter, changing the first line in the build step to

 #!/bin/bash --login <your script here> 

The explicit shebang on the first line will also prevent the excessive debug output that you get from the default option -x (see console snippet above).

An alternative solution to "B" uses the fact that bash will be the source of any script whose name is specified in $BASH_ENV (if this variable is defined and the file exists). Define this variable globally in your subordinate properties (for example, set in /jenkins/tools/setup.sh) and add the export as necessary during the configuration of the slave. Each step of building a bash shell will read the settings then.

In solution "B," you do not need to use the --login , and you do not need to mess up .bashrc . However, the β€œBASH_ENV” function is only active when bash is running in bash mode. ”When Jenkins launches a shell through sh , bash tries to emulate a historical sh that does not have this function. So, also for B, you need shebang:

 #!/bin/bash <your script here> 

But you still need to get rid of the trace output, which is usually too much in production settings.

+1
source

All Articles