Hello everybody,
Recently the saltstack plugin was updated for pipeline support. In working this up, it was important to not just print the return to the console, but to return the value in case it might be needed for further processing. The syntax for doing this looks something like:
stage("run salt") {
node("agent1") {
saltresult = salt authtype: 'pam', clientInterface: local(arguments: '"sleep 5; tail -1 /etc/hosts"', blockbuild: true, minionTimeout: 32, function: 'cmd.run', jobPollTime: 7, target: '*', targetType: 'glob'), credentialsId: 'b5f40401-01b9-4b27-a5e8-8ae94bc90250', servername: 'http://localhost:8000'
echo saltresult
}
}
This seems to work well, except in the case of the newly released declarative pipeline syntax. I think the syntax for this would be something like:
pipeline {
agent {label 'agent1'}
stages {
stage('run salt') {
steps {
output = salt authtype: 'pam', clientInterface: local(arguments: '"sleep 5; ls -la"', function: 'cmd.run', jobPollTime: 16, target: '*', targettype: 'glob'), credentialsId: 'b5f40401-01b9-4b27-a5e8-8ae94bc90250', servername: '
http://localhost:8000'
echo output
}
}
}
}
Unfortunately this results in an error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 6: Expected a step @ line 6, column 17.
output = salt authtype: 'pam', clientInterface: local(arguments: '"sleep 5; ls -la"', function: 'cmd.run', jobPollTime: 16, target: '*', targettype: 'glob'), credentialsId: 'b5f40401-01b9-4b27-a5e8-8ae94bc90250', servername: 'http://localhost:8000'
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1073)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
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.reparse(CpsGroovyShell.java:67)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:430)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:393)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:238)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)
It looks like the error can be avoided by dropping the variable assignment 'output =' bit, however this then makes it impossible to see/print/parse the return from the plugin. Adding in pipeline support made aspects of the plugin much easier, as previously it supported replacing templated options with environment variables (as to support parameterized builds) and saving the salt output into an environment variable. But by supporting pipeline, this all becomes available for free as you can pass in groovy variables and save the return output. However, with the declarative syntax, it again complicates things with its simplicity. How should this be handled?
Thanks much