Jenkins将git子模块添加到带有DSL的multibranchPipelineJob

时间:2017-12-03 15:43:45

标签: jenkins jenkins-job-dsl

我有一个DSL脚本来创建我的Jenkins管道作业。

String[] repos =
    ['xxx']    

for (int i = 0; i < repos.length; i++) {
    repoName = repos[i];    
    multibranchPipelineJob('PIPELINE-'+repoName) {
        branchSources {
            git {
                remote('git@github.com:yyy/'+repoName+'.git')
                credentialsId('112233445566')
            }
        }
        description ("""<p> <b>generate by DSL - DO NOT CHANGE Manually </b> <p>""")
        triggers {
            periodic(2)
        }
        orphanedItemStrategy {
            discardOldItems {
                numToKeep(0)
                daysToKeep(0)
            }
        }
    }
}

我想添加附加行为 - 高级子模块行为enter image description here

2 个答案:

答案 0 :(得分:4)

内置DSL不支持&#34;附加行为&#34;,但Automatically Generated DSL会:

multibranchPipelineJob('example-mbp2') {
  branchSources {
    branchSource {
      source {
        git {
          id('bbedfd29-5bb0-4c13-b040-0dbd0d19345b')
          remote('https://github.com/jenkinsci/job-dsl-plugin.git')
          traits {
            submoduleOptionTrait {
              extension {
                disableSubmodules(false)
                recursiveSubmodules(true)
                trackingSubmodules(false)
                reference(null)
                timeout(null)
                parentCredentials(true)
              }
            }
          }
        }
      }
    } 
  }
}

答案 1 :(得分:3)

这是一个添加子模块标签和分支发现

的脚本
multibranchPipelineJob('example') {
  branchSources {    
    branchSource {
      source {        
        git {
          credentialsId('1111-2222-3333')
          remote('git@github.com:xxx/yyyy.git')

          traits {            
            submoduleOptionTrait {
              extension {
                disableSubmodules(false)
                recursiveSubmodules(true)
                trackingSubmodules(false)
                reference(null)
                timeout(null)
                parentCredentials(true)
              }
            }

            cloneOptionTrait {
              extension {
                shallow (false)
                noTags (false)
                reference (null)
                depth(0)
                honorRefspec (false)
                timeout (10)
              }
            }
          }
        }
      }
    } 
  }

  triggers {
            periodic(2)
        }

configure {
      def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
      traits << 'jenkins.plugins.git.traits.BranchDiscoveryTrait' {}
    }  
}