Hongli
Hongli

Reputation: 18924

Jenkins: how to use parameters to skip pipeline parallel steps?

I have a pipeline which runs a bunch of different tests in parallel. Sometimes one of those tests fail, and I want to restart that test only. Before I used pipelines, I used the Matrix Reloaded plugin to achieve this. I want to achieve the same thing with pipelines, and I believe that one way to achieve this is through build parameters.

I have the following pipeline:

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.'
                            }
                        }
                    }
                )
            }
        }
    }
}

But this gives me an error:

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) {
               ^

How can I solve this?

Upvotes: 0

Views: 3089

Answers (1)

Philip
Philip

Reputation: 3078

If you want to use Groovy expressions in the declarative pipeline syntax you can either use the script step or you switch to scripted pipeline.

See the following link for syntax comparison: https://jenkins.io/doc/book/pipeline/syntax/

The following code fixes you're issue by wrapping the parallel part in a script step:

...
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.'
                            }
                        }
                    }
                )
            }
        }
    }
}
...

Upvotes: 2

Related Questions