Multibranch Pipeline: Discover Tags, but build needs the BRANCH_NAME

2,513 views
Skip to first unread message

Sverre Moe

unread,
Jul 5, 2018, 9:06:29 AM7/5/18
to Jenkins Users
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 ?

Imran Raza Khan

unread,
Jul 5, 2018, 2:35:36 PM7/5/18
to jenkins...@googlegroups.com
Have you tried scm.branch[0].name

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/46d9e92a-5872-4d14-a4b6-c103e136039f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sverre Moe

unread,
Jul 6, 2018, 2:12:23 AM7/6/18
to Jenkins Users
torsdag 5. juli 2018 20.35.36 UTC+2 skrev Imran Raza Khan følgende:
Have you tried scm.branch[0].name


That gave me a NullPointerException
java.lang.NullPointerException: Cannot invoke method getAt() on null object


Tried scm.getBranches().get(0).name, but that just gave me the tag name. 

Stephen Connolly

unread,
Jul 10, 2018, 2:50:21 AM7/10/18
to jenkins...@googlegroups.com
You need to do git magic because git doesn’t know what “branch” a tag is from

Branches are just named pointers.

The name *you* pick on your local clone need not be the same as the name I pick for mine.

Hence your ‘master’ might be my ‘junk’

So what you want to do is use a specific repo as the “canonical” repo and find the nearest named mutable pointer in that repo

That will require pulling all refs from the canonical, not pulling just the tag (slower builds, but you have non-standard requirements, and that is the cost you pay) and then you can ask git for the branch name in the canonical repo that is closest to the tag (and happens before)



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 ?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/46d9e92a-5872-4d14-a4b6-c103e136039f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Sent from my phone
Reply all
Reply to author
Forward
0 new messages