Joao  Vitorino
Joao Vitorino

Reputation: 3256

Java get the hostname and other env var through jenkins

I'm using jenkins to build my projects and i can't get the hostname with java

According to this java doc I wrote the follow lines

            System.out.println(System.getenv("MACHINE"));
            System.out.println(System.getenv("HOSTNAME"));

These are environment variables. BUt the output on build is:

 null
 null

What I'm doing wrong?

Upvotes: 0

Views: 3002

Answers (2)

Joao  Vitorino
Joao Vitorino

Reputation: 3256

I solve the problem using the Jenkins Enviromenment Script Plugin

Wiht this plugin I can just run a shell script code and save the values as var to use in the build.

host=$(hostname)
flavor=$(cat /etc/os-release |grep ^'NAME=' |cut -d'=' -f2)

Upvotes: 0

Amit
Amit

Reputation: 1006

If you are using the Jenkins script console, you can try:

println InetAddress.getLocalHost()

There is some very good discussion on this topic here.

Upvotes: 1

Related Questions