[JENKINS-41335] Allow variables and functions to be defined within pipeline to be used in any stage · Issue #946 · jenkinsci/pipeline-model-definition-plugin · GitHub
Skip to content You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert [JENKINS-41335] Allow variables and functions to be defined within pipeline to be used in any stage #946 New issue Copy link New issue Copy link Closed Closed [JENKINS-41335] Allow variables and functions to be defined within pipeline to be used in any stage #946 Copy link Labels component:pipeline-model-definition-plugin enhancement imported-jira-issue priority:critical resolution:wont-fix Description jenkins-infra-bot opened on Jan 23, 2017 Issue body actions I have several use cases where I may operate on a list or map on different stages of a pipeline.
This variable may not be known ahead of time - it could be defined within a stage. I'd like a syntax that allows me to define variables within a pipeline. Today, I have to define a shared variable before entering pipeline {}. Jenkinsfile #!/bin/groovy // Define outside of pipeline to make sure accessible in all script {} blocks def my_list pipeline { agent { label 'label' } stages { stage( 'stage1' ) { steps { script { my_list = [1,2,3] } } } stage( 'stage2' ) { steps { script { for ( int i=0; i<my_list.size();i++) { echo "Doing something with ${my_list[i]}" } } } } } } A simple approach would be to allow "def" to work anywhere within pipeline {}.
This avoids any overly new syntax for pipeline. Another option would allow the user to provide a special pipeline {} block for defining non-env/params style variables. This could be helpful in allowing the user to use items available in script {} for defining shared state. Jenkinsfile with possible example syntax #!/bin/groovy pipeline { agent { label 'label' } define { def my_map = [:] <span class="code-comment">//empty map def my_list //undefined shared variable } stages { stage( 'stage1' ) { steps { script { //If def can be anywhere in pipeline {}, drop script {} my_list = [1,2,3] } } } stage( 'stage2' ) { steps { script { for ( int i=0; i<my_list.size();i++) { echo "Doing something with ${my_list[i]}" } } } } } } Originally reported by rpocase , imported from: Allow variables and functions to be defined within pipeline to be used in any stage assignee : abayer status : Closed priority : Critical component(s) : pipeline-model-definition-plugin resolution : Won't Fix resolved : 2018-04-12T15:59:31+00:00 votes : 43 watchers : 57 imported : 2025-12-06 Raw content of original issue I have several use cases where I may operate on a list or map on different stages of a pipeline.
This variable may not be known ahead of time - it could be defined within a stage. I'd like a syntax that allows me to define variables within a pipeline. Today, I have to define a shared variable before entering pipeline {}. Jenkinsfile #!/bin/groovy // Define outside of pipeline to make sure accessible in all script {} blocks def my_list pipeline { agent { label 'label' } stages { stage( 'stage1' ) { steps { script { my_list = [1,2,3] } } } stage( 'stage2' ) { steps { script { for ( int i=0; i<my_list.size();i++) { echo "Doing something with ${my_list[i]}" } } } } } } A simple approach would be to allow "def" to work anywhere within pipeline {}.
This avoids any overly new syntax for pipeline. Another option would allow the user to provide a special pipeline {} block for defining non-env/params style variables. This could be helpful in allowing the user to use items available in script {} for defining shared state. Jenkinsfile with possible example syntax #!/bin/groovy pipeline { agent { label 'label' } define { def my_map = [:] <span class="code-comment">//empty map def my_list //undefined shared variable } stages { stage(<span class="code-quote">'stage1'</span>) { steps { script { <span class="code-comment">//If def can be anywhere in pipeline {}, drop script {} my_list = [1,2,3] } } } stage(<span class="code-quote">'stage2'</span>) { steps { script { <span class="code-keyword">for</span>(<span class="code-object">int</span> i=0; i<my_list.size();i++) { echo <span class="code-quote">"Doing something with ${my_list[i]}"</span> } } } } } } Activity Next jenkins-infra-bot commented on Jan 23, 2017 jenkins-infra-bot on Jan 23, 2017 Author More actions abayer: Comment link Original comment link (no redirect) Raw content of original comment: I like the define block idea, though I can't yet guarantee I can make that work in implementation.
Will experiment in that direction when I get a chance. I've also long been kicking around the idea of having functions available for setting the value of variables to deal with the = blocking but haven't fallen in love with it enough to actually do it. =) I like the define block idea, though I can't yet guarantee I can make that work in implementation. Will experiment in that direction when I get a chance. I've also long been kicking around the idea of having functions available for setting the value of variables to deal with the = blocking but haven't fallen in love with it enough to actually do it. =) jenkins-infra-bot commented on Jan 23, 2017 jenkins-infra-bot on Jan 23, 2017 Author More actions abayer: Comment link Original comment link (no redirect) Raw content of original comment: hrmpw jamesdumay michaelneale rsandell - something else I'd like your thoughts on. hrmpw jamesdumay michaelneale rsandell - something else I'd like your thoughts on. jenkins-infra-bot commented on Jan 23, 2017 jenkins-infra-bot on Jan 23, 2017 Author More actions hrmpw: Comment link Original comment link (no redirect) Raw content of original comment: Using top-level define and then def inside the block seems a bit redundant.
This would make the use of Artifactory much simpler. They rely heavily on defining configuration settings. That can easily be done in a script but I could see it used with this, too. Using top-level define and then def inside the block seems a bit redundant. This would make the use of Artifactory much simpler. They rely heavily on defining configuration settings. That can easily be done in a script but I could see it used with this, too. jenkins-infra-bot commented on Jan 23, 2017 jenkins-infra-bot on Jan 23, 2017 Author More actions rpocase: Comment link Original comment link (no redirect) Raw content of original comment: Not a huge fan of the redundant def either, but wanted a compromise to possibly allow script expressions.
An alternative would be using expression (similar to when) to be explicit about when script style usage is allowed. I'm not overly fond of this syntax either, but it does provide some consistency with other declarative constructs. define { my_var = expression { return "foo" == "bar" } } Not a huge fan of the redundant def either, but wanted a compromise to possibly allow script expressions. An alternative would be using expression (similar to when) to be explicit about when script style usage is allowed.
I'm not overly fond of this syntax either, but it does provide some consistency with other declarative constructs. define { my_var = expression { return "foo" == "bar" } } jenkins-infra-bot commented on Jan 24, 2017 jenkins-infra-bot on Jan 24, 2017 Author More actions abayer: Comment link Original comment link (no redirect) Raw content of original comment: Worth mentioning that in any scenario, the def would literally be redundant no matter what we're doing with the value - we wouldn't be literally calling this Groovy code at parse time, we'd be saying "Ok, here's the variable name, and here's what to set it to - evaluate that value at runtime and set the variable to that result".
Worth mentioning that in any scenario, the def would literally be redundant no matter what we're doing with the value - we wouldn't be literally calling this Groovy code at parse time, we'd be saying "Ok, here's the variable name, and here's what to set it to - evaluate that value at runtime
The rest of this page is on the live site.