How do you capture POST content parameters in code in Jenkins declarative syntax pipeline script?
This is an example of how to do it with scripted syntax:
node {
properties([
pipelineTriggers([
[$class: 'GenericTrigger',
genericVariables: [
[ key: 'committer_name', value: '$.actor.name' ],
[ key: 'committer_email', value: '$.actor.emailAddress' ],
[ key: 'ref', value: '$.changes[0].refId'],
[ key: 'tag', value: '$.changes[0].ref.id', regexpFilter: 'refs/tags/'],
[ key: 'commit', value: '$.changes[0].toHash' ],
[ key: 'repo_slug', value: '$.repository.slug' ],
[ key: 'project_key', value: '$.repository.project.key' ],
[ key: 'clone_url', value: '$.repository.links.clone[0].href' ]
],
causeString: '$committer_name pushed tag $tag to $clone_url referencing $commit',
token: 'my_token',
printContributedVariables: true,
printPostContent: true,
regexpFilterText: '$ref',
regexpFilterExpression: '^refs/tags/.*'
]
])
])
stage("Test") {
deleteDir()
def msg = "hello, world!"
emailext (subject: "Hello", \
mimeType: "text/html",\
body: "${msg} <br> \
committer_name ${committer_name} <br> \
committer_email ${committer_email} <br> \
ref ${ref} <br> \
tag ${tag} <br> \
commit ${commit} <br> \
repo_slug ${repo_slug} <br> \
project_key ${project_key} <br> \
clone_url ${clone_url} <br> \
",
from: "Jenkins@company.com", \
to: "${committer_email}")
This article claims that equivalent scripted syntax would be:
pipeline {
agent any
triggers {
GenericTrigger {
genericVariables {
genericVariable {
key( "committer_name" )
value( "\$.actor.name" )
expressionType( "JSONPath" )
}
}
token( "my_token" )
printContributedVariables(true)
printPostContent(true)
regexpFilterText( "\$ref" )
regexpFilterExpression( "^refs/tags/.*" )
}
}
stages {
stage( "Test" ) {
steps {
deleteDir()
}
}
}
(I stripped down the Test stage to reduced sources of error -- it should be inconsequential to this question).
...whereas in reality, it produces this error:
WorkflowScript: 4: Triggers definitions cannot have blocks @ line 4, column 5.
GenericTrigger {
^
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
What is correct declarative syntax "equivalent" to the working scripted syntax -- i.e. how can I capture POST content parameters with declarative syntax?
I'd specifically like to learn how to do this "in code", i.e. not using the Varianble
/Expression
fields of Post content parameters
in the Jenkins GUI.