2017-03-24 62 views
0

我需要我的構建在特定時間(截止時間)超時,但是當前Jenkins dsl僅支持「絕對」策略。所以我嘗試編寫配置塊,但無法創建具有不同截止時間值的作業。在Jenkins dsl configure塊中提供不同的值以創建不同的作業

def settings = [ 
    [ 
     jobname: 'job1', 
     ddl: '13:10:00' 
    ], 
    [ 
     jobname: 'job2', 
     ddl: '14:05:00' 
    ] 
] 


for (i in settings) { 
    job(i.jobname) { 
     configure { 
      it/buildWrappers << 'hudson.plugins.build__timeout.BuildTimeoutWrapper' { 
       strategy(class:'hudson.plugins.build_timeout.impl.DeadlineTimeOutStrategy') { 
        deadlineTime(i.ddl) 
        deadlineToleranceInMinutes(1) 
       } 
      } 
     } 
     steps { 
      // some stuff to do here 
     } 
    } 
} 

上述腳本給了我兩個作業具有相同截止時間(14點05分00秒):

<project> 
    <actions></actions> 
    <description></description> 
    <keepDependencies>false</keepDependencies> 
    <properties></properties> 
    <scm class='hudson.scm.NullSCM'></scm> 
    <canRoam>true</canRoam> 
    <disabled>false</disabled> 
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> 
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> 
    <triggers></triggers> 
    <concurrentBuild>false</concurrentBuild> 
    <builders></builders> 
    <publishers></publishers> 
    <buildWrappers> 
     <hudson.plugins.build__timeout.BuildTimeoutWrapper> 
      <strategy class='hudson.plugins.build_timeout.impl.DeadlineTimeOutStrategy'> 
       <deadlineTime>14:05:00</deadlineTime> 
       <deadlineToleranceInMinutes>1</deadlineToleranceInMinutes> 
      </strategy> 
     </hudson.plugins.build__timeout.BuildTimeoutWrapper> 
    </buildWrappers> 
</project> 

我發現this question但還是沒能得到它的工作。

回答

2

可以使用Automatic Generated API

The generated DSL is only supported when running in Jenkins, e.g. it is 
not available when running from the command line or in the Playground. 

Use The Configure Block to generate custom config elements when not 
running in Jenkins. 

The generated DSL will not work for all plugins, e.g. if a plugin does 
not use the @DataBoundConstructor and @DataBoundSetter annotations to 
declare parameters. In that case The Configure Block can be used to 
generate the config XML. 

幸運的是超時插件支持DataBoundConstructors

@DataBoundConstructor 
public DeadlineTimeOutStrategy(String deadlineTime, int deadlineToleranceInMinutes) { 
    this.deadlineTime = deadlineTime; 
    this.deadlineToleranceInMinutes = deadlineToleranceInMinutes <= MINIMUM_DEADLINE_TOLERANCE_IN_MINUTES ? MINIMUM_DEADLINE_TOLERANCE_IN_MINUTES 
      : deadlineToleranceInMinutes; 
} 

所以,你應該能夠做到像

def settings = [ 
    [ 
     jobname: 'job1', 
     ddl: '13:10:00' 
    ], 
    [ 
     jobname: 'job2', 
     ddl: '14:05:00' 
    ] 
] 


for (i in settings) { 
    job(i.jobname) {   
     wrappers { 
      buildTimeoutWrapper { 
      strategy { 
       deadlineTimeOutStrategy { 
       deadlineTime(i.ddl) 
       deadlineToleranceInMinutes(1) 
       } 
      } 
      timeoutEnvVar('WHAT_IS_THIS_FOR') 
      } 
     } 

     steps { 
      // some stuff to do here 
     } 
    } 
} 

有一個額外的層在BuildTimeoutWrapper其中包含不同的領域tegies

使用時您需要設置嵌套類的類的第一個字母爲小寫


編輯

您可以在自己的詹金斯看到這個用「工作DSL安裝API參考」在喬布斯頁面

http://<your jenkins>/plugin/job-dsl/api-viewer/index.html#method/javaposse.jobdsl.dsl.helpers.wrapper.WrapperContext.buildTimeoutWrapper 

enter image description here

鏈接
+0

這不回答我的問題。但對於我正在努力解決的問題來說,這絕對是一個更好的方法。謝謝! –

相關問題