-
-
Save briscula/fa562f56f825ad1da172696c57c8179c to your computer and use it in GitHub Desktop.
Jenkins Job DSL Example - Bitbucket project and branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// URL components | |
String baseUrl = "https://bitbucket.org/rest/api" | |
String version = "1.0" | |
String project = "SYS" | |
// Put it all together | |
String reposUrl = [baseUrl, version, "projects", project, "repos"].join("/") | |
def send_request(url_string){ | |
// Create URL | |
Boolean enableAuthentication = true | |
URL url = url_string.toURL() | |
// Open connection | |
URLConnection connection = url.openConnection() | |
if (enableAuthentication) { | |
String username = "dummy_user" | |
String password = "dummy_pass" | |
// Create authorization header using Base64 encoding | |
String userpass = username + ":" + password; | |
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); | |
// Set authorization header | |
connection.setRequestProperty ("Authorization", basicAuth) | |
} | |
// Open input stream | |
InputStream inputStream = connection.getInputStream() | |
json_data = new groovy.json.JsonSlurper().parseText(inputStream.text) | |
// Close the stream | |
inputStream.close() | |
return json_data | |
} | |
def reposJson = send_request(reposUrl) | |
def repos = reposJson.get('values') | |
repos.each { repo_item-> | |
def repoName = repo_item.get('name') | |
String branchesUrl = [baseUrl, version, "projects", project, "repos" ,repoName, "branches"].join("/") | |
// Get JSON output | |
def branchesJson = send_request(branchesUrl) | |
def branches = branchesJson.get('values') | |
// Iterate through branches JSON | |
branches.each { branch_item-> | |
def branchName = branch_item.get('displayId') | |
// Check if branch name and age are valid | |
def jobName = "${project}-${repoName}-${branchName}".replaceAll('/', '-') | |
job(jobName) { | |
scm { | |
// git or bitbucket URL | |
git("git://github.com/${project}/${repoName}.git", branchName) | |
} | |
steps { | |
maven("test -Dproject.name=${repoName}/${branchName}") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment