Accessing Jenkins pipeline WorkflowScript during execution

494 Views Asked by At

I would like to know if there is a way to access the Jenkins Workflow script object during its execution.

I have a shared library, and I can pass this object to any groovy class as an argument, either directly from the Jenkins file, using 'this' keyword, or from any DSL in the vars folder, also using the 'this' keyword.

But I would like to access it using a method, even if this imply using reflexivity.

Is that possible?

1

There are 1 best solutions below

2
On

Here example with pipeline, where this is a script object. Some other examples here:

MyClass myClass = new MyClass()

pipeline {
    agent any

    environment {
        VAR1 = "var1"
        VAR2 = sh(returnStdout: true, script: "echo var2").trim()
        VAR3 = "var3"
    }

    stages {
        stage("Stage 1") {
            steps {
                script {
                    myClass.myPrint(this, "${VAR1}", "${VAR2}", "${VAR3}")
                }
            }
        }
    }
}

class MyClass implements Serializable {
    void myPrint(def script, String var1, String var2, String... vars) {
        script.echo "myPrint: ${var1}"
    }
}