Search code examples
jenkinsgroovyjenkins-pipeline

How to fail Jenkins stage on groovy condition


I want the stage to fail if this groovy condition is false: version ==~ versionPattern

I have tried:

        stage("Sanity check") {
            steps {
                script {
                    assert version !=~ versionPattern : "Build failed because git branch does not contain valid semantic version"
                }
            }

and:

        stage("Sanity check") {
            steps {
                script {
                    if(version != versionPattern) {
                        exit 1
                    }
                }
            }

How can I do this? Both examples Jenkins just ignores


Solution

  • You can use error:

    if(version != versionPattern) {
      error(message: "Build failed because git branch does not contain valid semantic version.")
    }