From: [email protected] [mailto:[email protected]] On Behalf Of Baptiste Mathus Sent: Monday, April 10, 2017 4:41 PM To: [email protected] Subject: Re: using .each with closure in jenkins pipeline - @NonCPS method
Resending from this morning, which didn't go through, see below. 2017-04-10 8:23 GMT+02:00 Stephen Connolly <[email protected]<mailto:[email protected]>>: On Mon 10 Apr 2017 at 06:02, niristotle okram <[email protected]<mailto:[email protected]>> wrote: I have seen that mentioned somewhere in the past. But i am baffled as to why things works in the master and fails in the slave/agent. Can anyone spot where i am faulting. this have owned me for days now :( ... i am trying to generate an XML file. based on the data provided by a JSON file. The pipeline snippet is import groovy.xml.* import groovy.json.JsonSlurper node('slave1') { deleteDir() stage('Checkout') { checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'b27f7cb2-efa8-496a-90d8-825b9332bf44', url: '[email protected]']]]) } writeFile file: "MyFile.XML", text: GenerateXML() //calling the method to generate the XML println "Generated the manifest XML" } @NonCPS def GenerateXML() { /* parsing the obj.json file */ def currentws = pwd() println currentws def jsonSlurper = new JsonSlurper(); //def fileReader = new BufferedReader(new FileReader("${currentws}/objects.json")) //the file location need to change in the actual implementation This won't work as groovy always executes on the master. You need to use FilePath to get the remote file Yes, also, avoid IMO doing fancy XML/JSON processing in Java/Groovy. See Pipeline as an orchestration tool with basic/no programming capabilities and delegate things to CLI tools and so on. If you wish to do more high level things, then possibly have a look at writing a plugin. In general, keep your pipeline code as stupid as possible. Also, using external tools will make it easier to test/reuse from your own local dev env. That is certainly where the programming experience leads one. But this is unfortunate. It is rather bizarre to put a complete programming language such as groovy in front of someone and then say “but only use a tiny little bit of it.” Makes one wonder whether using groovy as a framework for this was a mistake from the get-go. The notion of pushing ‘build’ operations down into scripts is perfectly sensible. The idea of having to ‘pull’ CI-integration activities ‘up into a plugin’ seems really unfortunate to me. At any rate my personal experience with the NonCPS mechanism is that one needs to be thoughtful about defining what is inside the function and what is outside (e.g. passed in to the function). So, for example maybe do the file read outside of the function and pass a string into the GenerateXML() function. def fileReader = readFile "${currentws}/objects.json" // ^^avoided the above line to use the pipeline DSL Afaik you can put CPS calls inside nonCPS No, it's not supported. "You may not call regular (CPS-transformed) methods, or Pipeline steps, from a @NonCPS method" https://github.com/jenkinsci/workflow-cps-plugin/#technical-design [snip] -- 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 [email protected]<mailto:[email protected]>. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CANWgJS4u8fp1_NFrNKCeop933Gier_f9MuhH1oMo480rHQ5z5A%40mail.gmail.com<https://groups.google.com/d/msgid/jenkinsci-users/CANWgJS4u8fp1_NFrNKCeop933Gier_f9MuhH1oMo480rHQ5z5A%40mail.gmail.com?utm_medium=email&utm_source=footer>. For more options, visit https://groups.google.com/d/optout. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CY1PR0301MB199626CE40527D7FE264641BB8010%40CY1PR0301MB1996.namprd03.prod.outlook.com. For more options, visit https://groups.google.com/d/optout.
