2017-06-01 81 views
1

我有一個declarative流水線階段像下面,詹金斯管道建設使用Jenkinsfile沒有失敗

stage('build') { 
    steps { 
     echo currentBuild.result 
     script { 
       try { 
        bat 'ant -f xyz\\build.xml' 
       } catch (err) { 
        echo "Caught: ${err}" 
        currentBuild.result = 'FAILURE' 
       } 
      } 
     echo currentBuild.result 
    } 
} 

我期待,因爲生成與下面的消息未能管道失敗。

BUILD FAILED 
C:\...\build.xml:8: The following error occurred while executing this line: 
C:\...\build.xml:156: The following error occurred while executing this line: 
C:\...\build.xml:111: Problem creating jar: C:\...\xyz.war (The system cannot find the path specified) (and the archive is probably corrupt but I could not delete it) 

currentBuild.result在我打印它的時候都是空的。
螞蟻叫錯了嗎?
爲什麼返回狀態不是通過管道自動捕獲的?
螞蟻通話可否不返回失敗狀態?

我試過catchError而不是try..catch,仍然沒有捕獲到構建失敗。

catchError { 
    bat 'ant -f xyz\\build.xml' 
} 
+1

看起來像螞蟻不會傳播錯誤到管道。也許這可以幫助https://stackoverflow.com/questions/22395597/propagating-exit-code-from-execd-batch-file-back-to-ant。 – haschibaschi

+0

謝謝haschibaschi,您評論中的頁面給了我一些嘗試的指針,即'echo%ERRORLEVEL%'。我在下面列出的解決方案之一中使用了它。 – user2891264

回答

2

解決的辦法是添加關鍵詞「call」,以象下面螞蟻呼叫,此傳播從螞蟻批調用退出代碼。

stage('build') { 
    steps { 
     bat 'call ant -f xyz\\build.xml' 
    } 
} 

有使用批處理腳本另一種解決方案,見下文,
- Jenkinsfile

stage('build') { 
    steps { 
     bat 'xyz\\build.bat' 
    } 
} 

- 的build.bat

call ant -f "%CD%\xyz\build.xml" 
echo ELVL: %ERRORLEVEL% 
IF NOT %ERRORLEVEL% == 0 ( 
    echo ABORT: %ERRORLEVEL% 
    call exit /b %ERRORLEVEL% 
) ELSE (
    echo PROCEED: %ERRORLEVEL% 
) 

在此的build.bat,如果您不使用通話關鍵字,則只會執行第一條命令其餘的將被忽略。我直接調整了這個螞蟻在管道調用,它的工作。