如何在Jenkins中将环境变量传递到Docker容器中?

3
我在一个Docker容器(node:latest)中运行我的Jenkins构建。但是在容器中没有定义环境变量:
$GIT_BRANCH, $GIT_COMMIT

我遇到了这个错误:

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)

我有很多变量需要传递到容器中。如何在Jenkins中实现这一点?

我正在寻找一种解决方案,可以继承Jenkins进程和我的主机/ Docker中存在的所有环境变量。

这是我的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个回答

0

也许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'
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接