-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from rht-labs/feature/add-seed
Adding Multibranch seed job
- Loading branch information
Showing
2 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
configuration/jobs/seed-multibranch-pipelines/config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?xml version='1.1' encoding='UTF-8'?> | ||
<project> | ||
<actions/> | ||
<description>Groovy script used to seed Jenkins with multi-branch pipeline jobs:
 | ||
1. Call GitLab API to get each git repo in a given project
 | ||
2. Check if project is archived, if so skip it.
 | ||
3. Check if there is a Jenkinsfile (on master) in each of the found projects
 | ||
4. Generate a pipeline using the Jenkinsfile and add it to the queue on first creation
 | ||
5. Every 10 mins run again</description> | ||
<keepDependencies>false</keepDependencies> | ||
<properties> | ||
<jenkins.model.BuildDiscarderProperty> | ||
<strategy class="hudson.tasks.LogRotator"> | ||
<daysToKeep>-1</daysToKeep> | ||
<numToKeep>5</numToKeep> | ||
<artifactDaysToKeep>-1</artifactDaysToKeep> | ||
<artifactNumToKeep>-1</artifactNumToKeep> | ||
</strategy> | ||
</jenkins.model.BuildDiscarderProperty> | ||
<com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty plugin="[email protected]"> | ||
<doNotScan>false</doNotScan> | ||
</com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty> | ||
<com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="[email protected]"> | ||
<gitLabConnection></gitLabConnection> | ||
</com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty> | ||
<org.jenkinsci.plugins.gitlablogo.GitlabLogoProperty plugin="[email protected]"> | ||
<repositoryName></repositoryName> | ||
</org.jenkinsci.plugins.gitlablogo.GitlabLogoProperty> | ||
<org.jenkinsci.plugins.gogs.GogsProjectProperty plugin="[email protected]"> | ||
<gogsSecret></gogsSecret> | ||
<gogsUsePayload>false</gogsUsePayload> | ||
</org.jenkinsci.plugins.gogs.GogsProjectProperty> | ||
<com.sonyericsson.rebuild.RebuildSettings plugin="[email protected]"> | ||
<autoRebuild>false</autoRebuild> | ||
<rebuildDisabled>false</rebuildDisabled> | ||
</com.sonyericsson.rebuild.RebuildSettings> | ||
</properties> | ||
<scm class="hudson.scm.NullSCM"/> | ||
<assignedNode>master</assignedNode> | ||
<canRoam>false</canRoam> | ||
<disabled>false</disabled> | ||
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> | ||
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> | ||
<triggers> | ||
<hudson.triggers.TimerTrigger> | ||
<spec>H/10 * * * *</spec> | ||
</hudson.triggers.TimerTrigger> | ||
</triggers> | ||
<concurrentBuild>false</concurrentBuild> | ||
<builders> | ||
<javaposse.jobdsl.plugin.ExecuteDslScripts plugin="[email protected]"> | ||
<scriptText>// Groovy script used to seed Jenkins with multi-branch pipeline jobs: | ||
// 1. Call GitLab API to get each git project in a given group | ||
// 2. Check if project is archived, if so skip it. | ||
// 3. Check if there is a Jenkinsfile (on master) in each of the found projects | ||
// 4. Generate a pipeline using the Jenkinsfile and add it to the queue on first creation | ||
// 5. Every 10 mins run again | ||
|
||
def gitlabHost = System.getenv("GITLAB_HOST") ?: "https://gitlab.apps.proj.example.com" | ||
def gitlabToken = System.getenv("GITLAB_TOKEN") ?: "mytoken123" | ||
def projectName = System.getenv("GITLAB_GROUP_NAME") ?: "rht-labs" | ||
def projectsApi = new URL("${gitlabHost}/api/v4/groups/${projectName}/projects?per_page=100") | ||
|
||
|
||
try { | ||
def projects = new groovy.json.JsonSlurper().parse(projectsApi.newReader(requestProperties: ['PRIVATE-TOKEN': gitlabToken])) | ||
|
||
projects.each { | ||
def project = "${it.path}" | ||
def gitPath = it.http_url_to_repo | ||
|
||
if (it.archived) { | ||
print "skipping project ${project} because it has been archived\n\n" | ||
return | ||
} | ||
|
||
try { | ||
def filesApi = new URL("${gitlabHost}/api/v4/projects/${it.id}/repository/files/Jenkinsfile?ref=master") | ||
def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['PRIVATE-TOKEN': gitlabToken])) | ||
|
||
if (!jenkins.model.Jenkins.instance.getItemByFullName(project)) { | ||
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" | ||
queue(project) | ||
} | ||
|
||
// Build Jenkins multibranc jobs | ||
multibranchPipelineJob(project) { | ||
branchSources { | ||
git { | ||
remote(gitPath) | ||
credentialsId('labs-ci-cd-jenkins-git-password') | ||
} | ||
} | ||
triggers { | ||
periodic(1) | ||
} | ||
orphanedItemStrategy { | ||
discardOldItems { | ||
numToKeep(10) | ||
} | ||
} | ||
} | ||
} | ||
catch(Exception e) { | ||
println e | ||
print "skipping project ${project} because it has no Jenkinsfile\n\n" | ||
} | ||
} | ||
} catch(Exception e) { | ||
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" | ||
throw e | ||
}</scriptText> | ||
<usingScriptText>true</usingScriptText> | ||
<sandbox>false</sandbox> | ||
<ignoreExisting>true</ignoreExisting> | ||
<ignoreMissingFiles>false</ignoreMissingFiles> | ||
<failOnMissingPlugin>false</failOnMissingPlugin> | ||
<unstableOnDeprecation>false</unstableOnDeprecation> | ||
<removedJobAction>DELETE</removedJobAction> | ||
<removedViewAction>DELETE</removedViewAction> | ||
<removedConfigFilesAction>DELETE</removedConfigFilesAction> | ||
<lookupStrategy>JENKINS_ROOT</lookupStrategy> | ||
</javaposse.jobdsl.plugin.ExecuteDslScripts> | ||
</builders> | ||
<publishers/> | ||
<buildWrappers> | ||
<hudson.plugins.ansicolor.AnsiColorBuildWrapper plugin="[email protected]"> | ||
<colorMapName>xterm</colorMapName> | ||
</hudson.plugins.ansicolor.AnsiColorBuildWrapper> | ||
</buildWrappers> | ||
</project> |