简体   繁体   English

Jenkins 管道 - 将 yaml 和 append 读取到列表

[英]Jenkins pipeline - Read yaml and append to a list

I have the below template yaml in my github我的 github 中有以下模板 yaml

kind: Endpoints
apiVersion: v1
metadata:
 name: SERVICENAME
 namespace: NAMESPACE
subsets:
 - addresses:
     - ip: SERVICEADDRESS
   ports:
     - port: SERVICEPORT

I am trying to parse this yaml file in Jenkins pipeline and append values to placeholders like below.我正在尝试将 Jenkins 管道中的 yaml 文件和 append 值解析为如下所示的占位符。

kind: Endpoints
apiVersion: v1
metadata:
 name: test
 namespace: testnamespace
subsets:
 - addresses:
     - ip: 10.22.33.22
     - ip: 10.22.11.33
   ports:
     - port: 1042

I am able to read the yaml as an object using below code我可以使用以下代码将 yaml 作为 object 读取

def extEpData = readYaml (file: 'external-ep.yaml')

Not able to append multiple ip addresses to the list无法将 append 多个 ip 地址列表

 extEpData.metadata.name = serviceName
 extEpData.metadata.namespace = namespace
 extEpData.subsets[0].ports[0].port = servicePort as int
 for (int i=0; i<=addressList.size(); i++) {
           echo addressList[i]
           extEpData.subsets[0].addresses[0].ip = addressList[i]
  }
  writeYaml file: 'external-ep.yaml', data: extEpData, overwrite: true

addressList array holds the ip list. addressList 数组保存 ip 列表。 Above code is not appending multiple ip addresses.上面的代码没有附加多个 ip 地址。 Its only working for one ip address.它仅适用于一个 ip 地址。 What am I doing wrong?我究竟做错了什么?

If you update the accessed element index in the nested array/list on each iteration, then you should be good to go:如果您在每次迭代时更新嵌套数组/列表中的访问元素索引,那么您应该对 go 很好:

for (int i=0; i<=addressList.size(); i++) {
  extEpData.subsets[0].addresses[i].ip = addressList[i]
}

However, this is only true if the number of elements of the placeholder is equal to the number of inserted elements.但是,仅当占位符的元素数量等于插入元素的数量时,这才是正确的。 This is not necessarily always true, so you should append by Map instead of by value associated with Map key:这不一定总是正确的,因此您应该通过 Map 而不是通过与 Map 键关联的值来 append :

for (int i=0; i<=addressList.size(); i++) {
  extEpData.subsets[0].addresses[i] = ['ip': addressList[i]]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM