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..d6a36d5 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. @@ -12,7 +13,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,15 +21,23 @@ def githubAccount = System.getenv("GITHUB_ACCOUNT") def githubOrg = System.getenv("GITHUB_ORG") ?: false //eg https://api.github.com/users/springdo/repos or -def githubProjects = githubOrg ? new URL("${githubHost}/orgs/${githubAccount}/repos?per_page=100") : new URL("${githubHost}/users/${githubAccount}/repos?per_page=100") +// BITBUCKET +def bitbucketHost = System.getenv("BITBUCKET_HOST") ?: "https://bitbucket.org/repo" +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") 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" def librariesDir = System.getenv("JTE_LIBRARIES_DIR") ?: "libraries" + def credentialsId = System.getenv("SSH_ACCESS_KEY") ?: "Access-Key" // Build Jenkins multibranch jobs multibranchPipelineJob(project) { @@ -37,7 +45,7 @@ def createMultibranchPipelineJob(project, gitPath, jte) { git { id("${project}") remote(gitPath) - credentialsId("${buildNamespace}-${buildGitAuthSecret}") + credentialsId("${credentialsId}") } } triggers { @@ -123,6 +131,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])) @@ -214,6 +223,71 @@ if (gitlabToken) { print "\n\n Oops! something went wrong..... Try setting the GITHUB_* Env Vars \n\n\n" throw e } +} else if (bitbucketAuth) { + try { + 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 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 + // => Jenkins classic + // 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...." + } + + 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* 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 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