|
17 | 17 | <artifactNumToKeep>-1</artifactNumToKeep>
|
18 | 18 | </strategy>
|
19 | 19 | </jenkins.model.BuildDiscarderProperty>
|
20 |
| - <com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty plugin="build-failure-analyzer@1.20.0"> |
| 20 | + <com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty plugin="build-failure-analyzer@1.26.0"> |
21 | 21 | <doNotScan>false</doNotScan>
|
22 | 22 | </com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty>
|
23 |
| - < com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin= "[email protected].9"> |
| 23 | + < com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin= "[email protected].13"> |
24 | 24 | <gitLabConnection></gitLabConnection>
|
25 | 25 | </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
|
26 |
| - < org.jenkinsci.plugins.gitlablogo.GitlabLogoProperty plugin= "[email protected].3"> |
| 26 | + < org.jenkinsci.plugins.gitlablogo.GitlabLogoProperty plugin= "[email protected].5"> |
27 | 27 | <repositoryName></repositoryName>
|
28 | 28 | </org.jenkinsci.plugins.gitlablogo.GitlabLogoProperty>
|
29 |
| - < org.jenkinsci.plugins.gogs.GogsProjectProperty plugin= "[email protected]"> |
30 |
| - <gogsSecret></gogsSecret> |
31 |
| - <gogsUsePayload>false</gogsUsePayload> |
32 |
| - </org.jenkinsci.plugins.gogs.GogsProjectProperty> |
33 | 29 | < com.sonyericsson.rebuild.RebuildSettings plugin= "[email protected]">
|
34 | 30 | <autoRebuild>false</autoRebuild>
|
35 | 31 | <rebuildDisabled>false</rebuildDisabled>
|
|
48 | 44 | </triggers>
|
49 | 45 | <concurrentBuild>false</concurrentBuild>
|
50 | 46 | <builders>
|
51 |
| - <javaposse.jobdsl.plugin.ExecuteDslScripts plugin="job-dsl@1.70"> |
| 47 | + <javaposse.jobdsl.plugin.ExecuteDslScripts plugin="job-dsl@1.77"> |
52 | 48 | <scriptText>// Groovy script used to seed Jenkins with multi-branch pipeline jobs:
|
53 | 49 | // 1. Call GitLab API to get each git project in a given group
|
54 | 50 | // 2. Check if project is archived, if so skip it.
|
55 | 51 | // 3. Check if there is a Jenkinsfile (on master) in each of the found projects
|
56 | 52 | // 4. Generate a pipeline using the Jenkinsfile and add it to the queue on first creation
|
57 | 53 | // 5. Every 10 mins run again
|
58 | 54 |
|
59 |
| -def gitlabHost = System.getenv("GITLAB_HOST") ?: "https://gitlab.apps.proj.example.com" |
60 |
| -def gitlabToken = System.getenv("GITLAB_TOKEN") ?: "mytoken123" |
61 |
| -def projectName = System.getenv("GITLAB_GROUP_NAME") ?: "rht-labs" |
62 |
| -def projectsApi = new URL("${gitlabHost}/api/v4/groups/${projectName}/projects?per_page=100") |
63 | 55 |
|
| 56 | +// GITLAB |
| 57 | +def gitlabHost = System.getenv("GITLAB_HOST") ?: "https://gitlab.apps.proj.example.com" |
| 58 | +def gitlabToken = System.getenv("GITLAB_TOKEN") |
| 59 | +def projectName = System.getenv("GITLAB_GROUP_NAME") ?: "rht-labs" |
| 60 | +def gitlabProjectsApi = new URL("${gitlabHost}/api/v4/groups/${projectName}/projects?per_page=100") |
64 | 61 |
|
65 |
| -try { |
66 |
| - def projects = new groovy.json.JsonSlurper().parse(projectsApi.newReader(requestProperties: ['PRIVATE-TOKEN': gitlabToken])) |
67 | 62 |
|
68 |
| - projects.each { |
69 |
| - def project = "${it.path}" |
70 |
| - def gitPath = it.http_url_to_repo |
| 63 | +// GITHUB |
| 64 | +def githubHost = "https://api.github.com" |
| 65 | +// Token needed for rate limiting issues.... |
| 66 | +def githubToken = System.getenv("GITHUB_TOKEN") |
| 67 | +def githubAccount = System.getenv("GITHUB_ACCOUNT") |
| 68 | +def githubOrg = System.getenv("GITHUB_ORG") ?: false |
| 69 | +//eg https://api.github.com/users/springdo/repos or |
71 | 70 |
|
72 |
| - if (it.archived) { |
73 |
| - print "skipping project ${project} because it has been archived\n\n" |
74 |
| - return |
75 |
| - } |
| 71 | +def githubProjects = githubOrg ? new URL("${githubHost}/org/${githubAccount}/repos?per_page=100") : new URL("${githubHost}/users/${githubAccount}/repos?per_page=100") |
76 | 72 |
|
77 |
| - try { |
78 |
| - def filesApi = new URL("${gitlabHost}/api/v4/projects/${it.id}/repository/files/Jenkinsfile?ref=master") |
79 |
| - def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['PRIVATE-TOKEN': gitlabToken])) |
80 | 73 |
|
81 |
| - if (!jenkins.model.Jenkins.instance.getItemByFullName(project)) { |
82 |
| - print "About to create ${project} for the first time, this will result in a triggering the build after this run to prepare the ${project} pipeline\n\n" |
83 |
| - queue(project) |
84 |
| - } |
| 74 | +def createMultibranchPipelineJob(project, gitPath) { |
| 75 | + def buildNamespace = System.getenv("BUILD_NAMESPACE") ?: "labs-ci-cd" |
| 76 | + def buildGitAuthSecret = System.getenv("BUILD_GIT_AUTH_SECRET") ?: "git-auth" |
85 | 77 |
|
86 |
| - // Build Jenkins multibranc jobs |
87 |
| - multibranchPipelineJob(project) { |
88 |
| - branchSources { |
89 |
| - git { |
90 |
| - remote(gitPath) |
91 |
| - credentialsId('labs-ci-cd-jenkins-git-password') |
92 |
| - } |
93 |
| - } |
94 |
| - triggers { |
95 |
| - computedFolderWebHookTrigger { |
96 |
| - // The token to match with webhook token. |
97 |
| - token(project) |
98 |
| - } |
99 |
| - } |
100 |
| - orphanedItemStrategy { |
101 |
| - discardOldItems { |
102 |
| - numToKeep(10) |
103 |
| - } |
104 |
| - } |
| 78 | + // Build Jenkins multibranc jobs |
| 79 | + multibranchPipelineJob(project) { |
| 80 | + branchSources { |
| 81 | + git { |
| 82 | + id("${project}") |
| 83 | + remote(gitPath) |
| 84 | + credentialsId("${buildNamespace}-${buildGitAuthSecret}") |
| 85 | + } |
| 86 | + } |
| 87 | + triggers { |
| 88 | + computedFolderWebHookTrigger { |
| 89 | + // The token to match with webhook token. |
| 90 | + token(project) |
105 | 91 | }
|
106 | 92 | }
|
107 |
| - catch(Exception e) { |
108 |
| - println e |
109 |
| - print "skipping project ${project} because it has no Jenkinsfile\n\n" |
| 93 | + orphanedItemStrategy { |
| 94 | + discardOldItems { |
| 95 | + // Set to 0 to autoprune jobs once branch is deleted |
| 96 | + numToKeep(0) |
| 97 | + } |
110 | 98 | }
|
111 | 99 | }
|
112 |
| -} catch(Exception e) { |
113 |
| - print "\n\n Please make sure you have set GITLAB_HOST, GITLAB_TOKEN and GITLAB_GROUP_NAME in your deploy config for Jenkins \n\n\n" |
114 |
| - throw e |
| 100 | +} |
| 101 | + |
| 102 | +def addJobToQueue(project){ |
| 103 | + if (!jenkins.model.Jenkins.instance.getItemByFullName(project)) { |
| 104 | + print "About to create ${project} for the first time, this will result in a triggering the build after this run to prepare the ${project} pipeline\n\n" |
| 105 | + queue(project) |
| 106 | + } |
| 107 | +} |
| 108 | +// if GITLAB* set .... |
| 109 | +if (gitlabToken) { |
| 110 | + try { |
| 111 | + def projects = new groovy.json.JsonSlurper().parse(gitlabProjectsApi.newReader(requestProperties: ['PRIVATE-TOKEN': gitlabToken])) |
| 112 | + |
| 113 | + projects.each { |
| 114 | + def project = "${it.path}" |
| 115 | + def gitPath = it.http_url_to_repo |
| 116 | + |
| 117 | + if (it.archived) { |
| 118 | + print "skipping project ${project} because it has been archived\n\n" |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + try { |
| 123 | + def filesApi = new URL("${gitlabHost}/api/v4/projects/${it.id}/repository/files/Jenkinsfile?ref=master") |
| 124 | + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['PRIVATE-TOKEN': gitlabToken])) |
| 125 | + |
| 126 | + createMultibranchPipelineJob(project, gitPath) |
| 127 | + addJobToQueue(project) |
| 128 | + |
| 129 | + } |
| 130 | + catch(Exception e) { |
| 131 | + println e |
| 132 | + print "skipping project ${project} because it has no Jenkinsfile\n\n" |
| 133 | + } |
| 134 | + } |
| 135 | + } catch(Exception e) { |
| 136 | + print "\n\n Please make sure you have set GITLAB_HOST, GITLAB_TOKEN and GITLAB_GROUP_NAME in your deploy config for Jenkins \n\n\n" |
| 137 | + throw e |
| 138 | + } |
| 139 | +} else if (githubAccount) { |
| 140 | + try { |
| 141 | + def projects = new groovy.json.JsonSlurper().parse(githubProjects.newReader(requestProperties: ['Authorization': "token ${githubToken}"])) |
| 142 | + |
| 143 | + projects.each { |
| 144 | + def project = it.name |
| 145 | + def gitPath = it.clone_url |
| 146 | + |
| 147 | + if (it.archived) { |
| 148 | + print "skipping project ${project} because it has been archived\n\n" |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + try { |
| 153 | + // https://api.github.com/repos/$ORG or $USER/name/contents/Jenkinsfile |
| 154 | + def filesApi = new URL("${githubHost}/repos/${githubAccount}/${project}/contents/Jenkinsfile") |
| 155 | + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': "token ${githubToken}"])) |
| 156 | + |
| 157 | + |
| 158 | + createMultibranchPipelineJob(project, gitPath) |
| 159 | + addJobToQueue(project) |
| 160 | + } |
| 161 | + catch(Exception e) { |
| 162 | + println e |
| 163 | + print "skipping project ${project} because it has no Jenkinsfile\n\n" |
| 164 | + } |
| 165 | + } |
| 166 | + } catch(Exception e) { |
| 167 | + print "\n\n Oops! something went wrong..... Try setting the GITHUB_* Env Vars \n\n\n" |
| 168 | + throw e |
| 169 | + } |
| 170 | +} else { |
| 171 | + print "\n\n No tokens set in the Environment eg GITHUB* or GITLAB* so not sure what to do ..... 🤷♂️ \n\n\n" |
115 | 172 | }</scriptText>
|
116 | 173 | <usingScriptText>true</usingScriptText>
|
117 | 174 | <sandbox>false</sandbox>
|
|
0 commit comments