2017-04-19 199 views
4

我正在創建一個詹金斯管道。這條管線正在構建三個工作(JobOne,JobTwo,JobThree)。我可以用下面的代碼運行這個工作。詹金斯管道工作條件

node { 
    stage 'Stage 1' 
    echo 'Hello World 1' 
    build 'Pipeline-Test/JobOne' 

    stage 'Stage 2' 
    echo 'Hello World 2' 
    build 'Pipeline-Test/JobTwo' 

    stage 'Stage 3' 
    echo 'Hello World 3' 
    build 'Pipeline-Test/JobThree' 
} 

現在我想提出一些條件。例如,當JobOne失敗時,作業必須再次重新啓動。當JobTwo通過時,想要再次運行作業。在JobTwo完成10分鐘後,JobThree應該運行。我不知道如何使這種情況下的管道。我是詹金斯管道的新手。

我檢查了幾個Jenkins WiKi頁面,但找不到正確的方法來實現條件是否符合上述條件。我試着在代碼下面檢查'if'條件是如何實現的。但它失敗了。

node { 
    stage 'Stage 1' 
    echo 'Hello World 1' 
    build 'Pipeline-Test/JobOne' 
    post { 
     always{ 
      build 'Pipeline-Test/JobOne' 
     } 
    } 

錯誤:

java.lang.NoSuchMethodError: No such DSL method 'post' found among [archive, bat, build, catchError, checkout, checkpoint, deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, echo, error, fileExists, git, input, isUnix, load, mail, node, parallel, properties, publishHTML, pwd, readFile, retry, sh, sleep, sshagent, stage, stash, step, teamconcert, timeout, tool, triggerRemoteJob, unarchive, unstash, waitUntil, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, withEnv, wrap, writeFile, ws] 
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:107) 

是否有人可以指導我這個?

在此先感謝!

回答

7

肯定是有與詹金斯管道一個學習的過程,所以不要氣餒:)

我建議檢查出Jenkins' official documentation還有Pipeline Steps Reference page給任何人開始使用詹金斯管道。

FYI,stage s without a block argument is deprecated;你應該定義stage S作爲如下:

stage('Name of Stage') { 
    // code 
} 

管道有一個retry step,您可以使用,如果它不能重試JobOne構建。

要在第2階段和第3階段之間等待10分鐘,可以使用sleep step

if由於Groovy is actually compiled on a JVM中的語句像Java一樣編寫,

if (animal == 'dog' || boolean == true) { 

每一種結合,我覺得這是可以使用的:

node { 
    stage ('Stage 1') { 
      echo 'Hello World 1' 
      retry(1) { 
       build 'Pipeline-Test/JobOne' 
      } 
    } 
    stage ('Stage 2') { 
      echo 'Hello World 2' 
      build 'Pipeline-Test/JobTwo' 
    } 

    sleep time:10, unit:"MINUTES" 

    stage ('Stage 3') { 
      echo 'Hello World 3' 
      build 'Pipeline-Test/JobThree' 
    } 
} 
+0

由於一噸克里斯托弗! :)我一定會深入研究它。我對此完全陌生,並沒有找到出路。再次感謝。我會嘗試你上面提到的步驟。 – Raji

+0

嗨克里斯托弗。 JobOne重複的很好。它按預期工作,但在10分鐘後必須運行的tird作業失敗,出現以下錯誤:groovy.lang.MissingPropertyException:沒有這樣的屬性:類爲MINUTES:WorkflowScript。我正在網上查詢。如果您有任何想法,請告訴我。 – Raji

+0

它只適用於睡眠(30)。我的意思是如果我只通過時間而不是單位。當我通過單位(MINUTES,MILLISECONDS)時,它會拋出上述錯誤。 – Raji