<p>This was already <a href="https://stackoverflow.com/questions/42837066/can-i-create-dynamically-stages-in-a-jenkins-pipeline">asked and answered over on StackOverflow</a>.</p>
<p>TL;DR: you must use Scripted Pipeline; Declarative does not support dynamic stages.  Here is a copy-paste of the top answer:</p>
<blockquote>
<p>Use the scripted syntax that allows more flexibility than the
declarative syntax, even though the declarative is more documented and
recommended.</p>
<p>For example stages can be created in a loop:</p>
<pre><code>def tests = params.Tests.split(',')
for (int i = 0; i &lt; tests.length; i++) {
    stage(&quot;Test ${tests[i]}&quot;) {
        sh '....'
    }
}
</code></pre>
</blockquote>

CC BY-SA 4.0
2022-10-27T20:23:45.663Z
is this file complete or would you need to wrap this in a `stages` block?
Adam Hughes
CC BY-SA 4.0
2022-10-28T16:07:15.297Z
It would need to be wrapped in a `script { }` block.
jayhendren
CC BY-SA 4.0
2022-04-26T18:38:32.033Z
2022-04-26T18:38:32.033Z
jayhendren
<p>I want to use a python script as input to create stages in jenkins pipeline</p>
<p>test.py</p>
<pre class="lang-py prettyprint-override"><code>{'job-name': 'Test1', 'action': 'a', 'targetname': 'x'}
{'job-name': 'Test2', 'action': 'b', 'targetname': 'y'} 
{'job-name': 'Test3', 'action': 'c', 'targetname': 'z'}
</code></pre>
<p>The output of the above python script is a dictionary.</p>
<p>How to run this python script in Jenkins pipeline in a stage and how to store the output and use the output to define further stages</p>
<p>Example:</p>
<pre><code>stage(Test1) {

}
stage(Test2) {

}
stage(Test3) {

}
</code></pre>
<p>Extract only 'value' of a particular 'key' from the output of python script to define stages in Jenkins pipeline.</p>
<p>How to execute it in Jenkins groovy as declarative pipeline ?</p>

CC BY-SA 4.0
2022-05-28T17:12:39.623Z
Did you solve your requirement?
JRichardsz
<jenkins><python><groovy><pipeline>
how to create dynamic stages in jenkins pipeline
Pavithra
# how to create dynamic stages in jenkins pipeline

(Asked by: username_0 on 2021-11-24)

I want to use a python script as input to create stages in jenkins pipeline
test.py
{'job-name': 'Test1', 'action': 'a', 'targetname': 'x'}
{'job-name': 'Test2', 'action': 'b', 'targetname': 'y'} 
{'job-name': 'Test3', 'action': 'c', 'targetname': 'z'}

The output of the above python script is a dictionary.
How to run this python script in Jenkins pipeline in a stage and how to store the output and use the output to define further stages
Example:
stage(Test1) {

}
stage(Test2) {

}
stage(Test3) {

}

Extract only 'value' of a particular 'key' from the output of python script to define stages in Jenkins pipeline.
How to execute it in Jenkins groovy as declarative pipeline ?

**Tags:** jenkins, python, groovy, pipeline

> **Comments:**
> **username_1**: Did you solve your requirement?  

---

## 1 answers

### Answer by username_2 on 2022-04-26. Score: 1

This was already asked and answered over on StackOverflow.
TL;DR: you must use Scripted Pipeline; Declarative does not support dynamic stages.  Here is a copy-paste of the top answer:

Use the scripted syntax that allows more flexibility than the
declarative syntax, even though the declarative is more documented and
recommended.
For example stages can be created in a loop:
def tests = params.Tests.split(',')
for (int i = 0; i < tests.length; i++) {
    stage("Test ${tests[i]}") {
        sh '....'
    }
}

> **Comments:**
> **username_3**: is this file complete or would you need to wrap this in a `stages` block?  
> **username_2**: It would need to be wrapped in a `script { }` block.  

---

