rm *_*-rf 5 groovy jenkins-groovy jenkins-pipeline
我正在尝试collectEntries
在Groovy脚本中使用多个串联。最好看一下代码,现在我有了:
stage('Test') {
// Reading content of the file
def portsFileContent = readFile 'UsedPorts.txt'
// Split the file by next line
def ports = portsFileContent.split('\n')
def steps = ports.collectEntries { port ->
["UI Test on port $port", {
sh "#!/bin/bash -lx \n startServerWithDifferentPort --params=port=$port"
}]
}
parallel steps
}
Run Code Online (Sandbox Code Playgroud)
在文件“ UsedPorts.txt”中,不同的端口由换行符分隔,例如:
4723
4733
4743
Run Code Online (Sandbox Code Playgroud)
因此,此数字将存储在变量中ports
,然后将该变量用于为每个端口启动服务器实例。因此,在这种情况下,它将通过以下命令启动3个不同的serverinstance:
def steps = ports.collectEntries { port ->
["UI Test on port $port", {
sh "#!/bin/bash -lx \n startServerWithDifferentPort --params=port=$port"
}]
}
parallel steps
Run Code Online (Sandbox Code Playgroud)
由于parallel steps
服务器的3个启动实例同时具有不同的端口。
没问题,但是我有另一个文件,需要再次执行相同操作。因此,在我的第二个文件中,有如下条目:
name1
name2
name3
Run Code Online (Sandbox Code Playgroud)
我再次创建了一个变量,用于存储3个条目:
def anotherFile = readFile 'Names.txt'
def names = anotherFile.split('\n')
Run Code Online (Sandbox Code Playgroud)
这是我尝试的:
stage('Test') {
// Reading content of the file
def portsFileContent = readFile 'UsedPorts.txt'
// Split the file by next line
def ports = portsFileContent.split('\n')
// Do the same again
def anotherFile = readFile 'Names.txt'
def names = anotherFile.split('\n')
def steps = ports.collectEntries, names.collectEntries { port, name ->
["UI Test on $name", {
sh "#!/bin/bash -lx \n someMoreShellStuff --params=port=$port"
}]
}
parallel steps
}
Run Code Online (Sandbox Code Playgroud)
但是我不能collectEntries
用逗号分隔第二个,因为它给了我一个语法错误。现在我的问题是,如何在同一命令中使用此变量。可能吗
谢谢
更新#1
使用Szymon Stepniak的答案后,我的新代码如下所示:
stage('Test') {
// Reading content of the file
def portsFileContent = readFile 'AppiumUsedPorts.txt'
// Split the file by next line
def ports = portsFileContent.split('\n')
// Getting device IDs to get properties of device
def deviceIDFileContent = readFile 'DeviceIDs.txt'
def deviceIDs = deviceIDFileContent.split('\n')
// Define port and id as an pair
def pairs = (0..Math.min(ports.size(), deviceIDs.size())).collect { i -> [id: deviceIDs[i], port: ports[i]] }
def steps = pairs.collectEntries { pair ->
["UI Test on ${pair.id}", {
sh "echo 'Running test with port ${pair.port}'"
}]
}
parallel steps
}
Run Code Online (Sandbox Code Playgroud)
这导致错误 java.lang.ArrayIndexOutOfBoundsException
更新#2
内容AppiumUsedPorts.txt
:
4723
4733
Run Code Online (Sandbox Code Playgroud)
内容 DeviceIDs.txt
5353352c
G000KU0663550R92
Run Code Online (Sandbox Code Playgroud)
像你看上去想从两个列表拉链元素-ports
以及names
在创建并行执行步骤,使用这些对。因此,假设ports
并names
包含以下内容:
def ports = [8080, 8081, 8082, 8083]
def names = ['Host A', 'Host B', 'Host C', 'Host D', 'Host E']
Run Code Online (Sandbox Code Playgroud)
你需要一个对的列表,比如:
def pairs = [[port: 8080, name: 'Host A'], [port: 8081, name: 'Host B'], [port: 8082, name: 'Host C'], [port:8083, 'Host D']]
Run Code Online (Sandbox Code Playgroud)
我故意使用了两个不同大小的列表,以解释压缩两个列表的结果总是相同的大小,然后是最短的列表。
Groovy 有一个方法GroovyCollections.transpose(List lists)
可以获取列表列表(例如[[8080, 8081, 8082, 8083], ['Host A', 'Host B', 'Host C', 'Host D', 'Host E']]
)并将两个列表“压缩”在一起,例如:
[[8080, 'Host A'], [8081, 'Host B'], [8082, 'Host C'], [8083, 'Host D']]
Run Code Online (Sandbox Code Playgroud)
但它在 Jenkins Pipeline 中不起作用 - 如果您尝试使用它,您将获得:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods transpose java.util.List
Run Code Online (Sandbox Code Playgroud)
无论如何,您可以简单地使用collect
从 0 到的范围min(ports.size(), names.size())
来创建这些对/映射列表。看看下面的例子:
node {
stage('Test') {
def ports = [8080, 8081, 8082, 8083]
def names = ['Host A', 'Host B', 'Host C', 'Host D', 'Host E']
def pairs = (0..<Math.min(ports.size(), names.size())).collect { i -> [name: names[i], port: ports[i]] }
def steps = pairs.collectEntries { pair ->
["UI Test on ${pair.name}", {
sh "echo 'Running test with port ${pair.port}'"
}]
}
parallel steps
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我们将两个列表转换为一个映射列表,[port: ..., name: ...]
我们调用collectEntries
该映射列表以在同一执行步骤中获取端口和名称。在 Jenkins Pipeline 中运行此脚本会产生以下输出:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] parallel
[Pipeline] [UI Test on Host A] { (Branch: UI Test on Host A)
[Pipeline] [UI Test on Host B] { (Branch: UI Test on Host B)
[Pipeline] [UI Test on Host C] { (Branch: UI Test on Host C)
[Pipeline] [UI Test on Host D] { (Branch: UI Test on Host D)
[Pipeline] [UI Test on Host A] sh
[UI Test on Host A] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host B] sh
[UI Test on Host B] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host C] sh
[UI Test on Host C] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host D] sh
[UI Test on Host A] + echo Running test with port 8080
[UI Test on Host A] Running test with port 8080
[UI Test on Host B] + echo Running test with port 8081
[UI Test on Host B] Running test with port 8081
[UI Test on Host D] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host A] }
[UI Test on Host C] + echo Running test with port 8082
[UI Test on Host C] Running test with port 8082
[UI Test on Host D] + echo Running test with port 8083
[UI Test on Host D] Running test with port 8083
[Pipeline] [UI Test on Host B] }
[Pipeline] [UI Test on Host C] }
[Pipeline] [UI Test on Host D] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
归档时间: |
|
查看次数: |
1358 次 |
最近记录: |