I added "Discover tags" in Git checkout configuration on Multibranch Pipeline.
I want to use the tag jobs to build and publish releases in Jenkins.
To be used with both Maven and Gradle.
* With maven running mvn release to produce the git tag and version bumps.
* With gradle just bump up the version and create a git tag. At least for now (looking into using a plugin).
My builds produce a number of RPMs that are published to a yum repository.
For release branch origin/ReleaseA it will publish the RPMs to /repository/ReleaseA/sles12.3/noarch/
def releaseRepository = "/repository/${env.BRANCH_NAME}/${DISTRIBUTION}/noarch/"
Here comes my little problem. It doesn't know the branch name since BRANCH_NAME is same as TAG_NAME.
This actually makes sense because Jenkins does not know about the branch, just the tag.
final def tagName = env.TAG_NAME
final def branchName = env.BRANCH_NAME
final def buildEnvironment = getBuildEnvironment(branchName)
final def buildNode = getBuildNode(buildEnvironment)
final def releaseBuild = tagName != null && !tagName.isEmpty()
node(buildNode) {
stage("Checkout") {
checkout scm
}
stage("Build") {
withMaven() {
sh "mvn --batch-mode deploy"
}
stash allowEmpty: false, includes: 'target/**/*.rpm', name: 'rpms'
}
stage("Publish") {
publish(branchName)
}
}
def publish(branchName) {...}
def getBuildEnvironment(branchName) {...}
def getBuildNode(buildEnvironment) {...}
I don't see any way how to get the closest branch to the git tag name other than to:
1) Do the checkout on the default buildNode found by getBuildNode (hence for master).
2) Perform git magic to find closest branch name
3) Do the Checkout+Build+Publish stages
This is what I have come up with.
def branchName
node(buildNode) {
stage("Checkout") {
checkout scm
if (branchName.equals(tagName)) {
branchName = gitMagic()
}
sh "git archive --format=tar.gz" --output=archive.tar.gz ${tagName}
stash allowEmpty: false, includes: '*.tar.gz', name: 'archive'
}
}
final def buildEnvironment = getBuildEnvironment(branchName)
final def buildNode = getBuildNode(buildEnvironment)
node(buildNode) {
stage("Build") {
unstash 'archive'
withMaven() {
sh "mvn --batch-mode deploy"
}
stash allowEmpty: false, includes: 'target/**/*.rpm', name: 'rpms'
}
stage("Publish") {
publish(branchName)
}
}
Is there a better way to determine the git branch name, that would allow me to keep the same build script for both Tags and Branches ?