loop over array of jobs to create them?

▼魔方 西西 提交于 2020-01-06 06:24:13

问题


I have the dsl job configured to delete unreferenced jobs and i want to keep that:

Im trying to do this:

def bitbucket_team = 'myteam'
def bitbucket_user = 'mycreds'
def repo_arr = ['job1','job2']

repo_arr.collect { repo ->
    println "${repo}"

    multibranchPipelineJob("${repo}") {
        configure {
            it / sources / data / 'jenkins.branch.BranchSource' / source(class: 'com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource') {
                credentialsId("${bitbucket_user}")
                //checkoutCredentialsId('bitbucket-ssh-key') // can use ssh key here instead of a BB user
                repoOwner("${bitbucket_team}")
                repository("${repo}")
                includes('*')
                excludes()

                traits {
                    'com.cloudbees.jenkins.plugins.bitbucket.BranchDiscoveryTrait'() {
                        strategyId(1) // Exclude branches that are also filed as PRs
                        //strategyId(2) // Only branches that are also filed as PRs
                        //strategyId(3) // All branches
                    }
                    'com.cloudbees.jenkins.plugins.bitbucket.ForkPullRequestDiscoveryTrait'() {
                        strategyId(1)
                    }
                    'com.cloudbees.jenkins.plugins.bitbucket.OriginPullRequestDiscoveryTrait'(){
                        strategyId(1) // Merging the pull request with the current target branch revision
                        //strategyId(2) // The current pull request revision
                        //strategyId(3) // Both the current pull request revision and the pull request merged with the current target branch revision
                        //Default to trust forks in same account
                    }
                    'com.cloudbees.jenkins.plugins.bitbucket.WebhookRegistrationTrait'() {
                        mode('ITEM')
                    }
                }

            }
        }
    }

    // Add jobs to a list view
    listView('myview') {
        jobs {
            name("${repo}")
        }
         columns{
                    status()
                    weather()
                    name()
                    lastSuccess()
                    lastFailure()
                    lastDuration()
                    buildButton()
            }
    }
} // End repo_arr.collect

Jenkins creates job1 but then deletes it when it creates job2. How do I loop over a list to create multiple jobs?

Perhaps I can build a map/closure of multibranchPipelineJob objs and listView.jobs and pass that to the dsl somehow?


回答1:


Im dumb the jobs themselves were actually getting created fine it was just the listview that was replacing them. Makes sense because I was recreating the same listview for each iteration.

https://gist.github.com/kyounger/83134869ea523b3661f0

I just had to move that out of the loop:

listView('mylist') {
  jobs {
    jobsarry.each { job ->
      name(job)
    }
  }
  columns{
    status()
    weather()
    name()
    lastSuccess()
    lastFailure()
    lastDuration()
    buildButton()
  }
}


来源:https://stackoverflow.com/questions/51144018/loop-over-array-of-jobs-to-create-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!