Pipeline: Fails reading build cause

1,736 views
Skip to first unread message

Sverre Moe

unread,
Jun 21, 2016, 2:11:43 AM6/21/16
to Jenkins Users
I have tried reading the build cause as suggested in this example

Accessing the build cause outside of a node causes the pipeline to fail right after entering the first stage and without an error or exception.
void main(def args) {
  def causes = currentBuild.rawBuild.getCauses()
  stage
'Checkout'
  node
{

    scm checkout
 
}
}


Accessing the build cause within a node throws exception java.io.NotSerializableException: hudson.model.Cause$UserIdCause
void main(def args) {
  stage
'Checkout'
  node
{
    def causes = currentBuild.rawBuild.getCauses()
    scm checkout
 
}
}


So I put it within a @NonCPS method, but it still fails the same way as before.
void main(def args) {
stage 'Checkout'
node
{
 
def cause =
getLastBuildCause()
scm checkout
}
}

@NonCPS
def getLastBuildCause() {
    def causes = currentBuild.rawBuild.getCauses()
    return causes.last()
}

 
I need to find out who or what caused the build, to find out if it is an upstream project. Unless there is some other access methods for this?

Sverre Moe

unread,
Jun 21, 2016, 2:32:29 AM6/21/16
to Jenkins Users
I found a JENKINS issue dealing with NonCPS
https://issues.jenkins-ci.org/browse/JENKINS-31314

Tried the suggestion in the last comment and only call the NonCPS methods from the master node.
Seems I really need to define a heavyweight executor on the master. Reading the build cause on a flyweight executor fails or any other heavyweight executor than master.

Antonio Muñiz

unread,
Jun 21, 2016, 7:12:29 AM6/21/16
to jenkins...@googlegroups.com
You need to do all the work with non-serializable objects inside the @NonCPS annotated function and return a serializable object.

In your example you are storing the result of `getLastBuildCause` outside the non-cps function. Try return a `String` there.

--
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/5b3f9231-a257-4160-a6c3-987604e72331%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Antonio Muñiz
Software Engineer
CloudBees, Inc.

Reply all
Reply to author
Forward
0 new messages