Skip to content

Instantly share code, notes, and snippets.

@filviu
Last active February 5, 2020 13:08
Show Gist options
  • Save filviu/a191ebfee006957bef9c1275bd352e18 to your computer and use it in GitHub Desktop.
Save filviu/a191ebfee006957bef9c1275bd352e18 to your computer and use it in GitHub Desktop.
Jenkins snippets
# checkout a repository with branch fallback
repos.split(',').each {
echo "Checking out ${it}"
dir("${it}"){
checkout resolveScm(
source: [
$class: 'GitSCMSource',
credentialsId: '51234-5678-9101-2131',
id: '_',
remote: "[email protected]:somebody/${it}.git",
traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]],
targets: ["${params.BLD_BRANCH}", "${params.FALL_BLD_BRANCH}"]
)
echo "========== DONE =========="
}
}
// requires python but no approval
String port = sh(script: 'echo $(python -c \'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()\');', returnStdout: true)
// requires approval, looks better
// actually doesn't work
// https://issues.jenkins-ci.org/browse/JENKINS-56330
def port = new ServerSocket(0).withCloseable { socket -> socket.getLocalPort() }
# loop through a variable and checkout each repository in it's own folder
repos.split(',').each {
echo "Checking out ${it}"
dir("${it}"){
checkout([
$class: 'GitSCM',
branches: [[name: "*/${params.BLD_BRANCH}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '1234-5678-9101-2131', url: "[email protected]:somebody/${it}.git"]]
])
echo "========== DONE =========="
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment