From 9557a3619113ce01ae0224bf4b8f38690e468519 Mon Sep 17 00:00:00 2001 From: donal Date: Wed, 27 Mar 2019 18:07:38 +0000 Subject: [PATCH 1/2] Adding Multibranch seed job --- .../seed-multibranch-pipelines/config.xml | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 configuration/jobs/seed-multibranch-pipelines/config.xml diff --git a/configuration/jobs/seed-multibranch-pipelines/config.xml b/configuration/jobs/seed-multibranch-pipelines/config.xml new file mode 100644 index 0000000..8ec8d89 --- /dev/null +++ b/configuration/jobs/seed-multibranch-pipelines/config.xml @@ -0,0 +1,131 @@ + + + + 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 + false + + + + -1 + 5 + -1 + -1 + + + + false + + + + + + + + + + false + + + false + false + + + + master + false + false + false + false + + + H/10 * * * * + + + false + + + // 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 +} + true + false + true + false + false + false + DELETE + DELETE + DELETE + JENKINS_ROOT + + + + + + xterm + + + From f7d9cace64933bf0c1712a1e1938f3e82831972f Mon Sep 17 00:00:00 2001 From: donal Date: Thu, 28 Mar 2019 10:11:47 +0000 Subject: [PATCH 2/2] Update to README as per PR review --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3433cd0..2396188 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,15 @@ By default the deployment will attempt to connect to SonarQube and configure its ## Git Creds 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. +## Jenkins DSL Seed for MultiBranch Pipelines (GitLab) + +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. +``` +GITLAB_HOST is the Http address of the GitLab Project eg 'https://gitlab.apps.proj.example.com' +GITLAB_TOKEN is the GitLab API token to access repos and projects eg 'token123' +GITLAB_GROUP_NAME is the GitLab group name where projects are stored eg 'rht-labs' +``` + ## Shared Library 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 ## Contributing There are some [helpers](helpers/README.MD) to get configuration out of a running Jenkins. -