-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for bitbucket #34
base: master
Are you sure you want to change the base?
Changes from all commits
3bab744
02b290e
47a62af
baf0f08
81b3483
24ae0ef
0094a2e
3bf33db
d96a5e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,23 +21,31 @@ 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) { | ||
branchSources { | ||
git { | ||
id("${project}") | ||
remote(gitPath) | ||
credentialsId("${buildNamespace}-${buildGitAuthSecret}") | ||
credentialsId("${credentialsId}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we need this. I think, instead of defining a new SSH_ACCESS_KEY variable, you can create a secret with ssh key in it (secret type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @springdo tagging you to check if you have other suggestions |
||
} | ||
} | ||
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" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we keep this as
labs-ci-cd
? 🙃