Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jenkins Job DSL Example - Bitbucket Branches
//
// Jenkins Job DSL example to create build projects for Bitbucket branches
//
// Imports
import java.text.DateFormat
import java.text.SimpleDateFormat
import groovy.time.TimeCategory
// URL components
String baseUrl = "https://bitbucket.org/api"
String version = "1.0"
String organization = "i4niac"
String repository = "flappy-swift"
// Put it all together
String branchesUrl = [baseUrl, version, "repositories", organization, repository, "branches"].join("/")
Boolean enableAuthentication = false
// Create URL
URL url = branchesUrl.toURL()
// Open connection
URLConnection connection = url.openConnection()
if (enableAuthentication) {
String username = "i4niac"
String password = "mypassword"
// 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()
// Get JSON output
def branchesJson = new groovy.json.JsonSlurper().parseText(inputStream.text)
// Close the stream
inputStream.close()
// Optional: set system proxy
Boolean setProxy = false
if (setProxy) {
String host = "myproxyhost.com.au"
String port = 8080
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", host);
System.getProperties().put("proxyPort", port);
}
// Note: no def or type used to declare this variables!
// List with names of major branches
majorBranches = ["master", "development", "release"]
// List with valid branch prefixes
validBranchPrefixes = ["feature", "bugfix", "hotfix"]
// All valid prefixes
allValidPrefixes = majorBranches + validBranchPrefixes
// Check if the branch is a valid branch
Boolean isValidBranch(String name) {
String prefix = name.split("/")[0]
prefix in allValidPrefixes
}
// Check if the branch is not too old
Boolean isUpToDateBranch(String branch, Date date) {
// major branches are considered as always up to date
if (branch in majorBranches) {
true
} else {
def maxBranchAgeInDays = 15
Date now = new Date()
use (TimeCategory) {
date.before(now) && date.after(now - maxBranchAgeInDays.days)
}
}
}
// Iterate through branches JSON
branchesJson.each { branchName, details ->
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
Date lastModified = dateFormat.parse(details["timestamp"])
// Check if branch name and age are valid
if (isValidBranch(branchName) && isUpToDateBranch(branchName, lastModified)) {
// Branch is valid, create the job for it
println "Valid branch: ${branchName}"
// Configure the job
job {
name branchName.replaceAll('/','-')
// TODO: the rest of Jenkins job configuration
}
}
}
@mneuhaeuser

This comment has been minimized.

Show comment
Hide comment
@mneuhaeuser

mneuhaeuser Jul 8, 2016

thanks, great example

thanks, great example

@navidurrahman

This comment has been minimized.

Show comment
Hide comment

Thank man, I extended a little bit as project wise.

https://gist.github.com/navidurrahman/551af240b2b2ba5769d86e7667e5ad96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment