Generating multibranch pipeline jobs without adding a Jenkinsfile to each repo

861 views
Skip to first unread message

Robert Munteanu

unread,
Nov 6, 2017, 10:57:58 AM11/6/17
to job-dsl-plugin
Hi,

I am trying to enable pull request build checks to a number of 200+ repositories. Currently I'm generating the Jobs as Maven jobs ( https://builds.apache.org/view/S-Z/view/Sling/ ).

I did a prototype with multibranch pipeline jobs and pull requests work just fine. However, to my understanding we can't have a multibranch pipeline job without a Jenkinsfile in the repository. For 200+ repositories that is a no-go.

The GitHub PR builder plug-in seems unmaintained ( https://wiki.jenkins.io/display/JENKINS/GitHub+pull+request+builder+plugin ) so I am not eager to try it out.

What options to I have to manage pull request building with the Job DSL plugin without needing to touch each project and create a Jenkinsfile?

Thanks,

Robert

Robert Munteanu

unread,
Nov 6, 2017, 11:01:27 AM11/6/17
to job-dsl-plugin
Oh, and as an additional constraint, I can not get admin credentials on the Github projects, only write.

Robert

Kristiyan Nikolov

unread,
Nov 7, 2017, 3:04:58 PM11/7/17
to job-dsl-plugin
Hi,


In order to use it via Job DSL you can do something like this:

String getProjectType() {
  return instance.getDescriptor(
    'PipelineMultiBranchDefaultsProject',
  ).clazz.getCanonicalName()
}

String getBranchFactory() {
  return instance.getDescriptor(
   'PipelineBranchDefaultsProjectFactory',
  ).clazz.getCanonicalName()
}

multibranchPipelineJob('my-pipeline') {
  configure
{ project ->
    project
.name getProjectType()
    project
/ factory(class: "${getBranchFactory()}")
 
}
}

Hope this is helpful for you

Matt Sheehan

unread,
Nov 8, 2017, 3:27:05 PM11/8/17
to job-dsl...@googlegroups.com
You can also do something like this:


GitHub gitHub = new GitHubBuilder()
    .withOAuthToken('token', 'user')
    .build()

String repoName = 'repo'
gitHub.getRepository(repoName).getPullRequests(GHIssueState.OPEN).each { GHPullRequest pr ->
    String branchName = pr.head.ref
    job("$repoName-$branchName") {

        scm {
            git {
                remote {
                    github "$pr.repository.ownerName/$pr.repository.name"
                }
                branch branchName
            }
        }

        steps {
            // do some work
        }
    }
}

Robert Munteanu

unread,
Nov 9, 2017, 4:02:45 AM11/9/17
to job-dsl...@googlegroups.com
Hi Kristiyan,

On Tue, Nov 7, 2017 at 10:04 PM, Kristiyan Nikolov <knik...@gmail.com> wrote:
> you can use this plugin
> https://plugins.jenkins.io/pipeline-multibranch-defaults

This seems quite cool, thanks! Is it possible to use it without
defining a global Jenkins config file? I don't have admin access to
the Jenkins instance.

Thanks,

Robert

Robert Munteanu

unread,
Nov 9, 2017, 4:04:34 AM11/9/17
to job-dsl...@googlegroups.com
Hi Matt,

On Wed, Nov 8, 2017 at 10:26 PM, Matt Sheehan <mr.sh...@gmail.com> wrote:
> gitHub.getRepository(repoName).getPullRequests(GHIssueState.OPEN).each {
> GHPullRequest pr ->
> String branchName = pr.head.ref
> job("$repoName-$branchName") {

That creates a job per pull request, right? Seems a bit heavy to me, I
would prefer not to expand the number of jobs even more. Also, how are
jobs deleted in this scenario?

Thanks,

Robert
Reply all
Reply to author
Forward
0 new messages