Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion configuration/jobs/seed-multibranch-pipelines/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down Expand Up @@ -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"
}</scriptText>
<usingScriptText>true</usingScriptText>
<sandbox>false</sandbox>
Expand Down
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.
Expand All @@ -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....
Expand All @@ -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"
Copy link
Collaborator

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? 🙃

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}")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 kubernetes.io/ssh-auth). It'd be the same as username/password secret. And you get to use BUILD_GIT_AUTH_SECRET and doesn't have to change credentialsId

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@springdo tagging you to check if you have other suggestions ☺️

}
}
triggers {
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions plugins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
github-branch-source:2.11.1