cannot get build causes

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

cannot get build causes

Tom Brownridge
Hello,

I am new and am fundamentally misunderstanding pipeline syntax and cannot relate the examples in the documentation. What am I missing?

Looking at the example here: https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/get-build-cause/getBuildCause.groovy, I see a code snippet like this:

def causes = currentBuild.getBuildCauses()

But when I try to write a pipeline script to use it:

pipeline {
    agent any
    stages {
        stage('example') {
            steps {
                def causes = currentBuild.getBuildCauses()
                echo "Causes are ${causes}"
            }
        }
    }
}

I get the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 6: Expected a step @ line 6, column 17.
                   def causes = currentBuild.getBuildCauses()
                   ^

1 error

	at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
	at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
	at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
	at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
	at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
	at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
	at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE

Thanks in advance.

Sincerely,
Tom





--
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/da7f2288-490d-40f5-ad6f-693ea3869bdbo%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: cannot get build causes

Gavin Davies-2
Try nesting that in a script block 

pipeline {
    agent any
    stages {
        stage('example') {
            steps {
              script {
                def causes = currentBuild.getBuildCauses()
                echo "Causes are ${causes}"
              }
            }
        }
    }
}

On Wed, Jul 29, 2020 at 10:04 PM Tom Brownridge <[hidden email]> wrote:
Hello,

I am new and am fundamentally misunderstanding pipeline syntax and cannot relate the examples in the documentation. What am I missing?


def causes = currentBuild.getBuildCauses()

But when I try to write a pipeline script to use it:

pipeline {
    agent any
    stages {
        stage('example') {
            steps {
                def causes = currentBuild.getBuildCauses()
                echo "Causes are ${causes}"
            }
        }
    }
}

I get the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 6: Expected a step @ line 6, column 17.
                   def causes = currentBuild.getBuildCauses()
                   ^

1 error

	at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
	at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
	at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
	at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
	at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
	at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
	at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE

Thanks in advance.

Sincerely,
Tom





--
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/da7f2288-490d-40f5-ad6f-693ea3869bdbo%40googlegroups.com.


--
BackbaseGavin Davies
Senior System Engineer · R&D BA Cardiff Tribe · Cardiff
O +31 (0) 20 465 8888
LinkedIn · Twitter · Careers

--
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/CAONG3Zw4GDUUAzV2JL7GSk%2BWNYwuPXc%2B3eAHMg3D23K23YvhpA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: cannot get build causes

Gianluca
In reply to this post by Tom Brownridge
Hi,
that is a confusing error in my point of view that you will learn to interpret :-) 

The pipeline you wrote is a declarative one and the usage of "def ... " is something you can do only in scripted pipelines ... hence, what the error really means is: "You forgot to open a script block".

The fix is just to wrap inside a "script" block that allow to execute scripted portion inside a declarative pipeline:

pipeline {
    agent any
    stages {
        stage('example') {
            steps {
                script {
                  def causes = currentBuild.getBuildCauses()
                  echo "Causes are ${causes}"
                }
            }
        }
    }
}

On Wednesday, 29 July 2020 22:04:44 UTC+1, Tom Brownridge wrote:
Hello,

I am new and am fundamentally misunderstanding pipeline syntax and cannot relate the examples in the documentation. What am I missing?

Looking at the example here: <a href="https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/get-build-cause/getBuildCause.groovy" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fjenkinsci%2Fpipeline-examples%2Fblob%2Fmaster%2Fpipeline-examples%2Fget-build-cause%2FgetBuildCause.groovy\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG1dG7DzqNRILwMduaAcUk7mZRBrw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fjenkinsci%2Fpipeline-examples%2Fblob%2Fmaster%2Fpipeline-examples%2Fget-build-cause%2FgetBuildCause.groovy\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG1dG7DzqNRILwMduaAcUk7mZRBrw&#39;;return true;">https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/get-build-cause/getBuildCause.groovy, I see a code snippet like this:

def causes = currentBuild.getBuildCauses()

But when I try to write a pipeline script to use it:

pipeline {
    agent any
    stages {
        stage('example') {
            steps {
                def causes = currentBuild.getBuildCauses()
                echo "Causes are ${causes}"
            }
        }
    }
}

I get the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 6: Expected a step @ line 6, column 17.
                   def causes = currentBuild.getBuildCauses()
                   ^

1 error

	at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
	at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
	at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
	at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
	at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
	at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
	at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE

Thanks in advance.

Sincerely,
Tom





--
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/21658d43-59e3-4619-96cc-f4d54ec86299o%40googlegroups.com.