【问题标题】:unable to set environment variable in jenkinsfile无法在 jenkinsfile 中设置环境变量
【发布时间】:2018-03-05 20:14:21
【问题描述】:

我正在尝试在我的 Jenkins 管道中设置一个名为“TEST_CONFIG_ROOT”的环境变量,这里指的是示例:

https://jenkins.io/doc/book/pipeline/jenkinsfile/#working-with-the-environment

但是,当我执行我的测试时,似乎没有设置 env 变量,因为我的测试仍然抱怨它没有得到它应该从 env 获得的变量“TEST_CONFIG_ROOT”的值。

请在下面查看我的 jenkinsFile:

node('node1'){


        def buildInput;

      echo 'Deploying my build'
     if(!params.buildName) {
         buildInput = input(
                 id: 'userInput', message: 'What is the build name?', parameters: [
                 [$class: 'StringParameterDefinition', defaultValue: 'abcd-1', description: 'Environment', name: 'buildName']
         ])
      }
      buildToUse = params.buildName ? params.buildName : buildInput;
      echo ("Env: "+buildToUse);



    if ( "${params.buildParam}" == 'prequal' || !params.buildParam ){
        stage('Prequal') {


        }
    }


    node('nodename'){

        if ( "${params.buildParam}" == 'test' || !params.buildParam ){
            withMaven(
                    maven: 'M2Slave',
                    mavenSettingsConfig: 'MavenSettingsXML',
                    mavenLocalRepo: '${HOME}/.m2/repository') {

                stage('Test') {
                    echo 'Testing my build'
                    echo " my work space is ${env.WORKSPACE}"
                    checkout scm

                    environment {
                        TEST_CONFIG_ROOT = '${env.WORKSPACE}/testsE2e/src/main/resources'

                    }

  dir ( 'testsE2e'){
 sh 'mvn clean verify surefire-report:report surefire-report:failsafe-report-only -Dtestngxml=testng.xml -Dhttp.proxyHost=proxy02.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy02.com -Dhttps.proxyPort=8080  -Djavax.xml.accessExternalSchema=all'
                        }

                }

            }
        }

    }

}

我也尝试使用下面的 shell 脚本执行导出命令,但这也无济于事。

echo " my work space is ${env.WORKSPACE}"
sh  'export TEST_CONFIG_ROOT="${WORKSPACE}/testsE2e/src/main/resources"'

在管道作业执行时的日志sn-p下面找到:

[Pipeline] echo
 my work space is /usr/home/nodename/Jenkins/workspace/workspace/someName-RCZ4A4MA3GRP4PSVYT4HTVVIKU4J7TW2667CKTZU22CHR2CBEM5Q
[Pipeline] dir
Running in /usr/home/nodename/Jenkins/workspace/workspace/someName-RCZ4A4MA3GRP4PSVYT4HTVVIKU4J7TW2667CKTZU22CHR2CBEM5Q/testsE2e
[Pipeline] {
[Pipeline] sh
[testsE2e] Running shell script
+ mvn clean verify surefire-report:report surefire-report:failsafe-report-only -Dtestngxml=testng.xml -Dhttp.proxyHost=proxy02.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy02.com -Dhttps.proxyPort=8080 -Djavax.xml.accessExternalSchema=all
----- withMaven Wrapper script -----
Picked up JAVA_TOOL_OPTIONS: -Dmaven.ext.class.path="/usr/home/nodename/Jenkins/workspace/workspace/someName-RCZ4A4MA3GRP4PSVYT4HTVVIKU4J7TW2667CKTZU22CHR2CBEM5Q@tmp/withMaven00e87287/pipeline-maven-spy.jar" -Dorg.jenkinsci.plugins.pipeline.maven.reportsFolder="/usr/home/nodename/Jenkins/workspace/workspace/someName-RCZ4A4MA3GRP4PSVYT4HTVVIKU4J7TW2667CKTZU22CHR2CBEM5Q@tmp/withMaven00e87287" 
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)
Maven home: /opt/maven/apache-maven-3.3.9
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: /opt/oracle/jdk1.8.0_111/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-573.7.1.el6.x86_64", arch: "amd64", family: "unix"
[jenkins-maven-event-spy] INFO generate /usr/home/nodename/Jenkins/workspace/workspace/someName-RCZ4A4MA3GRP4PSVYT4HTVVIKU4J7TW2667CKTZU22CHR2CBEM5Q@tmp/withMaven00e87287/maven-spy-20170924-225639-49.log ...
[INFO] Scanning for projects...

【问题讨论】:

  • 欢迎来到 StackOverflow,亲爱的 stackoverlfow 先生。请重新格式化您的代码示例以获得更好的可读性。

标签: jenkins environment-variables jenkins-pipeline


【解决方案1】:

我会说您将声明性管道与脚本化管道混为一谈(请参阅文档中的 Pipeline Syntax)。

以下代码 sn-p 属于声明性代码,但您有一个脚本代码:

environment {
    TEST_CONFIG_ROOT = "${env.WORKSPACE}/testsE2e/src/main/resources"
}

使用脚本化的管道,实际上要容易一些:

env.TEST_CONFIG_ROOT = "${env.WORKSPACE}/testsE2e/src/main/resources"

【讨论】:

  • 谢谢@StephenKing,我按照你上面说的尝试过,但还是没有成功。
  • 那我会说你搞砸了。将您的管道精简为最小版本,看看它是否可以工作。
  • 通过用双引号替换单引号来修复字符串插值。对不起那个错误。
【解决方案2】:

所以,这就是我的情况,只是提一下供其他人参考。

withEnv(["TEST_CONFIG_ROOT=${env.WORKSPACE}/testsE2e/src/main/resources"]) {
      dir ( 'testsE2e'){
     sh 'mvn clean verify surefire-report:report surefire-report:failsafe-report-only -Dtestngxml=testng.xml -Dhttp.proxyHost=proxy02.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy02.com -Dhttps.proxyPort=8080  -Djavax.xml.accessExternalSchema=all'
      }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多