Multibranch Pipeline: Discover Tags, but build needs the BRANCH_NAME

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Multibranch Pipeline: Discover Tags, but build needs the BRANCH_NAME

Sverre Moe-2
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 ?

--
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 [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Multibranch Pipeline: Discover Tags, but build needs the BRANCH_NAME

Imran Raza Khan
Have you tried scm.branch[0].name

On Thu, 5 Jul 2018, 15:06 Sverre Moe, <[hidden email]> wrote:
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 ?

--
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 [hidden email].
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAA9TSH3__GoJVJomtN0vmDk_2fA9Fk-F-LDpoXN%2BOoBDeBA%2Bxg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Multibranch Pipeline: Discover Tags, but build needs the BRANCH_NAME

Sverre Moe-2
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. 

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/6d0adcff-4206-407c-af26-b355b388d207%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Multibranch Pipeline: Discover Tags, but build needs the BRANCH_NAME

stephenconnolly
In reply to this post by Sverre Moe-2


On Thu 5 Jul 2018 at 14:06, Sverre Moe <[hidden email]> wrote:
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

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 [hidden email].
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

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CA%2BnPnMz3Geb_MKCnYyBOUcVBYjMhe8V7xsqrh8WsLreC6mk4Eg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.