Skip to content

Commit 278e9ee

Browse files
authored
Merge pull request #10 from rht-labs/feature/add-seed
Adding Multibranch seed job
2 parents c969fe4 + f7d9cac commit 278e9ee

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ By default the deployment will attempt to connect to SonarQube and configure its
2929
## Git Creds
3030
Inject the `git` credentials to Jenkins-s2i when it is being built by editing `configuration/init.groovy.d/configure-credentials.groovy` or by exposing a new environment Variable to the Jenkins deployment tempate.
3131

32+
## Jenkins DSL Seed for MultiBranch Pipelines (GitLab)
33+
34+
A DSL Seed job is included in the s2i. The purpose of this job is to automatically generate multi branc pipelines for each project in a given GitLab namespace that has a `Jenkinsfile`. To set this up, configure the Deployment Config for your Jenkins with the following `ENVIRONMENT` variables or just edit the `configuration/jobs/seed-multibranch-pipelines/config.xml` file. If you don't want or need this job, just delete it from the `configuration/jobs` directory.
35+
```
36+
GITLAB_HOST is the Http address of the GitLab Project eg 'https://gitlab.apps.proj.example.com'
37+
GITLAB_TOKEN is the GitLab API token to access repos and projects eg 'token123'
38+
GITLAB_GROUP_NAME is the GitLab group name where projects are stored eg 'rht-labs'
39+
```
40+
3241
## Shared Library
3342

3443
An optional shared global library can be used to add method calls to pipelines which can help to simplify and organize a pipeline. The global library will be implicitly available to all pipelines.
@@ -42,4 +51,3 @@ To configure a library environment variables need to be made available to your i
4251
## Contributing
4352

4453
There are some [helpers](helpers/README.MD) to get configuration out of a running Jenkins.
45-
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)