2017-10-19 54 views
0

在我聲明式的Jenkinsfile中,我試圖計算一些值,然後將它們傳遞給maven。看起來很簡單,我無法做到這一點。聲明式Jenkinsfile中的動態變量導致NPE

這裏是我的Jenkinsfile的相關部分:

def port = 1000 as Integer 
def mocksport = 0 as Integer 
def safebranch='unknown' 

pipeline { 
    stages { 
    stage('Compile'){ 
     steps { 
      script { 
       safebranch=env.BRANCH_NAME.toLowerCase().replaceAll("[-_/]", ""); 
       for (int i=0; i<safebranch.length(); i++) { 
       port = (port as Integer) + (Character.getNumericValue(safebranch.charAt(i)) as Integer); 
       } 
       port = (port as Integer) % 99 + 9000; 
       mocksport = (port as Integer) + 1; 
       echo "Application will be deployed on port ${port}" 
       echo "Mocks will be deployed on port ${mocksport}"​​​​​​​​​​​​​ 
      } 
      sh "mvn clean install -Dmaven.test.failure.ignore=true -T 1C -Drancher.port=${port} -Drancher.mocks.port=${mocksport} -Drancher.tag=${safebranch}​" 
     } 
     } 
    } // stages 
} // pipeline 

這個版本是最接近我能得到一個有效的解決方案。至少腳本塊被執行,呼應值是正確的:在日誌中我看到:

[Pipeline] [Compile] echo 
[Compile] Application will be deployed on port 9065 
[Pipeline] [Compile] echo 
[Compile] Mocks will be deployed on port 9066 

我現在收到這個異常,可能與常規暫時無法找到腳本塊上下文之外的變量:

java.lang.NullPointerException: Cannot get property '​​​​​​​​​​​​​' on null object 
    at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60) 
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174) 
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456) 
    at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:284) 
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:286) 
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29) 
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) 

我試着在流水線下的全局環境部分聲明變量,並使用「env」來訪問它們。前綴,但在這種情況下,似乎腳本塊不允許更新值(=我得到1000和0作爲回聲的結果)。

我該如何做這項工作?

獎金問題:奇怪的是,變量似乎總是作爲字符串處理。如果我在每次變量使用時都沒有將轉換添加到Integer中,它不起作用(端口變量在每次迭代中展開:1000,10001012,100010122343等)。我可以聲明和使用整數變量嗎?

回答

1

我沒有看到你的變量的範圍問題,因爲你已經設置了它們。它們應該在script{}區塊之外提供。錯誤消息也沒有真正指向範圍問題。

我把你粘貼的確切代碼,添加了一個代理聲明,併爲「safebranch」創建了一個值,它的運行方式與我所期望的完全相同(當然,沒有找到mvn命令)。

Running shell script 
+ mvn clean install -Dmaven.test.failure.ignore=true -T 1C -Drancher.port=9011 -Drancher.mocks.port=9012 -Drancher.tag=BRANCH 
/home/jenkins/jenkins/workspace/[email protected]/durable-6849aecb/script.sh: 2: /home/jenkins/jenkins/workspace/[email protected]/durable-6849aecb/script.sh: mvn: not found 

我甚擺脫所有to Integer類型強制的,它工作正常。也許問題實際上是你沒有發佈的腳本的一部分。這是我的確切腳本。嘗試插入它,看看它是否運行:

def port = 1000 
def mocksport = 0 
def safebranch='unknown' 

pipeline { 
    agent any 
    stages { 
    stage('Compile'){ 
     steps { 
      script { 
       safebranch="BRANCH" 
       for (int i=0; i<safebranch.length(); i++) { 
       port = (port) + (Character.getNumericValue(safebranch.charAt(i))); 
       } 
       port = (port) % 99 + 9000; 
       mocksport = (port) + 1; 
       echo "Application will be deployed on port ${port}" 
       echo "Mocks will be deployed on port ${mocksport}" 
      } 
      sh "mvn clean install -Dmaven.test.failure.ignore=true -T 1C -Drancher.port=${port} -Drancher.mocks.port=${mocksport} -Drancher.tag=${safebranch}" 
     } 
     } 
    } // stages 
} // pipeline 
+0

我把你的版本,並逐漸加回所有我的其他步驟,平行塊等......現在它的工作! :)不知道是什麼導致了這個問題,但你的重寫修復了它。非常感謝。 – lbndev