How to pass env vars into the docker container in Jenkins?

Viewed 276

I run my Jenkins build inside a docker container (node:latest).

But the enviroments variables are not defiend in the container:

$GIT_BRANCH, $GIT_COMMIT

So I got this error:

GitHub has been notified of this commit’s build result

groovy.lang.MissingPropertyException: No such property: GIT_BRANCH for class: groovy.lang.Binding

    at groovy.lang.Binding.getVariable(Binding.java:63)

I have a lot of variables I need to pass into the container. how to do that with Jenkins?

I looking for solution that inherit all environment variables that exist in Jenkins process and my host machine/docker

Here my Jenkinsfile:

throttle(['throttleDocker']) {
  node('docker') {
    wrap([$class: 'AnsiColorBuildWrapper']) {
      try{
        docker.image('node:latest').inside {
          stage('Checkout SCM'){
            checkout scm
          }
          stage('PS'){ 
              sh 'node -v'
              sh 'ls'
          }
          stage('Verify Branch') {
                echo "$GIT_BRANCH"
                echo "$GIT_COMMIT"
          }
          stage('Build'){
              sh "npm run build"
              sh 'ls'
          }
          stage('Test'){
              sh 'echo "Test Stage inside container."'
          }
        }
1 Answers

Maybe this

node ('linux-slave') {
    withEnv(['PATH=/usr/local/Cellar/coreutils/8.25/libexec/gnubin:/usr/local/Cellar/gnu-sed/4.2.2/libexec/gnubin:/Users/fbelzunc/cloudbees/support/support-shinobi-tools:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home//bin:/Users/fbelzunc/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:/usr/local/sbin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.6.5/bin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/beaker-1.16.0/bin:/usr/local/Cellar/ruby/2.1.0/bin/:/path/testing']) {
        docker.image('maven:3.3.3-jdk-8').inside {
          sh 'echo $PATH'
          sh 'mvn --version'
        }
    }
    sh 'echo $PATH'
}
Related