Skip to content

Commit 9557a36

Browse files
committed
Adding Multibranch seed job
1 parent c969fe4 commit 9557a36

File tree

1 file changed

+131
-0
lines changed
  • configuration/jobs/seed-multibranch-pipelines

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?xml version='1.1' encoding='UTF-8'?>
2+
<project>
3+
<actions/>
4+
<description>Groovy script used to seed Jenkins with multi-branch pipeline jobs:&#xd;
5+
1. Call GitLab API to get each git repo in a given project&#xd;
6+
2. Check if project is archived, if so skip it.&#xd;
7+
3. Check if there is a Jenkinsfile (on master) in each of the found projects&#xd;
8+
4. Generate a pipeline using the Jenkinsfile and add it to the queue on first creation&#xd;
9+
5. Every 10 mins run again</description>
10+
<keepDependencies>false</keepDependencies>
11+
<properties>
12+
<jenkins.model.BuildDiscarderProperty>
13+
<strategy class="hudson.tasks.LogRotator">
14+
<daysToKeep>-1</daysToKeep>
15+
<numToKeep>5</numToKeep>
16+
<artifactDaysToKeep>-1</artifactDaysToKeep>
17+
<artifactNumToKeep>-1</artifactNumToKeep>
18+
</strategy>
19+
</jenkins.model.BuildDiscarderProperty>
20+
<com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty plugin="[email protected]">
21+
<doNotScan>false</doNotScan>
22+
</com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty>
23+
<com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="[email protected]">
24+
<gitLabConnection></gitLabConnection>
25+
</com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
26+
<org.jenkinsci.plugins.gitlablogo.GitlabLogoProperty plugin="[email protected]">
27+
<repositoryName></repositoryName>
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+
<com.sonyericsson.rebuild.RebuildSettings plugin="[email protected]">
34+
<autoRebuild>false</autoRebuild>
35+
<rebuildDisabled>false</rebuildDisabled>
36+
</com.sonyericsson.rebuild.RebuildSettings>
37+
</properties>
38+
<scm class="hudson.scm.NullSCM"/>
39+
<assignedNode>master</assignedNode>
40+
<canRoam>false</canRoam>
41+
<disabled>false</disabled>
42+
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
43+
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
44+
<triggers>
45+
<hudson.triggers.TimerTrigger>
46+
<spec>H/10 * * * *</spec>
47+
</hudson.triggers.TimerTrigger>
48+
</triggers>
49+
<concurrentBuild>false</concurrentBuild>
50+
<builders>
51+
<javaposse.jobdsl.plugin.ExecuteDslScripts plugin="[email protected]">
52+
<scriptText>// Groovy script used to seed Jenkins with multi-branch pipeline jobs:
53+
// 1. Call GitLab API to get each git project in a given group
54+
// 2. Check if project is archived, if so skip it.
55+
// 3. Check if there is a Jenkinsfile (on master) in each of the found projects
56+
// 4. Generate a pipeline using the Jenkinsfile and add it to the queue on first creation
57+
// 5. Every 10 mins run again
58+
59+
def gitlabHost = System.getenv(&quot;GITLAB_HOST&quot;) ?: &quot;https://gitlab.apps.proj.example.com&quot;
60+
def gitlabToken = System.getenv(&quot;GITLAB_TOKEN&quot;) ?: &quot;mytoken123&quot;
61+
def projectName = System.getenv(&quot;GITLAB_GROUP_NAME&quot;) ?: &quot;rht-labs&quot;
62+
def projectsApi = new URL(&quot;${gitlabHost}/api/v4/groups/${projectName}/projects?per_page=100&quot;)
63+
64+
65+
try {
66+
def projects = new groovy.json.JsonSlurper().parse(projectsApi.newReader(requestProperties: [&apos;PRIVATE-TOKEN&apos;: gitlabToken]))
67+
68+
projects.each {
69+
def project = &quot;${it.path}&quot;
70+
def gitPath = it.http_url_to_repo
71+
72+
if (it.archived) {
73+
print &quot;skipping project ${project} because it has been archived\n\n&quot;
74+
return
75+
}
76+
77+
try {
78+
def filesApi = new URL(&quot;${gitlabHost}/api/v4/projects/${it.id}/repository/files/Jenkinsfile?ref=master&quot;)
79+
def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: [&apos;PRIVATE-TOKEN&apos;: gitlabToken]))
80+
81+
if (!jenkins.model.Jenkins.instance.getItemByFullName(project)) {
82+
print &quot;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&quot;
83+
queue(project)
84+
}
85+
86+
// Build Jenkins multibranc jobs
87+
multibranchPipelineJob(project) {
88+
branchSources {
89+
git {
90+
remote(gitPath)
91+
credentialsId(&apos;labs-ci-cd-jenkins-git-password&apos;)
92+
}
93+
}
94+
triggers {
95+
periodic(1)
96+
}
97+
orphanedItemStrategy {
98+
discardOldItems {
99+
numToKeep(10)
100+
}
101+
}
102+
}
103+
}
104+
catch(Exception e) {
105+
println e
106+
print &quot;skipping project ${project} because it has no Jenkinsfile\n\n&quot;
107+
}
108+
}
109+
} catch(Exception e) {
110+
print &quot;\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&quot;
111+
throw e
112+
}</scriptText>
113+
<usingScriptText>true</usingScriptText>
114+
<sandbox>false</sandbox>
115+
<ignoreExisting>true</ignoreExisting>
116+
<ignoreMissingFiles>false</ignoreMissingFiles>
117+
<failOnMissingPlugin>false</failOnMissingPlugin>
118+
<unstableOnDeprecation>false</unstableOnDeprecation>
119+
<removedJobAction>DELETE</removedJobAction>
120+
<removedViewAction>DELETE</removedViewAction>
121+
<removedConfigFilesAction>DELETE</removedConfigFilesAction>
122+
<lookupStrategy>JENKINS_ROOT</lookupStrategy>
123+
</javaposse.jobdsl.plugin.ExecuteDslScripts>
124+
</builders>
125+
<publishers/>
126+
<buildWrappers>
127+
<hudson.plugins.ansicolor.AnsiColorBuildWrapper plugin="[email protected]">
128+
<colorMapName>xterm</colorMapName>
129+
</hudson.plugins.ansicolor.AnsiColorBuildWrapper>
130+
</buildWrappers>
131+
</project>

0 commit comments

Comments
 (0)