Title: Message Title
David Rajnic updated an issue
Jenkins / JENKINS-56605
Cannot use custom agent directive in Jenkinsfile
Change By:
David Rajnic
We are not able to use custom directive for "agent".We'd like to create a set of reusable directives, including few of those replacing the default agent directive. The error thrown is:{code:java}org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:WorkflowScript: 4: Undefined section "nodeAgent" @ line 4, column 5. nodeAgent( ^WorkflowScript: 3: Missing required section "agent" @ line 3, column 1. pipeline { ^2 errors 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.lambda$doParse$0(CpsGroovyShell.java:135) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136) at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:132) at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127) at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560) at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521) at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429)Finished: FAILURE{code} And the Jenkinsfile{code:java}@Library('jenkins-pipelines') _pipeline {nodeAgent(version: '10')stages {stage('Build') {...} stage('Test') {...}stage('Publish') {...}}}{code} We'd like to run this within a docker container with NodeJS \{version}, so for that reason we've decided to create a reusable *nodeAgent* directive.{code:java}def call(Map opts) {def spec = new NodeAgentSpec(opts)if (!spec.version) {throw new RuntimeException('version must be provided')}new node(). agent( spec)}{code}and for the *node.groovy*{code:java}def agent( NodeAgentSpec spec) {def cacheFolderPath = '$HOME' + "/.docker-cache/node/${spec.version}"sh "mkdir -p ${cacheFolderPath}"sh "chmod 777 -R ${cacheFolderPath}"agent {docker {image "local/nodejs-node-${spec.version}"