2017-04-19 203 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