我正在尝试构建一组动态的步骤来并行运行。以下示例是我想到的(并在 https://devops.stackexchange.com/questions/3073/how-to-properly-achieve-dynamic-parallel-action-with-a-declarative-pipeline 找到了示例)。但我无法让它使用预期的变量。结果似乎总是来自循环的最后一次迭代的变量。
在以下示例中,两个测试的 echo 输出始终为 bdir2
:
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
def tests = [:]
def files
files = ['adir1/adir2/adir3','bdir1/bdir2/bdir3']
files.each { f ->
rolePath = new File(f).getParentFile()
roleName = rolePath.toString().split('/')[1]
tests[roleName] = {
echo roleName
}
}
parallel tests
}
}
}
}
}
我期望其中一个测试输出 adir2
,另一个测试输出 bdir2
。我在这里缺少什么?
最佳答案
只要尝试将测试部分移高一点,就可以了
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
def tests = [:]
def files
files = ['adir1/adir2/adir3','bdir1/bdir2/bdir3']
files.each { f ->
tests[f] = {
rolePath = new File(f).getParentFile()
roleName = rolePath.toString().split('/')[1]
echo roleName
}
}
parallel tests
}
}
}
}
}
关于Jenkins 并行脚本在循环中使用错误的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54191597/