Search code examples
jenkinsgroovyjenkins-pipelinepipelinedsl

How to replicate dir step in groovy


Below is my pipeline code :

dir(my_directory) {
      retry(1) {
             // something
      }
}

Is there a possibility to access dir step in groovy through the pipeline context ?

I'm thinking of something like this below

class StepExecutor {
  // some code
    void dir(String directory, Closure statement) {
        this.steps.dir(directory) { statement }
    }
}

Solution

  • Yes you can. It requires you to pass the steps-object of the pipeline though.

    class StepExecutor {
      
      def steps;
      public StepExecutor(def steps) {
         this.steps = steps
      }
      // some code
        void dir(String directory, Closure statement) {
            this.steps.dir(directory) { statement }
        }
    }
    

    creating the object from inside of your pipeline:

    pipeline { ....
       def stepExecutor = new StepExecutor(this);
    ...}