You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Pipeline脚本新手求助:函数间传递并获取修改后变量

How to Get Modified Variable Values in Jenkins Pipeline Functions

Hey there! Let's tackle this common Jenkins Pipeline issue. The root problem here is that in Groovy (the language underpinning Jenkins Pipeline), primitive types like Strings are passed by value to functions. That means when your verify function modifies a and b, it's actually changing copies of those variables—not the original ones defined in your node block. That's why abc can't see the updated values.

Here are three practical solutions to fix this:

1. Use a Map to Encapsulate Variables (Most Common Approach)

Wrap your a and b variables in a Map. Since Maps are reference types, any changes to the Map's contents inside a function will affect the original Map in your node block.

node{
  def vars = [a: 'xyz', b: 'abc'] // Encapsulate variables in a Map
  stage('verify'){
    verify(vars)
    abc(vars)
    echo "changed values of a and b are ${vars.a} ${vars.b}"
  }
}

def verify(Map vars) {
  // Modify the Map's values directly—this affects the original Map
  vars.a = 'modified_a_value'
  vars.b = 'modified_b_value'
}

def abc(Map vars){
  // Access the updated values directly from the Map
  echo "Received modified a: ${vars.a}, modified b: ${vars.b}"
}

2. Create a Custom Class for State Management (Good for Complex Scenarios)

If you have multiple variables or need more structured state, define a simple Groovy class to hold your values. Like Maps, objects are passed by reference, so changes inside functions will update the original object.

// Define a simple class to hold your pipeline variables
class PipelineState {
  String a
  String b
}

node{
  def pipelineState = new PipelineState(a: 'xyz', b: 'abc')
  stage('verify'){
    verify(pipelineState)
    abc(pipelineState)
    echo "changed values of a and b are ${pipelineState.a} ${pipelineState.b}"
  }
}

def verify(PipelineState state) {
  // Update the object's properties directly
  state.a = 'updated_a'
  state.b = 'updated_b'
}

def abc(PipelineState state){
  echo "Got updated values: a=${state.a}, b=${state.b}"
}

3. Return Modified Values from the Function (Simple for Small Changes)

If you only need to pass back the modified values, have your verify function return a Map or a tuple of the updated a and b, then update the original variables in your node block before calling abc.

node{
  def a = 'xyz'
  def b = 'abc'
  stage('verify'){
    // Capture the modified values returned by verify
    def updatedValues = verify(a, b)
    a = updatedValues.a
    b = updatedValues.b
    
    abc(a, b)
    echo "changed values of a and b are ${a} ${b}"
  }
}

def verify(String a, String b) {
  // Apply your modification logic here
  a = 'new_a_value'
  b = 'new_b_value'
  
  // Return the modified values as a Map
  return [a: a, b: b]
}

def abc(String a, String b){
  echo "Received modified a: ${a}, modified b: ${b}"
}

Recommendation

For most simple cases, the Map approach is the easiest and most straightforward. It avoids extra boilerplate while solving the pass-by-value limitation effectively.

内容的提问来源于stack exchange,提问作者sudhir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:29:30