【问题标题】:Jenkins: how to use parameters to skip pipeline parallel steps?Jenkins:如何使用参数来跳过管道并行步骤?
【发布时间】:2017-05-12 21:16:43
【问题描述】:

我有一个管道可以并行运行一堆不同的测试。有时其中一项测试失败,我只想重新启动该测试。在我使用管道之前,我使用了 Matrix Reloaded 插件来实现这一点。我想用管道实现同样的目标,我相信实现这一目标的一种方法是通过构建参数。

我有以下管道:

pipeline {
    agent any

    parameters {
        booleanParam(name: 'RUBY_LINUX', defaultValue: true, description: 'Ruby unit tests on Linux')
        booleanParam(name: 'RUBY_MACOS', defaultValue: true, description: 'Ruby unit tests on macOS')
    }

    stages {
        stage('test') {
            steps {
                parallel(
                    'Ruby unit tests on Linux': {
                        node('linux') {
                            if (params.RUBY_LINUX) {
                                echo 'Ran test.'
                            } else {
                                echo 'Skipped test.'
                            }
                        }
                    },
                    'Ruby unit tests on macOS': {
                        node('macos') {
                            if (params.RUBY_MACOS) {
                                echo 'Ran test.'
                            } else {
                                echo 'Skipped test.'
                            }
                        }
                    }
                )
            }
        }
    }
}

但这给了我一个错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 24: Expected a step @ line 24, column 13.
               if (params.RUBY_LINUX) {
               ^

WorkflowScript: 41: Expected a step @ line 41, column 13.
               if (params.RUBY_MACOS) {
               ^

我该如何解决这个问题?

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    如果您想在声明性管道语法中使用 Groovy 表达式,您可以使用脚本步骤或切换到脚本化管道。

    语法比较见以下链接: https://jenkins.io/doc/book/pipeline/syntax/

    以下代码通过将并行部分包装在脚本步骤中来解决您的问题:

    ...
    stages {
        stage('test') {
            steps {
                script {
                    parallel(
                        'Ruby unit tests on Linux': {
                            node('linux') {
                                if (params.RUBY_LINUX) {
                                    echo 'Ran test.'
                                } else {
                                    echo 'Skipped test.'
                                }
                           }
                         },
                        'Ruby unit tests on macOS': {
                            node('macos') {
                                if (params.RUBY_MACOS) {
                                    echo 'Ran test.'
                                } else {
                                    echo 'Skipped test.'
                                }
                            }
                        }
                    )
                }
            }
        }
    }
    ...
    

    【讨论】:

    • 您可能还想验证条件。我不确定声明性管道,但对于普通管道,all 参数作为字符串值传递,这将使您的if(..) 始终为真。您可以通过回显params.RUBY_MACOS.class 进行检查
    • 感谢 Philip 和 Rik,这很有效。显然类型是 java.lang.Boolean。
    • +1,但是如果你要使用'script {...',你最好在'node'之前使用'if',避免跳过时抓住一个节点。
    猜你喜欢
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多