1

I run in a Jenkins pipeline, a series of stages. Each stage represents a test. Even if a stage (test) fails, I'd like to continue with the following stages (tests), but I don't know how.

The only solution I know is to enclose the stage steps with a try/catch clause, but in this way I don't know easily if a test has failed or succeeded.

They cannot run in parallel, they must be run sequentially.

Is there a better solution?

Related question: Jenkins continue pipeline on failed stage

david.perez
  • 6,090
  • 4
  • 34
  • 57

1 Answers1

1

This isn't optimal, because Jenkins doesn't know which stages have failed, but we collect this data manually:

    def success = 0
    def failed = []

    for (int i = 0; i < tests.size(); i++) {
        def test = tests[i]
        stage(test) {
            try {
                sh "...."
                success++
            } catch (e) {
                failed << test
            }
        }
    }
    stage('Report') {
        def failCount = tests.size()-success
        if (failCount == 0)
            echo "Executed ${success} tests successfully."
        else
            error """Executed ${success} tests successfully and ${failCount} failed:
${failed.join(', ')}"""
    }
david.perez
  • 6,090
  • 4
  • 34
  • 57
  • This feels like a good answer to a problem that should be handled better in the DSL not in the groovy code. But I can see how I can write guards around each stage for stages I want to skip if any earlier stages have in fact failed. It gets my vote. Just not clear that this is the only or best way, based on the DSL as it stands today. –  Apr 04 '19 at 08:27