Skip to content

Instantly share code, notes, and snippets.

@Sysa
Created November 23, 2018 16:02
Show Gist options
  • Save Sysa/6d4085e50c1bcadcba7252b8c147fac7 to your computer and use it in GitHub Desktop.
Save Sysa/6d4085e50c1bcadcba7252b8c147fac7 to your computer and use it in GitHub Desktop.
get and filter git branch in jenkins pipeline
stage("git remote"){
withCredentials([usernamePassword(credentialsId: 'git_worker', passwordVariable: 'PASS', usernameVariable: 'USERNAME')]) {
String filterBranch = "release/"
String sh_git_result = sh(script: "git ls-remote http://${USERNAME}:${PASS}@bitbucket.yourdomain.com/scm/rpl/client.git | grep ${filterBranch}", returnStdout: true).trim()
println sh_git_result
//regex to take only `release/01.02` from `refs/heads/release/01.02-fake-239` - (?<=heads\/).*(?<=\d{2}\.\d{2})
// to take only `release/*` -> (?<=heads\/).*
branch_result = (sh_git_result =~ /(?<=heads\/).*/)[0]
println branch_result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment