From 3bab74403e41255ef57e6c5a469eabb4f27b4850 Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Fri, 23 Jul 2021 12:43:21 +0100 Subject: [PATCH 1/9] added support for bitbucket --- .../seed-multibranch-pipelines/config.xml | 56 +++++++++++++++++- .../seed-multibranch-pipeline.groovy | 57 ++++++++++++++++++- 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/configuration/jobs/seed-multibranch-pipelines/config.xml b/configuration/jobs/seed-multibranch-pipelines/config.xml index d24e1dd..c2b284f 100644 --- a/configuration/jobs/seed-multibranch-pipelines/config.xml +++ b/configuration/jobs/seed-multibranch-pipelines/config.xml @@ -69,6 +69,12 @@ def githubAccount = System.getenv("GITHUB_ACCOUNT") def githubOrg = System.getenv("GITHUB_ORG") ?: false //eg https://api.github.com/users/springdo/repos or +// BITBUCKET +def bitbucketHost = System.getenv("BITBUCKET_HOST") ?: "https://bitbucket.org/repo" +def bitbucketToken = System.getenv("BITBUCKET_TOKEN") +def bitbucketProjectKey = System.getenv("BITBUCKET_PROJECT_KEY") ?: "rht-labs" +def bitbucketProjectsApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos?limit=100") + def githubProjects = githubOrg ? new URL("${githubHost}/orgs/${githubAccount}/repos?per_page=100") : new URL("${githubHost}/users/${githubAccount}/repos?per_page=100") @@ -262,8 +268,56 @@ if (gitlabToken) { print "\n\n Oops! something went wrong..... Try setting the GITHUB_* Env Vars \n\n\n" throw e } +} else if (bitbucketToken) { + try { + def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + + projects.each { + def project = "${it.path}" + def repoPath = it.http_url_to_repo + def repositorySlug = "${it.id}" + + if (it.archived) { + println "skipping project ${project} because it has been archived\n\n" + return + } + + // 1. Check for "${gitlabHost}/api/v4/projects/${it.id}/repository/files/pipeline_config.groovy?ref=master" + // => JTE + // 2. Check for Jenkinsfile + // => Jenkins classic + // else - bail and do nothing + try { + def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/pipeline_config.groovy?ref=master") + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + println "😘 JTE pipeline_config.groovy found in ${project} 🥳" + createMultibranchPipelineJob(project, repoPath, true) + addJobToQueue(project) + return; + + } + catch(Exception e) { + println e + println "JTE pipeline_config.groovy not found in ${project}. Checking for Jenkinsfile \n\n" + } + try { + def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/Jenkinsfile?ref=master") + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + println "😘 Jenkinsfile found in ${project} 🥳" + createMultibranchPipelineJob(project, repoPath, false) + addJobToQueue(project) + } + catch(Exception e) { + println e + println "skipping project ${project} because it has no Jenkinsfile\n\n" + } + } + } catch(Exception e) { + print "\n\n Please make sure you have set BITBUCKET_HOST, BITBUCKET_TOKEN and BITBUCKET_PROJECT_KEY in your deploy config for Jenkins \n\n\n" + throw e + } } else { - print "\n\n No tokens set in the Environment eg GITHUB* or GITLAB* so not sure what to do ..... 路‍♂️ \n\n\n" + print "\n\n No tokens set in the Environment eg GITHUB* or GITLAB or BITBUCKET* so not sure what to do ..... 路‍♂️ \n\n\n" } true false diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index abd30dc..a4270b3 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -12,7 +12,6 @@ def gitlabToken = System.getenv("GITLAB_TOKEN") def groupName = System.getenv("GITLAB_GROUP_NAME") ?: "rht-labs" def gitlabProjectsApi = new URL("${gitlabHost}/api/v4/groups/${groupName}/projects?per_page=100") - // GITHUB def githubHost = "https://api.github.com" // Token needed for rate limiting issues.... @@ -21,6 +20,12 @@ def githubAccount = System.getenv("GITHUB_ACCOUNT") def githubOrg = System.getenv("GITHUB_ORG") ?: false //eg https://api.github.com/users/springdo/repos or +// BITBUCKET +def bitbucketHost = System.getenv("BITBUCKET_HOST") ?: "https://bitbucket.org/repo" +def bitbucketToken = System.getenv("BITBUCKET_TOKEN") +def bitbucketProjectKey = System.getenv("BITBUCKET_PROJECT_KEY") ?: "rht-labs" +def bitbucketProjectsApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos?limit=100") + def githubProjects = githubOrg ? new URL("${githubHost}/orgs/${githubAccount}/repos?per_page=100") : new URL("${githubHost}/users/${githubAccount}/repos?per_page=100") @@ -214,6 +219,54 @@ if (gitlabToken) { print "\n\n Oops! something went wrong..... Try setting the GITHUB_* Env Vars \n\n\n" throw e } +} else if (bitbucketToken) { + try { + def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + + projects.each { + def project = "${it.path}" + def repoPath = it.http_url_to_repo + def repositorySlug = "${it.id}" + + if (it.archived) { + println "skipping project ${project} because it has been archived\n\n" + return + } + + // 1. Check for "${gitlabHost}/api/v4/projects/${it.id}/repository/files/pipeline_config.groovy?ref=master" + // => JTE + // 2. Check for Jenkinsfile + // => Jenkins classic + // else - bail and do nothing + try { + def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/pipeline_config.groovy?ref=master") + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + println "😘 JTE pipeline_config.groovy found in ${project} 🥳" + createMultibranchPipelineJob(project, repoPath, true) + addJobToQueue(project) + return; + + } + catch(Exception e) { + println e + println "JTE pipeline_config.groovy not found in ${project}. Checking for Jenkinsfile \n\n" + } + try { + def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/Jenkinsfile?ref=master") + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + println "😘 Jenkinsfile found in ${project} 🥳" + createMultibranchPipelineJob(project, repoPath, false) + addJobToQueue(project) + } + catch(Exception e) { + println e + println "skipping project ${project} because it has no Jenkinsfile\n\n" + } + } + } catch(Exception e) { + print "\n\n Please make sure you have set BITBUCKET_HOST, BITBUCKET_TOKEN and BITBUCKET_PROJECT_KEY in your deploy config for Jenkins \n\n\n" + throw e + } } else { - print "\n\n No tokens set in the Environment eg GITHUB* or GITLAB* so not sure what to do ..... 路‍♂️ \n\n\n" + print "\n\n No tokens set in the Environment eg GITHUB* or GITLAB or BITBUCKET* so not sure what to do ..... 路‍♂️ \n\n\n" } \ No newline at end of file From 02b290e5973e7e4502cc217a3b7dd4109c73a581 Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Sun, 25 Jul 2021 18:14:15 +0100 Subject: [PATCH 2/9] Updated token setup for bitbucket api request --- .../seed-multibranch-pipeline.groovy | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index a4270b3..6fbd652 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -221,12 +221,12 @@ if (gitlabToken) { } } else if (bitbucketToken) { try { - def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['Authorization': 'Bearer ' + bitbucketToken])) - projects.each { - def project = "${it.path}" - def repoPath = it.http_url_to_repo - def repositorySlug = "${it.id}" + projects.values.each { + def project = "${it.project}" + def repoPath = it.links.self.href + def repositorySlug = "${it.slug}" if (it.archived) { println "skipping project ${project} because it has been archived\n\n" @@ -240,7 +240,7 @@ if (gitlabToken) { // else - bail and do nothing try { def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/pipeline_config.groovy?ref=master") - def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': 'Bearer ' + bitbucketToken])) println "😘 JTE pipeline_config.groovy found in ${project} 🥳" createMultibranchPipelineJob(project, repoPath, true) addJobToQueue(project) @@ -253,7 +253,7 @@ if (gitlabToken) { } try { def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/Jenkinsfile?ref=master") - def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['x-token-auth': bitbucketToken])) + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': 'Bearer ' + bitbucketToken])) println "😘 Jenkinsfile found in ${project} 🥳" createMultibranchPipelineJob(project, repoPath, false) addJobToQueue(project) From 47a62af2a8b8d75b7f0552abfa2e4776680a5840 Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Tue, 27 Jul 2021 22:31:54 +0100 Subject: [PATCH 3/9] using basic authorization for bitbucket api call --- .../seed-multibranch-pipeline.groovy | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index 6fbd652..9829b9c 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -22,9 +22,11 @@ def githubOrg = System.getenv("GITHUB_ORG") ?: false // BITBUCKET def bitbucketHost = System.getenv("BITBUCKET_HOST") ?: "https://bitbucket.org/repo" -def bitbucketToken = System.getenv("BITBUCKET_TOKEN") +def bitbucketUser = System.getenv("BITBUCKET_USER") +def bitbucketPassword = System.getenv("BITBUCKET_PASSWORD") def bitbucketProjectKey = System.getenv("BITBUCKET_PROJECT_KEY") ?: "rht-labs" def bitbucketProjectsApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos?limit=100") +def bitbucketAuth = (bitbucketUser+":"+bitbucketPassword).getBytes().encodeBase64().toString(); def githubProjects = githubOrg ? new URL("${githubHost}/orgs/${githubAccount}/repos?per_page=100") : new URL("${githubHost}/users/${githubAccount}/repos?per_page=100") @@ -219,9 +221,9 @@ if (gitlabToken) { print "\n\n Oops! something went wrong..... Try setting the GITHUB_* Env Vars \n\n\n" throw e } -} else if (bitbucketToken) { +} else if (bitbucketAuth) { try { - def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['Authorization': 'Bearer ' + bitbucketToken])) + def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['Authorization': "Basic ${bitbucketAuth}"])) projects.values.each { def project = "${it.project}" @@ -240,7 +242,7 @@ if (gitlabToken) { // else - bail and do nothing try { def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/pipeline_config.groovy?ref=master") - def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': 'Bearer ' + bitbucketToken])) + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': "Basic ${bitbucketAuth}"])) println "😘 JTE pipeline_config.groovy found in ${project} 🥳" createMultibranchPipelineJob(project, repoPath, true) addJobToQueue(project) @@ -253,7 +255,7 @@ if (gitlabToken) { } try { def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/Jenkinsfile?ref=master") - def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': 'Bearer ' + bitbucketToken])) + def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': "Basic ${bitbucketAuth}"])) println "😘 Jenkinsfile found in ${project} 🥳" createMultibranchPipelineJob(project, repoPath, false) addJobToQueue(project) From baf0f0816cf8c6c10d30fa6dee5eb99d9ee24052 Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Tue, 27 Jul 2021 22:36:59 +0100 Subject: [PATCH 4/9] updated error message --- .../seed-multibranch-pipelines/seed-multibranch-pipeline.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index 9829b9c..76c6b96 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -266,7 +266,7 @@ if (gitlabToken) { } } } catch(Exception e) { - print "\n\n Please make sure you have set BITBUCKET_HOST, BITBUCKET_TOKEN and BITBUCKET_PROJECT_KEY in your deploy config for Jenkins \n\n\n" + print "\n\n Please make sure you have set BITBUCKET_HOST, BITBUCKET_USER, BITBUCKET_PASSWORD and BITBUCKET_PROJECT_KEY in your deploy config for Jenkins \n\n\n" throw e } } else { From 81b3483c463c8abb6ff26940da031d836692fb1b Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Wed, 28 Jul 2021 08:48:51 +0100 Subject: [PATCH 5/9] added debug statements --- .../seed-multibranch-pipeline.groovy | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index 76c6b96..c8b203a 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -27,6 +27,7 @@ def bitbucketPassword = System.getenv("BITBUCKET_PASSWORD") def bitbucketProjectKey = System.getenv("BITBUCKET_PROJECT_KEY") ?: "rht-labs" def bitbucketProjectsApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos?limit=100") def bitbucketAuth = (bitbucketUser+":"+bitbucketPassword).getBytes().encodeBase64().toString(); +println "bitbucketAuth: ${bitbucketAuth}" def githubProjects = githubOrg ? new URL("${githubHost}/orgs/${githubAccount}/repos?per_page=100") : new URL("${githubHost}/users/${githubAccount}/repos?per_page=100") @@ -223,8 +224,9 @@ if (gitlabToken) { } } else if (bitbucketAuth) { try { + println "Before Bitbucket authorization..." def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['Authorization': "Basic ${bitbucketAuth}"])) - + println "After Bitbucket authorization..." projects.values.each { def project = "${it.project}" def repoPath = it.links.self.href From 24ae0efc7e440ab5e7d5b65faa47e00360140364 Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Wed, 28 Jul 2021 09:15:06 +0100 Subject: [PATCH 6/9] added more debug statement --- .../seed-multibranch-pipeline.groovy | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index c8b203a..4d8c3de 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -22,9 +22,13 @@ def githubOrg = System.getenv("GITHUB_ORG") ?: false // BITBUCKET def bitbucketHost = System.getenv("BITBUCKET_HOST") ?: "https://bitbucket.org/repo" +println "bitbucketHost: ${bitbucketHost}" def bitbucketUser = System.getenv("BITBUCKET_USER") +println "bitbucketUser: ${bitbucketUser}" def bitbucketPassword = System.getenv("BITBUCKET_PASSWORD") +println "bitbucketPassword: ${bitbucketPassword}" def bitbucketProjectKey = System.getenv("BITBUCKET_PROJECT_KEY") ?: "rht-labs" +println "bitbucketProjectKey: ${bitbucketProjectKey}" def bitbucketProjectsApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos?limit=100") def bitbucketAuth = (bitbucketUser+":"+bitbucketPassword).getBytes().encodeBase64().toString(); println "bitbucketAuth: ${bitbucketAuth}" @@ -131,6 +135,7 @@ def addJobToQueue(project){ } } // if GITLAB* set .... +println "Before starting to scan bitbucket projects in ${bitbucketProjectsApi}" if (gitlabToken) { try { def projects = new groovy.json.JsonSlurper().parse(gitlabProjectsApi.newReader(requestProperties: ['PRIVATE-TOKEN': gitlabToken])) From 0094a2ef95356b09248de3db2ba326d551a14c2d Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Thu, 29 Jul 2021 18:04:45 +0100 Subject: [PATCH 7/9] updated for bit bucket support --- .../seed-multibranch-pipeline.groovy | 109 ++++++++++-------- 1 file changed, 60 insertions(+), 49 deletions(-) diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index 4d8c3de..27491c9 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -1,3 +1,4 @@ +#!/usr/bin/env groovy // 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. @@ -22,22 +23,16 @@ def githubOrg = System.getenv("GITHUB_ORG") ?: false // BITBUCKET def bitbucketHost = System.getenv("BITBUCKET_HOST") ?: "https://bitbucket.org/repo" -println "bitbucketHost: ${bitbucketHost}" def bitbucketUser = System.getenv("BITBUCKET_USER") -println "bitbucketUser: ${bitbucketUser}" def bitbucketPassword = System.getenv("BITBUCKET_PASSWORD") -println "bitbucketPassword: ${bitbucketPassword}" def bitbucketProjectKey = System.getenv("BITBUCKET_PROJECT_KEY") ?: "rht-labs" -println "bitbucketProjectKey: ${bitbucketProjectKey}" def bitbucketProjectsApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos?limit=100") def bitbucketAuth = (bitbucketUser+":"+bitbucketPassword).getBytes().encodeBase64().toString(); -println "bitbucketAuth: ${bitbucketAuth}" def githubProjects = githubOrg ? new URL("${githubHost}/orgs/${githubAccount}/repos?per_page=100") : new URL("${githubHost}/users/${githubAccount}/repos?per_page=100") - def createMultibranchPipelineJob(project, gitPath, jte) { - def buildNamespace = System.getenv("BUILD_NAMESPACE") ?: "labs-ci-cd" + def buildNamespace = System.getenv("BUILD_NAMESPACE") ?: "ocp-ci-cd" def buildGitAuthSecret = System.getenv("BUILD_GIT_AUTH_SECRET") ?: "git-auth" def jteProject = System.getenv("JTE_PROJECT") ?: "https://gitlab.apps.proj.example.com/rht-labs/pipeline-template-configuration.git" def pipelineConfigDir = System.getenv("JTE_PIPELINE_DIR") ?: "pipeline-configuration" @@ -49,7 +44,7 @@ def createMultibranchPipelineJob(project, gitPath, jte) { git { id("${project}") remote(gitPath) - credentialsId("${buildNamespace}-${buildGitAuthSecret}") + credentialsId("ROSA-Jenkins-Bitbucket-Access-Key") } } triggers { @@ -229,53 +224,69 @@ if (gitlabToken) { } } else if (bitbucketAuth) { try { - println "Before Bitbucket authorization..." - def projects = new groovy.json.JsonSlurper().parse(bitbucketProjectsApi.newReader(requestProperties: ['Authorization': "Basic ${bitbucketAuth}"])) - println "After Bitbucket authorization..." + def process = "curl -s -k -u ${bitbucketUser}:${bitbucketPassword} ${bitbucketProjectsApi}".execute() + process.waitFor() + def projects = new groovy.json.JsonSlurper().parseText(process.text) projects.values.each { - def project = "${it.project}" - def repoPath = it.links.self.href - def repositorySlug = "${it.slug}" - - if (it.archived) { - println "skipping project ${project} because it has been archived\n\n" - return - } - - // 1. Check for "${gitlabHost}/api/v4/projects/${it.id}/repository/files/pipeline_config.groovy?ref=master" + def repositorySlug = it.slug + def repoPath = "" + def clone = it.links.clone + clone.each { + def name = it.name + if(name == "ssh") { + repoPath = it.href + } + } + def project = repositorySlug + if (it.archived) { + println "skipping project ${repositorySlug} because it has been archived\n\n" + return + } + + // 1. Check for "${gitlabHost}/api/v4/projects/${it.id}/repository/files/pipeline_config.groovy?ref=master" // => JTE - // 2. Check for Jenkinsfile + // 2. Check for Jenkinsfile // => Jenkins classic - // else - bail and do nothing - try { - def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/pipeline_config.groovy?ref=master") - def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': "Basic ${bitbucketAuth}"])) - println "😘 JTE pipeline_config.groovy found in ${project} 🥳" - createMultibranchPipelineJob(project, repoPath, true) - addJobToQueue(project) - return; + // else - bail and do nothing + try { + process = "curl -s -k -u ${bitbucketUser}:${bitbucketPassword} ${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/browse/pipeline_config.groovy?ref=master".execute() + process.waitFor() + def response = new groovy.json.JsonSlurper().parseText(process.text) + def errors = response.errors + if (errors) { + throw new Exception("${errors.message}") + } + println "😘 JTE pipeline_config.groovy found in ${repositorySlug} 🥳\n" + createMultibranchPipelineJob(project, repoPath, true) + addJobToQueue(project) + return; + } + catch(Exception e) { + println e + println "JTE pipeline_config.groovy not found in ${project}. Checking for Jenkinsfile...." + } - } - catch(Exception e) { - println e - println "JTE pipeline_config.groovy not found in ${project}. Checking for Jenkinsfile \n\n" - } - try { - def filesApi = new URL("${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/files/Jenkinsfile?ref=master") - def files = new groovy.json.JsonSlurper().parse(filesApi.newReader(requestProperties: ['Authorization': "Basic ${bitbucketAuth}"])) - println "😘 Jenkinsfile found in ${project} 🥳" - createMultibranchPipelineJob(project, repoPath, false) - addJobToQueue(project) - } - catch(Exception e) { - println e - println "skipping project ${project} because it has no Jenkinsfile\n\n" - } - } - } catch(Exception e) { + try { + process = "curl -s -k -u ${bitbucketUser}:${bitbucketPassword} ${bitbucketHost}/rest/api/1.0/projects/${bitbucketProjectKey}/repos/${repositorySlug}/browse/Jenkinsfile?ref=master".execute() + process.waitFor() + def response = new groovy.json.JsonSlurper().parseText(process.text) + def errors = response.errors + if (errors) { + throw new Exception("${errors.message}") + } + println "😘 Jenkinsfile found in ${repositorySlug} 🥳 \n" + createMultibranchPipelineJob(project, repoPath, false) + addJobToQueue(project) + } + catch(Exception e) { + println e + println "skipping project ${repositorySlug} because it has no Jenkinsfile \n" + } + } + } catch(Exception e) { print "\n\n Please make sure you have set BITBUCKET_HOST, BITBUCKET_USER, BITBUCKET_PASSWORD and BITBUCKET_PROJECT_KEY in your deploy config for Jenkins \n\n\n" throw e - } + } } else { print "\n\n No tokens set in the Environment eg GITHUB* or GITLAB or BITBUCKET* so not sure what to do ..... 路‍♂️ \n\n\n" } \ No newline at end of file From 3bf33dbe14ecd19719a0241f7b0680a6a6ea157a Mon Sep 17 00:00:00 2001 From: Rajeev Sachdeva Date: Fri, 30 Jul 2021 17:36:21 +0100 Subject: [PATCH 8/9] added env variable to get ssh access key id --- .../seed-multibranch-pipeline.groovy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy index 27491c9..d6a36d5 100644 --- a/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy +++ b/configuration/jobs/seed-multibranch-pipelines/seed-multibranch-pipeline.groovy @@ -37,6 +37,7 @@ def createMultibranchPipelineJob(project, gitPath, jte) { def jteProject = System.getenv("JTE_PROJECT") ?: "https://gitlab.apps.proj.example.com/rht-labs/pipeline-template-configuration.git" def pipelineConfigDir = System.getenv("JTE_PIPELINE_DIR") ?: "pipeline-configuration" def librariesDir = System.getenv("JTE_LIBRARIES_DIR") ?: "libraries" + def credentialsId = System.getenv("SSH_ACCESS_KEY") ?: "Access-Key" // Build Jenkins multibranch jobs multibranchPipelineJob(project) { @@ -44,7 +45,7 @@ def createMultibranchPipelineJob(project, gitPath, jte) { git { id("${project}") remote(gitPath) - credentialsId("ROSA-Jenkins-Bitbucket-Access-Key") + credentialsId("${credentialsId}") } } triggers { From d96a5e358fe18b20a894bae51c6a1fafb72f89cb Mon Sep 17 00:00:00 2001 From: rsachdeva-ma <84073876+rsachdeva-ma@users.noreply.github.com> Date: Wed, 11 Aug 2021 12:53:59 +0100 Subject: [PATCH 9/9] Update plugins.txt --- plugins.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins.txt b/plugins.txt index be95c83..4943a88 100644 --- a/plugins.txt +++ b/plugins.txt @@ -133,7 +133,7 @@ multibranch-scan-webhook-trigger:1.0.9 cloudbees-folder:6.15 cobertura:1.16 durable-task:1.37 -openshift-sync:1.0.47 +openshift-sync:1.0.48 generic-webhook-trigger:1.74 run-condition:1.5 ssh-slaves:1.32.0 @@ -171,4 +171,4 @@ xunit:2.3.9 pipeline-input-step:2.12 jquery-detached:1.2.1 build-monitor-plugin:1.12+build.201809061734 -github-branch-source:2.11.1 \ No newline at end of file +github-branch-source:2.11.1