Build in Jenkinsfile fehlgeschlagen


79

Unter bestimmten Umständen möchte ich den Build fehlschlagen. Wie mache ich das?

Ich habe es versucht:

throw RuntimeException("Build failed for some specific reason!")

Dies schlägt tatsächlich den Build fehl. Das Protokoll zeigt jedoch die Ausnahme:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.RuntimeException java.lang.String

Was für die Benutzer etwas verwirrend ist. Gibt es einen besseren Weg?

Antworten:


166

Sie können den errorSchritt aus dem Pipeline-DSL verwenden, um den aktuellen Build fehlzuschlagen.

error("Build failed because of this and that..")

1
Wissen Sie, ob diese Nachricht in einer Variablen wie BUILD_NUMBER oder JOB_NAME usw. gespeichert ist?
PRF

@ PRF Warum sollte es? Wenn der Build fehlschlägt, ist die Pipeline beendet. Wo würden Sie dann diese Variable verwenden?
Simon Forsberg

1
Um diese Nachricht in Mail- oder Chatroom-Benachrichtigungen zu verwenden
PRF

4
Verwenden Sie einfach ${env.BUILD_NUMBER} ${env.BUILD_URL}und${env.JOB_NAME}
tommed

Ein Variablenspeicher könnte im post{fail{...}}Pipeline-Schritt verwendet werden.
Sakurashinken

3

Ich habe unten verschiedene Arten der Fehlerbehandlung für den deklarativen Ansatz gezeigt:

failfast in paralleler Pipeline

https://issues.jenkins-ci.org/browse/JENKINS-55459?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel

Wenn ein Benutzer über ein deklaratives Pipeline-Skript mit parallelen Stufen verfügt und diese festlegt failFast true Stufen festlegt, wird der Build sofort abgebrochen, wenn eine der Stufen fehlschlägt.

HINWEIS: sh 'false' -> Kann die Bühne nicht bestehen

pipeline {
    agent any
    stages {
         stage('Validate Fail fast') {
             failFast true
             parallel {
                  stage('stage A') {
                    steps {
                        echo 'stage A started'
                        sleep 5
                        sh 'false'
                        echo 'stage A Ended' //will not execute because of above sh return

                    }
                }
                stage('stage B') {
                    steps {
                        echo 'stage B started'
                        sleep 10
                        echo 'stage B Ended' //will not execute because of above stage fail
                    }
                }
                 stage('stage C') {
                    steps {
                        echo 'stage C started'
                        echo 'stage C Ended' //May complete before Stage A fails
                    }
                }
             }
         }
         stage('final stage sequential') {
             steps {
                 script {
                     echo "The complete run!"
                 }
             }
         }
     }
}

Wenn failFast truees die parallelen Aufgaben beendet.

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Validate Fail fast)
[Pipeline] parallel
[Pipeline] { (Branch: stage A)
[Pipeline] { (Branch: stage B)
[Pipeline] { (Branch: stage C)
[Pipeline] stage
[Pipeline] { (stage A)
[Pipeline] stage
[Pipeline] { (stage B)
[Pipeline] stage
[Pipeline] { (stage C)
[Pipeline] echo
stage A started
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] echo
stage B started
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] echo
stage C started
[Pipeline] echo
stage C Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] sh
+ false
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage A
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage B
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (final stage sequential)
Stage "final stage sequential" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

Wenn "failFast false" ausgeführt wird, werden weiterhin andere parallele Aufgaben ausgeführt.

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Validate Fail fast)
[Pipeline] parallel
[Pipeline] { (Branch: stage A)
[Pipeline] { (Branch: stage B)
[Pipeline] { (Branch: stage C)
[Pipeline] stage
[Pipeline] { (stage A)
[Pipeline] stage
[Pipeline] { (stage B)
[Pipeline] stage
[Pipeline] { (stage C) (hide)
[Pipeline] echo
stage A started
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] echo
stage B started
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] echo
stage C started
[Pipeline] echo
stage C Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] sh
+ false
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage A
[Pipeline] echo
stage B Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (final stage sequential)
Stage "final stage sequential" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

Try-Catch-Block im Jenkins-Pipeline-Skript

Im deklarativen Pipeline-Stil dürfen Sie also keine try / catch-Blöcke (für Skript-Pipelines) verwenden. Der Schlüssel besteht darin, try ... catch in einen Skriptblock in deklarativer Pipeline-Syntax einzufügen. Dann wird es funktionieren. Dies kann nützlich sein, wenn Sie sagen möchten, dass die Pipeline-Ausführung trotz eines Fehlers fortgesetzt werden soll (z. B. Test fehlgeschlagen, Sie benötigen dennoch Berichte.)

HINWEIS: sh 'ungültiger Befehl' -> Kann die Phase nicht bestehen, obwohl sie Teil des Skripts ist.

script {
  try {
      sh 'do your stuff'
  } catch (Exception e) {
      sh 'Handle the exception!'
  }
}


    try {
        sh 'might fail'
        echo 'Succeeded!'
    } catch (err) {
        echo "Failed: ${err}"
    } finally {
        sh './tear-down.sh'
    }


pipeline {
    agent any
    stages {
         stage('Validate Fail fast') {
             failFast true
             parallel {
                  stage('stage A') {
                    steps {
                        echo 'stage A started'
                        sleep 5
                        script {
                              try {
                                  sh 'I_AM_NOT_VALID_CMD'
                              } catch (Exception e) {
                                  sh 'EVEN_I_AM_INVALID_2_FAIL_THIS_BUILD!'
                              }
                        }
                        echo 'stage A Ended' //will not execute because of above sh return
                    }
                }
                stage('stage B') {
                    steps {
                        echo 'stage B started'
                        sleep 10
                        echo 'stage B Ended' //will not execute because of above stage fail
                    }
                }
                 stage('stage C') {
                    steps {
                        echo 'stage C started'
                        echo 'stage C Ended' //will not execute because of above stage fail
                    }
                }
             }
         }
         stage('final stage sequential') {
             steps {
                 script {
                     echo "The complete run!"
                 }
             }
         }
     }
}

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Validate Fail fast)
[Pipeline] parallel
[Pipeline] { (Branch: stage A)
[Pipeline] { (Branch: stage B)
[Pipeline] { (Branch: stage C)
[Pipeline] stage
[Pipeline] { (stage A)
[Pipeline] stage
[Pipeline] { (stage B)
[Pipeline] stage
[Pipeline] { (stage C)
[Pipeline] echo
stage A started
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] echo
stage B started
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] echo
stage C started
[Pipeline] echo
stage C Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ I_AM_NOT_VALID_CMD
/Users/Shared/Jenkins/Home/workspace/ErrorHandling@tmp/durable-5fc28a9a/script.sh: line 1: I_AM_NOT_VALID_CMD: command not found
[Pipeline] sh
+ 'EVEN_I_AM_INVALID_2_FAIL_THIS_BUILD!'
/Users/Shared/Jenkins/Home/workspace/ErrorHandling@tmp/durable-5e73fa36/script.sh: line 1: EVEN_I_AM_INVALID_2_FAIL_THIS_BUILD!: command not found
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage A
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage B
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (final stage sequential)
Stage "final stage sequential" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

https://support.cloudbees.com/hc/en-us/articles/218554077-How-to-set-current-build-result-in-Pipeline

currentBuild.result = 'FAILURE'  //Should be inside script
This will not stop the executions.

Wie manipuliere ich das Build-Ergebnis eines Jenkins-Pipeline-Jobs? "Ergebnis kann nur schlechter werden, sonst wird Set ignoriert" -> @see setResult () @ https://github.com/jenkinsci/jenkins/blob/213363d387736874f1d14d83e57347f757f3ed4f/core/src/main/java/hudson/model /Run.java#L462-L466

pipeline {
    agent any
    stages {
         stage('Validate Fail fast') {
             failFast true
             parallel {
                  stage('stage A') {
                    steps {
                        echo 'stage A started'
                        sleep 5
                        script {
                            currentBuild.result = 'FAILURE'
                        }
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage A Ended'
                    }
                }
                stage('stage B') {
                    steps {
                        echo 'stage B started'
                        sleep 10
                        echo 'stage B wakeup'
                        script {
                            currentBuild.result = 'FAILURE'
                        }
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage B Ended' //will not execute because of above stage fail
                    }
                }
                 stage('stage C') {
                    steps {
                        echo 'stage C started'
                        echo 'stage C Ended' //will not execute because of above stage fail
                    }
                }
             }
         }
         stage('final stage sequential') {
             steps {
                 script {
                     echo "The complete run!"
                 }
             }
         }
     }
}


Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Validate Fail fast)
[Pipeline] parallel
[Pipeline] { (Branch: stage A)
[Pipeline] { (Branch: stage B)
[Pipeline] { (Branch: stage C)
[Pipeline] stage
[Pipeline] { (stage A)
[Pipeline] stage
[Pipeline] { (stage B)
[Pipeline] stage
[Pipeline] { (stage C)
[Pipeline] echo
stage A started
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] echo
stage B started
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] echo
stage C started
[Pipeline] echo
stage C Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
RESULT: FAILURE
[Pipeline] echo
stage A Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] echo
stage B wakeup
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
RESULT: FAILURE
[Pipeline] echo
stage B Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (final stage sequential)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The complete run!
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: FAILURE

Wie Ausnahme in Jenkins Pipeline werfen?

Das Auslösen einer Ausnahme ist keine reibungslose Ausgabe.

pipeline {
    agent any
    stages {
         stage('Validate Fail fast') {
             failFast true
             parallel {
                  stage('stage A') {
                    steps {
                        echo 'stage A started'
                        sleep 5
                        script {
                            throw new Exception()
                        }
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage A Ended' //will not execute because of above sh return
                    }
                }
                stage('stage B') {
                    steps {
                        echo 'stage B started'
                        sleep 10
                        echo 'stage B wakeup'
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage B Ended' //will not execute because of above stage fail
                    }
                }
                 stage('stage C') {
                    steps {
                        echo 'stage C started'
                        echo 'stage C Ended' //will not execute because of above stage fail
                    }
                }
             }
         }
         stage('final stage sequential') {
             steps {
                 script {
                     echo "The complete run!"
                 }
             }
         }
     }
}

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Validate Fail fast)
[Pipeline] parallel
[Pipeline] { (Branch: stage A)
[Pipeline] { (Branch: stage B)
[Pipeline] { (Branch: stage C)
[Pipeline] stage
[Pipeline] { (stage A)
[Pipeline] stage
[Pipeline] { (stage B)
[Pipeline] stage
[Pipeline] { (stage C)
[Pipeline] echo
stage A started
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] echo
stage B started
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] echo
stage C started
[Pipeline] echo
stage C Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] script
[Pipeline] {
Scripts not permitted to use new java.lang.Exception. Administrators can decide whether to approve or reject this signature.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage A
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage B
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (final stage sequential)
Stage "final stage sequential" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Also:   org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
        at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution.cancel(CpsBodyExecution.java:253)
        at org.jenkinsci.plugins.workflow.steps.BodyExecution.cancel(BodyExecution.java:76)
        at org.jenkinsci.plugins.workflow.cps.steps.ParallelStepExecution.stop(ParallelStepExecution.java:67)
        at org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.checkAllDone(ParallelStep.java:147)
        at org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.onFailure(ParallelStep.java:134)
        at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution$FailureAdapter.receive(CpsBodyExecution.java:361)
        at com.cloudbees.groovy.cps.impl.ThrowBlock$1.receive(ThrowBlock.java:68)
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.Exception
    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectNew(StaticWhitelist.java:271)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:174)
    at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:200)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:205)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.constructorCall(SandboxInvoker.java:21)
    at WorkflowScript.run(WorkflowScript:12)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:97)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:78)
    at jdk.internal.reflect.GeneratedMethodAccessor188.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
    at 
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Finished: FAILURE

Wie Ausnahme in Jenkins Pipeline werfen?

node { try { error 'Test error' } catch (ex) { echo 'Error handled' } }  //node has to be replaced with scripts

error kann die parallelen Pipeline-Stufen so einstellen, dass sie nicht mehr ausgeführt werden.

Der folgende unstableBefehl ' ' ähnelt dem Setzen von currentBuild.result = 'UNSTABLE '.

Dadurch werden die Ausführungen nicht gestoppt.

script {
        unstable 'unstable'
}


pipeline {
    agent any
    stages {
         stage('Validate Fail fast') {
             failFast true
             parallel {
                  stage('stage A') {
                    steps {
                        echo 'stage A started'
                        sleep 5
                        script {
                            error 'Test error'
                        }
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage A Ended' //will not execute because of above sh return
                    }
                }
                stage('stage B') {
                    steps {
                        echo 'stage B started'
                        sleep 10
                        echo 'stage B wakeup'
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage B Ended' //will not execute because of above stage fail
                    }
                }
                 stage('stage C') {
                    steps {
                        echo 'stage C started'
                        echo 'stage C Ended' //will not execute because of above stage fail
                    }
                }
             }
         }
         stage('final stage sequential') {
             steps {
                 script {
                     echo "The complete run!"
                 }
             }
         }
     }
}




Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Validate Fail fast)
[Pipeline] parallel
[Pipeline] { (Branch: stage A)
[Pipeline] { (Branch: stage B)
[Pipeline] { (Branch: stage C)
[Pipeline] stage
[Pipeline] { (stage A)
[Pipeline] stage
[Pipeline] { (stage B)
[Pipeline] stage
[Pipeline] { (stage C)
[Pipeline] echo
stage A started
[Pipeline] sleep
Sleeping for 5 sec
[Pipeline] echo
stage B started
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] echo
stage C started
[Pipeline] echo
stage C Ended
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] script
[Pipeline] {
[Pipeline] error
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage A
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch stage B
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (final stage sequential)
Stage "final stage sequential" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: Test error
Finished: FAILURE

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-catcherror-code-catch-error-and-set-build-result

catchError {
        sh 'might fail'
    }

Dies entspricht der Einstellung currentBuild.result = 'FAILURE'

Dadurch werden die Ausführungen nicht gestoppt.

pipeline {
    agent any
    stages {
         stage('Validate Fail fast') {
             failFast true
             parallel {
                  stage('stage A') {
                    steps {
                        echo 'stage A started'
                        sleep 5
                        script {
                            catchError {
                                sh 'might fail'
                            }
                        }
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage A Ended' //will not execute because of above sh return
                    }
                }
                stage('stage B') {
                    steps {
                        echo 'stage B started'
                        sleep 10
                        echo 'stage B wakeup'
                        echo "RESULT: ${currentBuild.result}"
                        echo 'stage B Ended' //will not execute because of above stage fail
                    }
                }
                 stage('stage C') {
                    steps {
                        echo 'stage C started'
                        echo 'stage C Ended' //will not execute because of above stage fail
                    }
                }
             }
         }
         stage('final stage sequential') {
             steps {
                 script {
                     echo "The complete run!"
                 }
             }
         }
     }
}


    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Validate Fail fast)
    [Pipeline] parallel
    [Pipeline] { (Branch: stage A)
    [Pipeline] { (Branch: stage B)
    [Pipeline] { (Branch: stage C)
    [Pipeline] stage
    [Pipeline] { (stage A)
    [Pipeline] stage
    [Pipeline] { (stage B)
    [Pipeline] stage
    [Pipeline] { (stage C)
    [Pipeline] echo
    stage A started
    [Pipeline] sleep
    Sleeping for 5 sec
    [Pipeline] echo
    stage B started
    [Pipeline] sleep
    Sleeping for 10 sec
    [Pipeline] echo
    stage C started
    [Pipeline] echo
    stage C Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] script
    [Pipeline] {
    [Pipeline] catchError
    [Pipeline] {
    [Pipeline] sh
    + might fail
    /Users/Shared/Jenkins/Home/workspace/ErrorHandling@tmp/durable-2b5ebe28/script.sh: line 1: might: command not found
    [Pipeline] }
    ERROR: script returned exit code 127
    [Pipeline] // catchError
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] echo
    RESULT: FAILURE
    [Pipeline] echo
    stage A Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] echo
    stage B wakeup
    [Pipeline] echo
    RESULT: FAILURE
    [Pipeline] echo
    stage B Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (final stage sequential)
    [Pipeline] script
    [Pipeline] {
    [Pipeline] echo
    The complete run!
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: FAILURE


1

Wenn Sie bestimmte Methoden / Klassen verwenden möchten, wird möglicherweise eine ähnliche Meldung angezeigt:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Skripte dürfen keine neue java.lang.RuntimeException java.lang.String verwenden

In der Regel folgt in Ihrem Jenkins ein Link zur Seite zur Genehmigung von Skripten (die Sie aktualisieren können, wenn Sie Administrator sind).

Zum Beispiel von meinen eigenen Jenkins:

Skripte dürfen die Methode org.w3c.dom.Element setAttribute java.lang.String java.lang.String nicht verwenden. Administratoren können entscheiden, ob diese Signatur genehmigt oder abgelehnt werden soll. org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Skripte dürfen die Methode org.w3c.dom.Element setAttribute java.lang.String java.lang.String nicht verwenden

Die Textadministratoren können entscheiden, ob diese Signatur genehmigt oder abgelehnt wird. sollte ein Link sein, dem Sie zur Jenkins Script Approval Page folgen können. Die URL lautet normalerweise:

http://<Jenkins URL>/scriptApproval/

Oder sollte zugänglich sein über: Jenkins -> Verwalten -> In-Process-Skriptgenehmigung

Auf dieser Seite können Sie genehmigen, dass Skripte diese Methoden / Klassen verwenden können.

Sie sollten jedoch nur eine Ausnahme auslösen können - ich kann dies tun, ohne eine Skriptgenehmigung durchführen zu müssen.

Zum Beispiel in meinem Pipeline-Groovy-Skript:

throw new Exception('Some error text')

Diese Ausgabe in der Jenkins-Buildkonsole:

java.lang.Exception: Some error text
at WorkflowScript.processProjectsToUpdate(WorkflowScript:106)
at ___cps.transform___(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)

Dies gibt eine ziemlich lange Stapelspur aus, die erste Zeile:

bei WorkflowScript.processProjectsToUpdate (WorkflowScript: 106 )

Zeile 106 sollte die Zeile in Ihrem Pipeline-Groovy-Skript sein, in der die Ausnahme ausgelöst wurde. Dies kann für Sie eine nützliche Information sein.

Wenn Sie nicht an der Stapelverfolgung interessiert sind, wie in anderen Antworten, verwenden Sie einfach error :

error('Some error text')

Dies wird in der Dokumentation zur Jenkins-Pipeline erwähnt: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#error-error-signal

Fehler: Fehlersignal Signalisiert einen Fehler. Nützlich, wenn Sie einen Teil Ihres Programms bedingt abbrechen möchten. Sie können auch einfach eine neue Ausnahme () auslösen. In diesem Schritt wird jedoch das Drucken einer Stapelverfolgung vermieden.


0

Wie System.exit(1)wäre es, wenn die fehlerhafte Bedingung erfüllt ist?

private void test(boolean status){
    if(!status){
       printReport();
       System.exit(1);
     }
}
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.