【问题标题】:How to define parallel stages of a Jenkinsfile in an external function?如何在外部函数中定义 Jenkinsfile 的并行阶段?
【发布时间】:2018-12-30 11:21:42
【问题描述】:

这个最小的管道可以工作:

pipeline {
    agent any
    stages {
        stage('test') {
            parallel {
                stage('test-1') {
                    steps {
                        sh "echo test1"
                    }
                }
                stage('test-2') {
                    steps {
                        sh "echo test2"
                    }
                }
            }
        }
    }
}

我们在 parallel 块内有几个测试阶段,因此我们遇到了 Method Code too large 错误,Jenkins 人员显然不打算修复。

我想在外部函数中定义我的并行阶段,如下所示:

pipeline {
    agent any
    stages {
        stage('test') {
            parallel test_func()
        }
    }
}

def test_func() {
    return {
            stage('test-1') {
                steps {
                    sh "echo test1"
                }
            }
            stage('test-2') {
                steps {
                    sh "echo test2"
                }
            }
        }
}

但是,这不适用于我们尝试过的许多语法变体。

提前致谢!

【问题讨论】:

  • parallel 需要一个列表 ([ .. ]),我会说它应该包含两个条目(每个都包含在 { .. } 中),每个条目包含一个 stage
  • 更正,不是列表,而是地图,请看下面的答案。

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline


【解决方案1】:

查看我对this question的回复。

您需要将并行阶段放在变量中,而不是函数中。所以,它可能是这样的:

def test_func = [
    "test1": {
        echo "test1"
    },
    "test2": {
        echo "test2"
    }
]

pipeline {
    agent any
    stages {
        stage('test') {
            steps {
                script {
                    parallel test_func
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    如示例here 中所见,它需要一个任意标识符的映射作为键,并包含一个包含代码作为值的闭包。所以有些东西(未经测试)比如:

    def test_func() {
        return [
            test1: {
                stage('test-1') {
                    steps {
                        sh "echo test1"
                    }
                }
            },
            test2: {
                stage('test-2') {
                    steps {
                        sh "echo test2"
                    }
                }
            }
        ]
    }
    

    【讨论】:

    • 谢谢,我得到了``` WorkflowScript: 4: Expected a block for parallel @ line 4, column 9. stage('test') { ^ WorkflowScript: 4: Expected one of "steps ", "stages", or "parallel" for stage "test" @ line 4, column 9. stage('test') { ``` 其中第 4 行是:``` pipeline { agent any stages { stage('test ') { 并行 test_func() ```
    • 啊,你在脚本上下文中,所以steps 节必须被删除。
    • 但这只是后续的事情.. 不能从这里尝试,但是可以在( .. ) 中返回返回的列表或在parallel 之后添加这些括号?我真的不确定这里,也许搜索更多的例子。
    • 如前所述,“不适用于我们尝试过的大量语法变体。”我正在声明式管道中。
    【解决方案3】:

    你可以这样做:

    def createStages() {
      stage_map = [:]
      stage_map.put('test-1', {echo 'test1'})
      stage_map.put('test-2', {echo 'test2'})
      return stage_map
    }
    
    pipeline {
      agent any
      stages {
        stage('test') {
          steps{
            script { parallel(createStages()) }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多