2017-09-24 135 views
0

我想在我的詹金斯管道設置名稱爲「TEST_CONFIG_ROOT」的環境變量,我指的例子在這裏:當我執行無法設置環境變量jenkinsfile

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腳本如下export命令,不過這也是沒有幫助。

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

查找以下日誌的片段執行流水線作業時:

[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/[email protected]p/withMaven00e87287/pipeline-maven-spy.jar" -Dorg.jenkinsci.plugins.pipeline.maven.reportsFolder="/usr/home/nodename/Jenkins/workspace/workspace/[email protected]p/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/[email protected]p/withMaven00e87287/maven-spy-20170924-225639-49.log ... 
[INFO] Scanning for projects... 
+1

歡迎來到StackOverflow,親愛的先生stackoverlfow。請重新格式化您的代碼示例以獲得更好的可讀性。 – StephenKing

回答

1

所以,這是什麼工作在我的情況下,只是提及其他人的參考。

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' 
     } 
} 
2

我會說你是混合了聲明輸送管線用腳本的人(見文檔Pipeline Syntax)。

下面的代碼片段所屬的聲明式的,但你有一個腳本之一:

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

隨着劇本的管道,它實際上是一個更容易一些:

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

謝謝@StephenKing,我按照上面的說法試過,但仍然沒有運氣。 – stackoverflow

+0

然後我會說你搞砸了。剝離管道到最小版本,看看它是否有效。 – StephenKing