Jenkinsfile – 组合变量以调用第三个变量

huangapple go评论54阅读模式
英文:

Jenkinsfile - Combine variables to call a 3rd variable

问题

我想知道下面的情况是否可能。我试图在一个流水线中放置一些逻辑,将两个变量组合成第三个变量进行调用。在下面的代码中,我定义了食物,并在第一个循环中添加了"_juice"。在这一点上,我想遍历数组'chips_juice',但它只返回单个字符。

任何帮助将不胜感激

def food = ['chips']
def chips_juice = ['lovely-meal']

food.each { grub ->
  env.meal = grub + '_juice'
  meal.each { dinner ->
    println dinner
  }
}  

结果

[Pipeline] echo
c
[Pipeline] echo
h
[Pipeline] echo
i
[Pipeline] echo
p
[Pipeline] echo
s
[Pipeline] echo
_
[Pipeline] echo
j
[Pipeline] echo
u
[Pipeline] echo
i
[Pipeline] echo
c
[Pipeline] echo
e
[Pipeline] node

我已经尝试以不同格式定义餐变量,但这要么停止了流水线,要么返回相同的结果。

英文:

I'm wondering if the below scenario is possible. I am trying to place some logic in a pipeline to combine two variables to call a 3rd. With the below I'm defining food and in the 1st each loop adding the '_juice'. At this point I would like to iterate through the array 'chips_juice' but it just returns single characters.

Any help would be appreciated

def food = ['chips']
def chips_juice = ['lovely-meal']

food.each { grub ->
  env.meal = grub + '_juice'
  meal.each {dinner ->
    println dinner
  }
}  

> Results

[Pipeline] echo
c
[Pipeline] echo
h
[Pipeline] echo
i
[Pipeline] echo
p
[Pipeline] echo
s
[Pipeline] echo
_
[Pipeline] echo
j
[Pipeline] echo
u
[Pipeline] echo
i
[Pipeline] echo
c
[Pipeline] echo
e
[Pipeline] node

I have tried defining the meal variable in different formats but this either stops the pipeline or returns the same result.

答案1

得分: 1

你无法通过你目前的尝试方式来实现这个。但你可以像下面这样做。

food = ['chips']
chips_juice = ['lovely-meal']

food.each { grub ->
  def meal = grub + '_juice'
  def newList = this.getBinding().getVariable(meal);

  newList.each { dinner ->
    println dinner
  }
}
英文:

You can't achieve this the way you are trying it. But you can do something like the below.

food = ['chips']
chips_juice = ['lovely-meal']

food.each { grub ->
  def meal = grub + '_juice'
  def newList = this.getBinding().getVariable(meal);

  newList.each {dinner ->
    println dinner
  }
}

huangapple
  • 本文由 发表于 2023年3月7日 20:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定