Skip to content

Instantly share code, notes, and snippets.

@cccaternberg
Created October 22, 2018 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cccaternberg/cf48c864e9886e0b54b25bb72d1dcc2f to your computer and use it in GitHub Desktop.
Save cccaternberg/cf48c864e9886e0b54b25bb72d1dcc2f to your computer and use it in GitHub Desktop.
Bitbucket from Groovy
#!groovy
/**
API Base URL - by default it’s https://bitbucket.org/api
API Version - 1.0 or 2.0
API Endpoint Path - includes the following
“repositories” - since we want to use one of the repositories API
Organization Name - aka team or account name
Repository Name - repository slug
Repositories API Endpoint - branches since we want to get list of branches
*/
https://caternberg@bitbucket.org/JenkinsWS/tomee-rest.git
def config = Config.getConfig()
String baseUrl = "https://bitbucket.org/api"
String version = "1.0"
String organization = "jenkinsws"
String repository = "tomee-rest"
// Create authorization header using Base64 encoding
println config.credentials
def userCredentials = Config.lookupCredentials(config.credentials)
String userpass = userCredentials.username + ":" + userCredentials.password;
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
// put it all together
String branchesUrl = [baseUrl, version, "repositories", organization, repository, "branches"].join("/")
println "URL" + branchesUrl
// Create URL
URL apiUrl = branchesUrl.toURL()
// Open connection
URLConnection connection = apiUrl.openConnection()
// Set authorization header
connection.setRequestProperty("Authorization", basicAuth)
// Open input stream
InputStream inputStream = connection.getInputStream()
// Get JSON output
def branchesJson = new groovy.json.JsonSlurper().parseText(inputStream.text)
def project = [organization, repository].join("/")
branchesJson.each {
def branchName = it.getKey()
def scmUrl = "https://bitbucket.org/" + organization + "/" + repository + ".git"
println "BRANCH NAME" + branchName
println "SCM URL" + scmUrl
job("/folder_dsl/BB-Build--${project}-${branchName}".replaceAll('/', '-')) {
scm {
git {
remote {
name("bitbucket")
branch(branchName)
url(scmUrl)
credentials(config.credentials)
}
extensions {
cleanBeforeCheckout()
wipeOutWorkspace()
cloneOptions {
timeout(240)
}
}
}
}
triggers {
scm("*/1 * * * *")
}
steps {
maven {
goals('clean')
goals('test')
mavenOpts('-Xms256m')
mavenOpts('-Xmx512m')
properties('maven.test.failure.ignore': true)
mavenInstallation('M3')
}
}
publishers {
archiveJunit('**/target/surefire-reports/*.xml')
}
}
}
// Close the stream
inputStream.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment