Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcelaraujo/ec7f6986549385bac7a50cf85857280d to your computer and use it in GitHub Desktop.
Save marcelaraujo/ec7f6986549385bac7a50cf85857280d to your computer and use it in GitHub Desktop.
multibranchPipelineJob snippets

Core plugin support

https://github.com/jenkinsci/job-dsl-plugin/blob/350005c9878ab4fd5210667c25b9f555b230fdae/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/DslFactory.groovy

Trigger with comment

https://github.com/openshift/contra-lib/blob/0728943678a0c7b632c4a4e78e9d60e63fe6ff1e/src/org/centos/contra/jobdsl/MultiBranchJob.groovy

configure {
                it / sources / 'data' / 'jenkins.branch.BranchSource' << {
                    strategy(class: 'jenkins.branch.DefaultBranchPropertyStrategy') {
                        properties(class: 'java.util.Arrays$ArrayList') {
                            a(class: 'jenkins.branch.BranchProperty-array') {
                                if (comment == "\\[merge\\]") {
                                    'jenkins.branch.NoTriggerBranchProperty'()

                                }
                                'com.adobe.jenkins.github__pr__comment__build.TriggerPRCommentBranchProperty' {
                                    commentBody(comment)
                                }
                            }
                        }
                    }
                }

Configure git properties

https://github.com/jenkinsci/codesonar-plugin/blob/87820da14608eb11a9bbeaba997a3e8702e17255/jenkins-pipeline/pipeline.groovy


    configure {
        def traitBlock = it / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'source' / 'traits' 
        traitBlock << 'jenkins.plugins.git.traits.CloneOptionTrait' {
            extension(class: 'hudson.plugins.git.extensions.impl.CloneOption') {
                shallow(false)
                noTag(false)
                reference()
                depth(0)
                honorRefspec(false)
            }
        }
        

Create jobs as functions

https://github.com/hereischen/hola-jenkins/blob/b12cc7a41e683294498256a356230b6ff422de10/dsl/seed.groovy


def createDeploymentJob(jobName, repoUrl) {
    pipelineJob(jobName) {
        definition {
            cpsScm {
                scm {
                    git {
                        remote {
                            url(repoUrl)
                        }
                        branches('master')
                        extensions {
                            cleanBeforeCheckout()
                        }
                    }
                }
                scriptPath("jobs/deploy.groovy")
            }
        }
    }
}
        
createDeploymentJob(deployName, repoUrl)
queue(deployName)

Git SCM properties

    branchSources {
      github {
        apiUri(GITHUB_API_URI)

        scanCredentialsId("37477e0c-2ab6-46fe-a83b-64b1add4777d")
        checkoutCredentialsId("37477e0c-2ab6-46fe-a83b-64b1add4777d")
        repoOwner(GITHUB_ORGANIZATION_NAME)
        repository(repositoryName)
        buildForkPRHead(true)
        buildForkPRMerge(false)
        buildOriginBranch(false)
        buildOriginBranchWithPR(false)
        buildOriginPRHead(true)
        buildOriginPRMerge(false)
      }
    }

Run a script

Method 1:

        configure {
            // my Jenkinsfile doesn't exist in the chef Cookbooks for 900 repositories... 
            // how can we set a seperate git repo for shared scripts? we don't want to use submodules
            it / factory(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory') {
                owner(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject', reference: '../..')
                scriptPath("jenkinsfile")
            }
        }

Method 2:

    factory {
        workflowBranchProjectFactory {
        // Relative location within the checkout of your Pipeline script.
        scriptPath('jenkinsfile')
        }
    }

Branch discovery trait


    configure {
        def traitBlock = it / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'source' / 'traits'

        traitBlock << 'jenkins.plugins.git.traits.BranchDiscoveryTrait' {}
    }

Generating folders for jobs

https://github.com/chit787/jenkins-jobs/blob/9bacd25e1b723ef7f350b2ac8b70be465067118d/jobs/test1/cypress.groovy

Another way to add git SCM's using clojures

This might be useful for advanceed properties? https://github.com/sumeetraheja/seed-jobs/blob/5698c6f1a3d66586d3454355126bc84a43fcfb64/jobs/checkout/checkout_backend_api_tests_job.groovy

    branchSources {
        configure { node ->
          node / sources(class: 'jenkins.branch.MultiBranchProject$BranchSourceList') / data / 'jenkins.branch.BranchSource' / source(class: 'jenkins.plugins.git.GitSCMSource') {
            remote 'ssh://[email protected]:2222/checkout/checkout-api-tests.git'
            credentialsId 'jenkins-ci'
            includes '*'
            excludes ''
            ignoreOnPushNotifications 'false'
            id ''
            traits {
              'jenkins.plugins.git.traits.BranchDiscoveryTrait'()
              'jenkins.plugins.git.traits.CloneOptionTrait' () {
                extension(class: 'hudson.plugins.git.extensions.impl.CloneOption') {
                  shallow 'false'
                  noTags 'false'
                  reference ''
                  depth '0'
                  honorRefspec 'false'
                }
              }
            }
          }
        }
    }

Very interesting seeder, possibly using multiple SCMS using folder libraries?

https://github.com/automatictester/lightning/blob/893b975996358ac0e0308a3620fbd3b0c14530e8/jobdsl.groovy

Very interesting seeder example 2

https://github.com/SignVOVA/jenkins-jobs/blob/a7be42e6561a31e85b5d5ca68781c0b53b3e6aa3/JenkinsJob/jobs/defn/demo/demo.groovy

Very interesting pattern building jobs from JSON structured data

https://github.com/ChumbokIT/sdlc/blob/c1b662570279ccb524a63b76be38bce3ce7da06c/src/main/groovy/com/chumbok/sdlc/JobBuilder.groovy

Example of slurping github repo json from public API and building jobs

https://github.com/ChumbokIT/sdlc/blob/e1e5afa29a7736abb871c7fd25e9b125165c60b3/src/main/groovy/com/chumbok/sdlc/AutoJobBuilder.groovy

Interesting jobs generator pattern

https://github.com/ggirou/yet-another-jenkins-notifier/blob/0d8f86134baa2f31eeb91b865ef97239f9a4a6d3/JobsGenerator.groovy

Interesting jobs generator using DSL outside and generating workflow pipelines inside

https://github.com/ggnanasekaran77/jenkins/blob/e6b41374b5dceeb7bc0d516ec71f498f0e472da9/files/firstseedjob.groovy

Pretty fantastic pipeline generator using workflow plugin github scm

https://github.com/samrocketman/demo-jenkins-world-2018-jenkins-bootstrap/blob/314b959339ae3fae772e70822006e401bf0e847c/jobs/jenkins_job_multibranch_pipeline.groovy#L35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment