JenkinsPipelineUnit icon indicating copy to clipboard operation
JenkinsPipelineUnit copied to clipboard

thisObject inside pipeline script referes to Declaration class

Open Willem1987 opened this issue 4 years ago • 1 comments

In actual jenkins the this object in the pipeline can be used to pass the pipeline script.

I workaround this by adding a line: Script pipelineScript = this

Above the pipeline closure

Willem1987 avatar Oct 01 '20 16:10 Willem1987

Would this also "help" for actual call stack output for target in MethodCall?

E.g. based upon: https://github.com/kamilszymanski/jenkins-pipeline-automation-starter

  • For pipeline:
timestamps {
    node {
        stage('Generate data') {
            progressNotices.displayProgressNotice('Generating data')
            try {
                generateData('data')
            } catch (exception) {
                cleanWs()
                throw exception
            }
        }
    }
}
  • And (new shared library) tests case:
    @Test
    void 'sharedlib-minimum-for-ticket'() throws Exception {
        String jenkinsfileFileName = 'SharedLibraryBasedSampleScriptedJenkinsfile'
        helper.registerAllowedMethod('ansiColor', [String, Closure], null)
        helper.registerAllowedMethod('timestamps', [Closure], null)

        // According to https://github.com/jenkinsci/JenkinsPipelineUnit/issues/318:
        def progressNoticesScript = loadScript('vars/progressNotices.groovy')
        // Like before:
        helper.registerAllowedMethod('generateData', [String], null)

        // Instead of:
        //runScript(jenkinsfilePath(jenkinsfileFileName))
        // ... inject the 'progressNotices' var and execute it:
        def jenkinsfileScript = loadScript(jenkinsfilePath(jenkinsfileFileName))
        jenkinsfileScript.progressNotices = progressNoticesScript
        jenkinsfileScript.run()

        printCallStack()

        // Must not contain: SharedLibraryBasedSampleScriptedJenkinsfile.cleanWs()
        assertCallStack().doesNotContain('cleanWs')
    }
  • The resulting call stack is:
   SharedLibraryBasedSampleScriptedJenkinsfile.run()
      SharedLibraryBasedSampleScriptedJenkinsfile.timestamps(groovy.lang.Closure)
         SharedLibraryBasedSampleScriptedJenkinsfile.node(groovy.lang.Closure)
            SharedLibraryBasedSampleScriptedJenkinsfile.stage(Generate data, groovy.lang.Closure)
               progressNotices.displayProgressNotice(Generating data)
                  progressNotices.ansiColor(xterm, groovy.lang.Closure)
                     progressNotices.sh(echo -e '\e[32mGenerating data\e[0m')
               SharedLibraryBasedSampleScriptedJenkinsfile.generateData(data)

Which (I think) I would rather expect to be:

   SharedLibraryBasedSampleScriptedJenkinsfile.run()
      SharedLibraryBasedSampleScriptedJenkinsfile.timestamps(groovy.lang.Closure)
         SharedLibraryBasedSampleScriptedJenkinsfile.node(groovy.lang.Closure)
            SharedLibraryBasedSampleScriptedJenkinsfile.stage(Generate data, groovy.lang.Closure)
               progressNotices.displayProgressNotice(Generating data)
                  SharedLibraryBasedSampleScriptedJenkinsfile.ansiColor(xterm, groovy.lang.Closure)
                     SharedLibraryBasedSampleScriptedJenkinsfile.sh(echo -e '\e[32mGenerating data\e[0m')
               SharedLibraryBasedSampleScriptedJenkinsfile.generateData(data)

reinholdfuereder avatar Feb 26 '21 09:02 reinholdfuereder