<p>I think what you are trying to describe can be done with a feature of the groovy programming language called <a href="https://groovy-lang.org/closures.html" rel="nofollow noreferrer">closures</a>.</p>
<p>Assuming you have a groovy script similar to this:</p>
<pre><code>node() {
    stage('A') {
        println(&quot;I'm stage A!&quot;)
    }

    stage('B') {
        println(&quot;I'm stage B!&quot;)
    }
}
</code></pre>
<p>You could rerun this pipeline multiple times doing something like this:</p>
<pre><code>node() {
    // Create the closure here
    def reusablePipeline = {
        node() {
            stage('A') {
                println(&quot;I'm stage A!&quot;)
            }

            stage('B') {
                println(&quot;I'm stage B!&quot;)
            }
        }
    }

    // Run the pipeline once
    reusablePipeline()

    // Run it a second time
    reusablePipeline()
}
</code></pre>
<p>If all the pipelines will run on the same <code>node()</code> than we can remove the second definition:</p>
<pre><code>def reusablePipeline = {
    stage('A') {
        println(&quot;I'm stage A!&quot;)
    }

    stage('B') {
        println(&quot;I'm stage B!&quot;)
    }
}
</code></pre>
<p>If you find the reusable part of the job really long, you might want to load it from a second file instead. You can do this with the pipeline <a href="https://www.jenkins.io/doc/pipeline/steps/workflow-cps/#load-evaluate-a-groovy-source-file-into-the-pipeline-script" rel="nofollow noreferrer">load</a> function. Here is an example:</p>
<p>Put your reusable pipeline in a new file. Here I call it <code>pipeline.groovy</code>.</p>
<pre><code>//contents of pipeline.groovy
def doStuff() {
    stage('A') {
        println(&quot;I'm stage A!&quot;)
    }

    stage('B') {
        println(&quot;I'm stage B!&quot;)
    }
}

// This is really important
return this
</code></pre>
<p>Now your pipeline would look something like this:</p>
<pre><code>node() {
    def reusablePipeline = load('pipeline.groovy')

    // Run the pipeline once
    reusablePipeline.doStuff()

    // Run it a second time
    reusablePipeline.doStuff()
}
</code></pre>
<p>The above methods work great if you have a pipeline with a series of complicated steps and you would like to call those steps multiple times based on some criteria. A common use case is recalling a test suite with different parameters or re-running the test suite if some flaky tests fail.</p>
<p>But if you want to re-use pipeline steps across multiple Jenkins pipelines. Then you are looking for <a href="https://www.jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">Jenkins Shared Libraries</a>.</p>
<p>I have a public shared library with lots of utilities to make writing pipelines easier called the <a href="https://github.com/DontShaveTheYak/jenkins-std-lib" rel="nofollow noreferrer">jenkins-std-lib</a>. I'm also the creator of the <a href="https://marketplace.visualstudio.com/items?itemName=DontShaveTheYak.jenkins-extension-pack" rel="nofollow noreferrer">Jenkins Extension Pack</a> which makes it easier to write Jenkins pipelines using the VSCode IDE.</p>

CC BY-SA 4.0
2022-12-24T09:40:53.610Z
2022-12-24T09:40:53.610Z
Levi
<p>I have a groovy script that defines a pipeline. The pipeline has several stages. I would like to assign that pipeline definition into a variable and run it later in the same groovy script. Is it possible?</p>
<p>I could not find a documentation on how to do that. Thanks in advance.</p>

<jenkins>
Is it possible to assign a jenkins pipeline to a variable and run in groovy script?
leopoodle
# Is it possible to assign a jenkins pipeline to a variable and run in groovy script?

(Asked by: username_0 on 2022-12-21)

I have a groovy script that defines a pipeline. The pipeline has several stages. I would like to assign that pipeline definition into a variable and run it later in the same groovy script. Is it possible?

I could not find a documentation on how to do that. Thanks in advance.

**Tags:** jenkins


---

## 1 answers

### Answer by username_1 on 2022-12-24. Score: 1

I think what you are trying to describe can be done with a feature of the groovy programming language called closures.
Assuming you have a groovy script similar to this:
node() {
    stage('A') {
        println("I'm stage A!")
    }

    stage('B') {
        println("I'm stage B!")
    }
}

You could rerun this pipeline multiple times doing something like this:
node() {
    // Create the closure here
    def reusablePipeline = {
        node() {
            stage('A') {
                println("I'm stage A!")
            }

            stage('B') {
                println("I'm stage B!")
            }
        }
    }

    // Run the pipeline once
    reusablePipeline()

    // Run it a second time
    reusablePipeline()
}

If all the pipelines will run on the same node() than we can remove the second definition:
def reusablePipeline = {
    stage('A') {
        println("I'm stage A!")
    }

    stage('B') {
        println("I'm stage B!")
    }
}

If you find the reusable part of the job really long, you might want to load it from a second file instead. You can do this with the pipeline load function. Here is an example:
Put your reusable pipeline in a new file. Here I call it pipeline.groovy.
//contents of pipeline.groovy
def doStuff() {
    stage('A') {
        println("I'm stage A!")
    }

    stage('B') {
        println("I'm stage B!")
    }
}

// This is really important
return this

Now your pipeline would look something like this:
node() {
    def reusablePipeline = load('pipeline.groovy')

    // Run the pipeline once
    reusablePipeline.doStuff()

    // Run it a second time
    reusablePipeline.doStuff()
}

The above methods work great if you have a pipeline with a series of complicated steps and you would like to call those steps multiple times based on some criteria. A common use case is recalling a test suite with different parameters or re-running the test suite if some flaky tests fail.
But if you want to re-use pipeline steps across multiple Jenkins pipelines. Then you are looking for Jenkins Shared Libraries.
I have a public shared library with lots of utilities to make writing pipelines easier called the jenkins-std-lib. I'm also the creator of the Jenkins Extension Pack which makes it easier to write Jenkins pipelines using the VSCode IDE.


---

