How to override the environment variables for Jenkins slave when adding new slave to the Jenkins master?

1,260 views
Skip to first unread message

praveen kumar jogi

unread,
Jun 23, 2016, 6:14:20 PM6/23/16
to Jenkins Users

Configured automatic slaves for couple jobs on Jenkins using ec2 plugin. I need to override couple of environment variables for the slave before connecting to master. Here is the init script that used to run before it connect as slave. What am I doing wrong. Can someone please help me?


PROBLEM: I'm able to launch a slave,But unable to set environment variables for that slave.


GOAL: Install maven-3.2.5 (set home directory to /opt/apache-maven-3.2.5),

           Environment variables: (name: DISPLAY, value: :1), (name: PATH, value: $PATH:/usr/local/bin)



#!/bin/sh

sudo mkdir /opt/apache-maven-3.2.5
if [ -d /opt/apache-maven-3.2.5 ]; then
    if [ ! -f /tmp/apache-maven-3.2.5-bin.tar.gz ]; then
        wget -nd -O /tmp/apache-maven-3.2.5-bin.tar.gz https://archive.apache.org/dist/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz
    fi
    tar -xvf /tmp/apache-maven-3.2.5-bin.tar.gz
    sudo mv apache-maven-3.2.5 /opt/
    sudo chown -R jenkins:jenkins /opt/apache-maven-3.2.5
fi
PATH=$PATH:/usr/local/bin
DISPLAY=:1
MAVEN_HOME=/opt/apache-maven-3.2.5


Jenkins version:1.656 Amazon EC2 plugin:1.31

Mark Waite

unread,
Jun 23, 2016, 11:15:07 PM6/23/16
to jenkins...@googlegroups.com
Jenkins has the ability to install tools for you automatically, including Maven.  If a job needs that tool on a slave which does not have the tool yet, Jenkins installs the tool and initializes the environment so the tool can be used.

I've used that technique successfully with various JDK versions, maven versions, and ant versions.

If you still want to use your technique, then I've asked a few questions inside the script.

Mark Waite

On Thu, Jun 23, 2016 at 4:14 PM praveen kumar jogi <jogi.prav...@gmail.com> wrote:

Configured automatic slaves for couple jobs on Jenkins using ec2 plugin. I need to override couple of environment variables for the slave before connecting to master. Here is the init script that used to run before it connect as slave. What am I doing wrong. Can someone please help me?


PROBLEM: I'm able to launch a slave,But unable to set environment variables for that slave.


GOAL: Install maven-3.2.5 (set home directory to /opt/apache-maven-3.2.5),

           Environment variables: (name: DISPLAY, value: :1), (name: PATH, value: $PATH:/usr/local/bin)



#!/bin/sh

sudo mkdir /opt/apache-maven-3.2.5
if [ -d /opt/apache-maven-3.2.5 ]; then

Isn't the sense of that conditional incorrect?  I think you want to take the actions below only if the directory does not exist. 

    if [ ! -f /tmp/apache-maven-3.2.5-bin.tar.gz ]; then
        wget -nd -O /tmp/apache-maven-3.2.5-bin.tar.gz https://archive.apache.org/dist/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz
    fi
    tar -xvf /tmp/apache-maven-3.2.5-bin.tar.gz
    sudo mv apache-maven-3.2.5 /opt/
    sudo chown -R jenkins:jenkins /opt/apache-maven-3.2.5
fi
PATH=$PATH:/usr/local/bin
Don't you want to place your Maven installation directory early in the PATH so that the shell will find your freshly installed maven? 

DISPLAY=:1
MAVEN_HOME=/opt/apache-maven-3.2.5


Jenkins version:1.656 Amazon EC2 plugin:1.31

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c7d0d440-7103-4230-ab0b-6cce01b2aec6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

praveen kumar jogi

unread,
Jun 24, 2016, 1:54:42 PM6/24/16
to jenkins...@googlegroups.com
Yes. We can install a tool on a Jenkins slave as part of the job. I wanted to install the tool on slave before connect it as slave to the master so then there is no need to install the tool on slave. Regarding the script, I'll change the script accordingly as you specified. Thank you so much for comments.

Regards,
Praveen

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/XXoVm8xJogk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAO49JtGB4r0g372cK4-73eoaTrnUoYJpcTgDf6pSV9Qkn7Dxmw%40mail.gmail.com.

praveen

unread,
Jun 24, 2016, 1:55:07 PM6/24/16
to Jenkins Users
Yes. We can install a tool on a Jenkins slave as part of the job. I wanted to install the tool on slave before connect it as slave to the master so then there is no need to install the tool on slave. Regarding the script, I'll change the script accordingly as you specified. Thank you so much for comments.

Regards,
Praveen

Mark Waite

unread,
Jun 26, 2016, 8:23:14 AM6/26/16
to jenkins...@googlegroups.com
On Thu, Jun 23, 2016 at 9:14 PM praveen <jogi.prav...@gmail.com> wrote:
Yes. We can install a tool on a Jenkins slave as part of the job. I wanted to install the tool on slave before connect it as slave to the master so then there is no need to install the tool on slave. Regarding the script, I'll change the script accordingly as you specified. Thank you so much for comments.


I am interested why you prefer to install the tool as part of provisioning the slave when Jenkins will do it for you automatically.  When Jenkins installs it automatically, it allows you to manage multiple versions of the same tool, and you can control which installer is used by the labels assigned to the slave.  That seems easier, more capable, and more reliable (to me) than writing your own scripts to install and manage tools needed by your build.

I could see choosing not to have Jenkins manage a tool if it is provided by the operating system (git, for example, is a common part of many Linux operating systems).  In that case, you might use the operating system package management system to install the tool rather than having Jenkins install it for you.

Mark Waite
 
Regards,
Praveen

On Thursday, June 23, 2016 at 3:14:20 PM UTC-7, praveen wrote:

Configured automatic slaves for couple jobs on Jenkins using ec2 plugin. I need to override couple of environment variables for the slave before connecting to master. Here is the init script that used to run before it connect as slave. What am I doing wrong. Can someone please help me?


PROBLEM: I'm able to launch a slave,But unable to set environment variables for that slave.


GOAL: Install maven-3.2.5 (set home directory to /opt/apache-maven-3.2.5),

           Environment variables: (name: DISPLAY, value: :1), (name: PATH, value: $PATH:/usr/local/bin)



#!/bin/sh

sudo mkdir /opt/apache-maven-3.2.5
if [ -d /opt/apache-maven-3.2.5 ]; then
    if [ ! -f /tmp/apache-maven-3.2.5-bin.tar.gz ]; then
        wget -nd -O /tmp/apache-maven-3.2.5-bin.tar.gz https://archive.apache.org/dist/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz
    fi
    tar -xvf /tmp/apache-maven-3.2.5-bin.tar.gz
    sudo mv apache-maven-3.2.5 /opt/
    sudo chown -R jenkins:jenkins /opt/apache-maven-3.2.5
fi
PATH=$PATH:/usr/local/bin
DISPLAY=:1
MAVEN_HOME=/opt/apache-maven-3.2.5


Jenkins version:1.656 Amazon EC2 plugin:1.31

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

praveen

unread,
Jun 27, 2016, 8:02:45 PM6/27/16
to Jenkins Users
I've tried to install the maven automatically, But It was not working. Jenkins master configuration is like this install - install maven automatically (version 3.0.5), However slave(on-demand) unable to install maven on the target directory(slave was spun up with the AWS ec2 plugin). My builds are failing with the maven error.


ERROR:

16:51:11 java.io.IOException: Failed to install https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.0.5/apache-maven-3.0.5-bin.zip to /home/csbuild/build/apache-maven-3.0.5


On Thursday, June 23, 2016 at 3:14:20 PM UTC-7, praveen wrote:
Auto Generated Inline Image 1

Mark Waite

unread,
Jun 27, 2016, 8:23:08 PM6/27/16
to Jenkins Users
That's a surprising screen shot that you included.  The Maven installations section on my Jenkins 1.651.3 installation does not have a field for MAVEN_HOME.  I believe Jenkins computes the correct value for MAVEN_HOME entirely on its own in my case.

Any idea what's different between your environment (which prompts for a MAVEN_HOME) and mine (which does not)?

Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

praveen

unread,
Jun 28, 2016, 2:17:50 AM6/28/16
to Jenkins Users


Jenkins system configuration also consisting of this section. on-demand slave that I've configured not using either of this configuration.

Jenkins version 1.656



On Thursday, June 23, 2016 at 3:14:20 PM UTC-7, praveen wrote:
Auto Generated Inline Image 1

Björn Pedersen

unread,
Jun 28, 2016, 3:33:21 AM6/28/16
to Jenkins Users
Hi,

can you check if the necessary parent dirs are there on the slave and have the correct access permissions for the jenkins user?

 /home/csbuild/build

And can the slave reach the
 https://repo.maven.apache.org ? If not, then install from apache would fail.

Björn

praveen

unread,
Jun 28, 2016, 5:18:37 PM6/28/16
to Jenkins Users
@Mark. It's the issue with the permission. Thank you so much for your help.

~Regards
Praveen


On Thursday, June 23, 2016 at 3:14:20 PM UTC-7, praveen wrote:

Baptiste Mathus

unread,
Jun 29, 2016, 3:07:16 AM6/29/16
to jenkins...@googlegroups.com

Side note: don't install tools downloading directly from the provider.
It's nice to use as a PoC, but if for some reason those services go down for a while, you're gonna have issues.
There have been things about that king oh thing in the past (was it the npm repo?) explaining that.
You want to set up a binary repository you own from which you'll download those.

Cheers

--

praveen

unread,
Jun 29, 2016, 12:01:09 PM6/29/16
to Jenkins Users, m...@batmat.net

I prefer to install tool in the configuration rather than script to get the binary from an URL. The thing is I didn't get any idea to install the tool as part of spin up the dynamic instance(ec2 plugin not has any configuration for the tool installation).

Baptiste Mathus

unread,
Jun 30, 2016, 1:09:39 AM6/30/16
to jenkins...@googlegroups.com

I didn't say you have to script it. Using a custom URL is a standard feature of the Tools management in Jenkins.

About your question, note that Tools management isn't related to the cloud provider you're using.

Cheers

Reply all
Reply to author
Forward
0 new messages