{"id":877,"date":"2019-03-05T18:30:17","date_gmt":"2019-03-05T15:30:17","guid":{"rendered":"https:\/\/artem.services\/?p=830"},"modified":"2019-03-12T17:35:18","modified_gmt":"2019-03-12T14:35:18","slug":"jenkins-dsl-multibranch-pipeline-build-2","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=877&lang=en","title":{"rendered":"Jenkins &#8212; DSL Multibranch Pipeline Creating by Seed Job"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"size-full wp-image-819 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo.png\" alt=\"\" width=\"1280\" height=\"412\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo.png 1280w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-300x97.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-768x247.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-1024x330.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-954x307.png 954w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/p>\n<p><span class=\"tlid-translation translation\"><span class=\"\" title=\"\">In the repository where the future <strong>groovy<\/strong> files will be stored, create a directory called &quot;<strong>utilities<\/strong>&quot; and create the file &quot;<strong>BitbucketMultibranch.groovy<\/strong>&quot; in it with the following contents:<\/span><\/span><\/p>\n<pre class=\"brush: groovy; title: ; notranslate\" title=\"\">\r\npackage utilities\r\n\r\nimport javaposse.jobdsl.dsl.DslFactory\r\n\r\nclass BitbucketMultibranch {\r\n\r\n    String name\r\n    String description\r\n    String displayName\r\n    String serverUrl\r\n    String repoOwner\r\n    String repository\r\n    String credentialsId\r\n    String includeBranches\r\n    String excludeBranches\r\n\r\n\tvoid build(DslFactory dslFactory) {\r\n\t    def job = dslFactory.multibranchPipelineJob(name) {\r\n\t        description(description)\r\n\t        displayName(displayName)\r\n\t        branchSources {\r\n\t            branchSource {\r\n\t                source {\r\n\t                    bitbucket {\r\n\t                        serverUrl(serverUrl)\r\n\t                        repoOwner(repoOwner)\r\n\t                        repository(repository)\r\n\t                        credentialsId(credentialsId)\r\n\t                        traits {\r\n\t                            headWildcardFilter {\r\n\t                                excludes(excludeBranches)\r\n\t                                includes(includeBranches)\r\n\t                            }\r\n\t                        }\r\n\t                    }\r\n\t                }\r\n\t            }\r\n\t        }\r\n\t        configure {\r\n\t          \tdef traits = it \/ sources \/ data \/ 'jenkins.branch.BranchSource' \/ source \/ traits\r\n\t          \ttraits &lt;&lt; 'com.cloudbees.jenkins.plugins.bitbucket.BranchDiscoveryTrait' {\r\n\t            \tstrategyId(3) \/\/ detect all branches\r\n\t          \t}\r\n\t        }\r\n        \tfactory {\r\n\t            workflowBranchProjectFactory {\r\n\t                scriptPath('.jenkins\/Jenkinsfile')\r\n\t            }\r\n\t        }\r\n\t        orphanedItemStrategy {\r\n\t            discardOldItems {\r\n\t                numToKeep(15)\r\n\t            }\r\n\t        }\r\n\t    }\r\n\t}\r\n}\r\n<\/pre>\n<p><span class=\"tlid-translation translation\"><span class=\"\" title=\"\">Now, to create an <strong>Item<\/strong>, it is enough to create a file with the <strong>groovy<\/strong> extension in the root of the repository, so that it can be processed by <strong>Seed Job<\/strong>.<\/span><\/span><\/p>\n<p><!--more--><\/p>\n<h4>artem-dsl.groovy<\/h4>\n<pre class=\"brush: groovy; title: ; notranslate\" title=\"\">\r\nimport utilities.BitbucketMultibranch\r\n\r\ndef multiPipeline = new BitbucketMultibranch(\r\n    description: 'Just try make world better',\r\n    name: 'Artem-DSL',\r\n    displayName: 'Artem-DSL',\r\n    serverUrl: 'https:\/\/git.artem.services',\r\n    repoOwner: &quot;dev&quot;,\r\n    repository: &quot;artem-dls&quot;,\r\n    credentialsId: 'svc-bitbucket',\r\n    includeBranches: 'development staging master',\r\n    excludeBranches: ''\r\n).build(this)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4>Build for GitHub.<\/h4>\n<h4>GitHubMultibranch.groovy:<\/h4>\n<pre class=\"brush: groovy; title: ; notranslate\" title=\"\">\r\npackage utilities\r\n\r\nimport javaposse.jobdsl.dsl.DslFactory\r\n\r\nclass GithubMultibranch {\r\n\r\n    String name\r\n    String description\r\n    String displayName\r\n    String repositoryOwner\r\n    String repositoryName\r\n    String credentials\r\n    String includeBranches\r\n    String excludeBranches\r\n\r\n\r\n\tvoid build(DslFactory dslFactory) {\r\n\t    def job = dslFactory.multibranchPipelineJob(name) {\r\n\t        description(description)\r\n\t        displayName(displayName)\r\n\t\t    branchSources {\r\n\t\t        github {\r\n\t\t            scanCredentialsId(credentials)\r\n\t\t            repoOwner(repositoryOwner)\r\n\t\t            repository(repositoryName)\r\n\t\t            includes(includeBranches)\r\n\t\t            excludes(excludeBranches)\r\n\t\t        }\r\n\t\t    }\r\n\t\t    factory {\r\n\t\t        workflowBranchProjectFactory {\r\n\t\t            scriptPath('.jenkins\/Jenkinsfile')\r\n\t\t        }\r\n\t\t    }\r\n\t\t    orphanedItemStrategy {\r\n\t\t        discardOldItems {\r\n\t\t            numToKeep(15)\r\n\t\t        }\r\n\t\t    }\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<h4>artem-github.groovy:<\/h4>\n<pre class=\"brush: groovy; title: ; notranslate\" title=\"\">\r\nimport utilities.GithubMultibranch\r\n\r\n\r\ndef multiPipeline = new GithubMultibranch(\r\n    description: 'Just try make world better',\r\n    name: 'Github-Test',\r\n    displayName: 'Github-Test',\r\n    repositoryOwner: &quot;artem&quot;,\r\n    repositoryName: &quot;dsl-test&quot;,\r\n    credentials: 'artem-github',\r\n    includeBranches: 'development staging master',\r\n    excludeBranches: ''\r\n).build(this)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the repository where the future groovy files will be stored, create a directory called &quot;utilities&quot; and create the file &quot;BitbucketMultibranch.groovy&quot; in it with the following contents: Now, to create an Item, it is enough to create a file with the groovy extension in the root of the repository, so that it can be processed &hellip; <a href=\"https:\/\/artem.services\/?p=877&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;Jenkins &#8212; DSL Multibranch Pipeline Creating by Seed Job&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[613],"tags":[635,633,615,627,637,617,629,639],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/877"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=877"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/877\/revisions"}],"predecessor-version":[{"id":879,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/877\/revisions\/879"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}