Skip to content

Instantly share code, notes, and snippets.

@mgrebenets
Created June 8, 2015 01:02
Show Gist options
  • Save mgrebenets/d3b5dcabb7606f3d7863 to your computer and use it in GitHub Desktop.
Save mgrebenets/d3b5dcabb7606f3d7863 to your computer and use it in GitHub Desktop.
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
Copy link

thanks, great example

@navidurrahman
Copy link

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