本文介绍了如何使用Jenkins DSL为所有作业设置作业超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了。这为一份工作设定了超时时间。我想为所有工作设置它,并设置稍微不同的设置:150%,平均超过10个工作,最多30分钟。

根据我应该使用下面的语法:

 'job('example-3'){
wrappers {
timeout {
elastic(150,10,30)
failBuild()
writeDescription('Build failed由于{0}分钟后超时)
}
}
}

我在进行了测试,是相关的XML部分:

 < buildWrappers> 
< hudson.plugins.build__timeout.BuildTimeoutWrapper>
< strategy class ='hudson.plugins.build_timeout.impl.ElasticTimeOutStrategy'>
< timeoutPercentage> 150< / timeoutPercentage>
< numberOfBuild> 10< / numberOfBuild>
< timeoutMinutesElasticDefault> 30< / timeoutMinutesElasticDefault>
< / strategy>
< operationList>
< hudson.plugins.build__timeout.operations.FailOperation>< /hudson.plugins.build__timeout.operations.FailOperation>
< hudson.plugins.build__timeout.operations.WriteDescriptionOperation>
< description>由于{0}分钟后超时而造成编译失败< / description>
< / operationList>
< /hudson.plugins.build__timeout.BuildTimeoutWrapper>
< / buildWrappers>

我使用之前手动编辑的作业进行了验证,并且XML是正确的。所以我知道Jenkins的DSL语法是正确的。






现在我想将它应用于所有作业。首先我试着列出所有的工作名称:

  import jenkins.model。* 

jenkins。 model.Jenkins.instance.items.findAll()。每个{
println(Job:+ it.name)
}

这也适用,所有的工作名称都印在控制台上。




现在我想将它们连接在一起。这是我使用的完整代码:

  import jenkins.model。* 

jenkins.model。 Jenkins.instance.items.findAll()。each {
job(it.name){
wrappers {
timeout {
elastic(150,10,30)
failBuild()
writeDescription('在{0}分钟后由于超时而生成失败)
}
}
}
}

当我推送此代码并且Jenkins运行DSL种子作业时,出现此错误:

 错误:项目类型jobname与现有类型不匹配,项目类型不能更改

我在这里做错了什么?

解决方案

Job-DSL插件只能用于维护之前由该插件创建的作业。您试图修改以其他方式创建的作业配置 - 这是行不通的。



对于现有作业的批量修改(例如,在你的情况下,添加超时)最直接的方法是直接更改作业的XML规范,或者通过更改<$ c


  • 使用REST或CLI API




    $ / $> b

    是直接在shell级别执行此类任务的强大工具。 p>

    或者,可以通过脚本控制台中的Groovy脚本执行更改 - 但为此,您需要了解Jenkins的内部工作原理和数据结构。

    I read How can I set the job timeout using the Jenkins DSL. That sets the timeout for one job. I want to set it for all jobs, and with slightly different settings: 150%, averaged over 10 jobs, with a max of 30 minutes.

    According to the relevant job-dsl-plugin documentation I should use this syntax:

    job('example-3') {
        wrappers {
            timeout {
                elastic(150, 10, 30)
                failBuild()
                writeDescription('Build failed due to timeout after {0} minutes')
            }
        }
    }
    

    I tested in http://job-dsl.herokuapp.com/ and this is the relevant XML part:

    <buildWrappers>
        <hudson.plugins.build__timeout.BuildTimeoutWrapper>
            <strategy class='hudson.plugins.build_timeout.impl.ElasticTimeOutStrategy'>
                <timeoutPercentage>150</timeoutPercentage>
                <numberOfBuilds>10</numberOfBuilds>
                <timeoutMinutesElasticDefault>30</timeoutMinutesElasticDefault>
            </strategy>
            <operationList>
                <hudson.plugins.build__timeout.operations.FailOperation></hudson.plugins.build__timeout.operations.FailOperation>
                <hudson.plugins.build__timeout.operations.WriteDescriptionOperation>
                    <description>Build failed due to timeout after {0} minutes</description>
                </hudson.plugins.build__timeout.operations.WriteDescriptionOperation>
            </operationList>
        </hudson.plugins.build__timeout.BuildTimeoutWrapper>
    </buildWrappers>
    

    I verified with a job I edited manually before, and the XML is correct. So I know that the Jenkins DSL syntax up to here is correct.


    Now I want to apply this to all jobs. First I tried to list all the job names:

    import jenkins.model.*
    
    jenkins.model.Jenkins.instance.items.findAll().each {
      println("Job: " + it.name)
    }
    

    This works too, all job names are printed to console.


    Now I want to plug it all together. This is the full code I use:

    import jenkins.model.*
    
    jenkins.model.Jenkins.instance.items.findAll().each {
      job(it.name) {
        wrappers {
          timeout {
            elastic(150, 10, 30)
            failBuild()
            writeDescription('Build failed due to timeout after {0} minutes')
          }
        }
      }
    }
    

    When I push this code and Jenkins runs the DSL seed job, I get this error:

    ERROR: Type of item "jobname" does not match existing type, item type can not be changed
    

    What am I doing wrong here?

    解决方案

    The Job-DSL plugin can only be used to maintain jobs that have been created by that plugin before. You're trying to modify the configuration of jobs that have been created in some other way -- this will not work.

    For mass-modification of existing jobs (like, in your case, adding the timeout) the most straightforward way is to change the job's XML specification directly,

    • either by changing the config.xml file on disk, or
    • using the REST or CLI API

    xmlstarlet is a powerful tool for performing such tasks directly on shell level.

    Alternatively, it is possible to perform the change via a Groovy script from the "Script Console" -- but for that you need some understanding of Jenkins' internal workings and data structures.

    这篇关于如何使用Jenkins DSL为所有作业设置作业超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:29