PATH variable is overwritten by jnlp container

Hello,

We are using Jenkins with kubernetes plugin.
In Jenkins, various agents are set up, all with a custom image. In one image, for example, golang is installed and the PATH is adjusted accordingly:

ENV GOPATH /home/jenkins/go
ENV PATH $PATH:/usr/local/go/bin:$GOPATH/bin

Now we have noticed that in the pipeline $PATH from the golang-image is overwritten with the $PATH from the jnlp container.

Pipeline:

pipeline {
    agent {
        kubernetes {
		    inheritFrom 'golang'
		    defaultContainer 'go'
        }
    }

    stages {
        stage ('test'){
            steps {
                sh "echo ${PATH}"
                sh "echo ${env.PATH}"
                println Path
                sh "printenv"
            }
        }
    }
}

Result from job log:

+ echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[Pipeline] sh
+ echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[Pipeline] echo
/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[Pipeline] sh
+ printenv
....
PATH=/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin
....

Similar issue we have with a java agent. We installed two java-versions and set im Dockerfile:

ENV JAVA_HOME_18 /usr/lib/jvm/jdk-18
ENV JAVA_HOME_19 /usr/lib/jvm/jdk-19

In pipeline:

sh "echo $env.JAVA_HOME_19"

has no result.

What is wrong?

Thanks in advance!
Marco

Okay, I got it now.
We are migrating from ssh-agents to kubernetes agents.
Before, we were able to use each environment variable from agent (backed into image).

How can we still use the environment of the agents?

What you could try is to run two containers in one pod, jnlp-agent and your golang stuff separately. jnlp-agent is pretty cut down and meant only to run the agent itself.

@saper sorry for late reply.

The jnlp container is created automatically by kubernetes plugin.

We are facing more and more issues… most of them are related to PATH.

While running the pipeline we cannot add something to PATH like

PATH=PATH:/path/to/some/bin

This is for example added es ENV in the Dockerfile but this path is not populated.

Echo on PATH has a different output as printev for example.

What is wrong?

Here one example. $PATH is configured as environment variable in pod.

Pipeline:

pipeline {
    agent {
        kubernetes {
  		    inheritFrom 'agent-build'
  		    defaultContainer 'build'
        }
    }
    stages {
        stage('TEST') {
            steps {
                echo "Hello world"
                echo "-------------"
                sh 'echo $PATH'
                echo "-------------"
                sh "PATH=$PATH:/some/path/to/bin"
                sh "echo $PATH"
                echo "-------------"
                sh """
                    PATH=$PATH:/some/path/to/bin
                    echo $PATH
                """
            }
        }
    }
}

Output:

[Pipeline] stage
[Pipeline] { (TEST)
[Pipeline] echo
Hello world
[Pipeline] echo
-------------
[Pipeline] sh
+ echo /usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin
/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin
[Pipeline] echo
-------------
[Pipeline] sh
+ PATH=/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin:/some/path/to/bin
[Pipeline] sh
+ echo /usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin
/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin
[Pipeline] echo
-------------
[Pipeline] sh
+ PATH=/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin:/some/path/to/bin
+ echo /usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin
/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/jenkins/go/bin
[Pipeline] }
[Pipeline] // stage
[Pipeline] }