1

I am struggling with this issue inside my declarative pipeline. I am trying to use python's docker image as my agent and run some scripts and I am receiving warning:

Warning: JENKINS-41339 probably bogus PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools; perhaps you meant to use ‘PATH+EXTRA=/something/bin’

I do not have access to Jenkins server global variables configuration. I am using one of the Debian slaves.

I have read other issues about this problem stackoverflow/43987005 and stackoverflow/44378221. Also discussion on Jenkins forum and Jenkins official documentation.

This is code I am executing:

//  https://www.jenkins.io/doc/book/pipeline/syntax/
pipeline {
  environment {
    PATH = "/bin/sh:$PATH"
  }
  agent {
    docker {
      image "python:3.9"
    }
  }
  stages {
    stage('Test') {
      steps {
        withEnv(["PATH+EXTRA=$PATH"]) {
          sh "PATH is: $PATH"
        }
      }
      post {
        cleanup {
          cleanWs()
        }
      }
    }
    stage('Build') {
      steps {
        echo "'Let's build"
      }
    }
    stage('Deploy') {
      steps {
        echo "'Let's deploy"
      }
    }
  }
}

Problem appeared after I wanted to implement docker image as agent and by changing environment section inside my pipeline still haven't fixed my problem. I was trying also these alternatives:

PATH = "/usr/local/bin:$PATH"
PATH = "/usr/local/bin:${env.PATH}"

I also went to Configure, section Prepare an environment for the run and filled up Properties Content input but I still didn't receive $PATH content into my job. I am still receiving global value of $PATH. I also unchecked the Keep Jenkins Environment Variables checkbox.

The server I am using has Environment Injector Plugin plugin installed.

2
  • Looks like this question is already asked and answered over on stack overflow: stackoverflow.com/questions/43987005/…
    – jayhendren
    Commented Jan 11, 2021 at 23:58
  • @jayhendren I was reading that, even mentioned it at the beginning of my answer, but unfortunately nothing helped me so far.
    – dubaksk
    Commented Jan 12, 2021 at 7:28

2 Answers 2

2

This post explains that/why environment variables don't propagate to Docker agent.

If you need a custom container, you could define path and whatever else as layers on top of base image in a Dockerfile. Then using that image in pipeline would be what I think is usual way of doing it. You can build that image outside of Jenkins, but it's also possible to build container from within pipeline too if you want (get Dockerfile from SCM or some other use case).

In the context of your question, I think you can still use base image, and if you know full path you'd like to set, do that at container (agent) startup with args. But $PATH variable won't be available at host to be resolved within container at startup. If you must dynamically set path in container, could adjust it in shells.

pipeline {
    agent {
        docker { 
            image 'python:3.9'
            // cannot resolve $PATH var in container (becasue HOST does not know what it is).
            args '-e PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/i/know/my/full/path/ahead/of/time'
        }
    }
    stages {
        stage('Test') {
            steps {
                
                // as set by args above. Applies to all shells
                sh 'echo $PATH'
                
                // You can also adjust it within container in each shell
                sh '''
                    export PATH=/even/more/path:$PATH
                    echo $PATH
                   '''

                // But this is back to what's in args in a new shell instance
                sh 'echo $PATH'
            }
        }
    }
}
3
  • Thank you @napovi for the answer, I ran your example with using args but still the same error.
    – dubaksk
    Commented Jan 15, 2021 at 11:51
  • Still getting 'bogus path' error? That would be odd, because you are not altering any paths in Jenkins/Pipeline in my example. Only passing absolute path to be set in the container or prepend it to your command in a sh block without string interpolation (single quotes). Could you post what you got now in your pipeline?
    – apo
    Commented Jan 15, 2021 at 16:35
  • I took your example and ran it inside provided Jenkins server and I got the same bogus PATH error. It is very weird indeed.
    – dubaksk
    Commented Jan 15, 2021 at 21:18
0

If you are not using Docker using in built support in pipeline and instead, if you are setting up docker agent manually from "Manage Jenkins -> Clouds -> Docker template", then below solution will work as provided in this Stackoverflow post.

pipeline {
  agent { label 'docker-custom-image' }
  environment {
        JAVA_HOME='/opt/java-se-8u44-ri'
        PATH = "$JAVA_HOME/bin/:${env.PATH}"
  }
  stages {
    stage ('build') {
      steps {
        echo "PATH is: ${env.PATH}"
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.