जेनकिंसफाइल में एक निर्माण में असफल


80

कुछ शर्तों के तहत मैं बिल्ड को विफल करना चाहता हूं। मैं उसको कैसे करू?

मैंने कोशिश की:

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

यह वास्तव में निर्माण को विफल करता है। हालाँकि, लॉग अपवाद दिखाता है:

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

जो यूजर्स को थोड़ा कंफ्यूज कर रहा है। क्या कोई बेहतर तरीका है?

जवाबों:


170

आप errorमौजूदा निर्माण को विफल करने के लिए पाइपलाइन डीएसएल से कदम का उपयोग कर सकते हैं ।

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

1
क्या आप जानते हैं कि यह संदेश BUILD_NUMBER या JOB_NAME, आदि जैसे चर में संग्रहीत है ...?
PRF

@PRF यह क्यों होगा? जब निर्माण विफल हो जाता है, तो पाइपलाइन खत्म हो जाती है। फिर आप उस चर का उपयोग कहां करेंगे?
साइमन फोर्सबर्ग

1
मेल या चैटरूम नोटिफिकेशन में उस संदेश का उपयोग करने के लिए
PRF

4
बस उपयोग ${env.BUILD_NUMBER} ${env.BUILD_URL}और${env.JOB_NAME}
tommed

1
एक चर स्टोर post{fail{...}}पाइपलाइन चरण में इस्तेमाल किया जा सकता है ।
सकुराशिंकिन

4

मैंने घोषणात्मक दृष्टिकोण के लिए त्रुटि हैंडलिंग के विभिन्न तरीकों को नीचे दिखाया है:

failfast समानांतर पाइपलाइन में

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

यदि किसी उपयोगकर्ता के पास समानांतर चरणों के साथ एक घोषणात्मक पाइपलाइन स्क्रिप्ट है, और वे failFast trueउन चरणों के लिए सेट करते हैं, यदि कोई चरण विफल रहता है, तो बिल्ड तुरंत निरस्त हो जाता है।

नोट: sh 'false' -> चरण को विफल कर सकते हैं

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!"
                 }
             }
         }
     }
}

जब failFast trueयह समानांतर कार्यों को समाप्त करता है।

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

जब "फेलफस्ट झूठा" तब भी यह अन्य समानांतर कार्यों को जारी रखता है।

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

जेनकिंस पाइपलाइन स्क्रिप्ट में ट्राइ-कैच ब्लॉक

पाइपलाइन की घोषणात्मक शैली में, इसलिए आपको कोशिश / कैच ब्लॉक (जो स्क्रिप्टेड पाइपलाइनों के लिए है) का उपयोग नहीं करना चाहिए, कुंजी की कोशिश है ... घोषणात्मक पाइपलाइन सिंटैक्स में स्क्रिप्ट ब्लॉक में पकड़। तब यह काम करेगा। यह उपयोगी हो सकता है यदि आप कहना चाहते हैं कि विफलता के बावजूद पाइपलाइन निष्पादन जारी रखें (उदाहरण के लिए: परीक्षण विफल रहा, फिर भी आपको रिपोर्ट की आवश्यकता है ..)

नोट: sh 'अमान्य कमांड' -> स्क्रिप्ट का हिस्सा होने पर भी चरण को विफल नहीं कर सकता है।

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.

जेनकींस पाइपलाइन नौकरी के निर्माण के परिणाम में हेरफेर कैसे करें? डिज़ाइन के अनुसार, "परिणाम केवल खराब हो सकता है, अन्य सेट को नजरअंदाज कर दिया जाता है" -> @see setResult () @ https://github.com/jenkinsci/jenkins/blob/213363d387736874fd14d83d57d7f757f3ed3f4f/core/src/main/java/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

जेनकींस पाइपलाइन में अपवाद कैसे फेंकें?

थ्रोइंग अपवाद चिकनी आउटपुट नहीं है।

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

जेनकींस पाइपलाइन में अपवाद कैसे फेंकें?

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

error इसे निष्पादित करने से रोकने के लिए समानांतर पाइपलाइन चरण बना सकते हैं।

नीचे ' unstable' कमांड currentBuild.result = ' UNSTABLE' सेट करने के समान है ।

इससे फाँसी पर रोक नहीं लगेगी।

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

यह सेटिंग के बराबर है currentBuild.result = 'FAILURE'

इससे फाँसी पर रोक नहीं लगेगी।

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

यदि आप कुछ विधियों / वर्ग का उपयोग करना चाहते हैं, तो आपको एक समान संदेश मिल सकता है:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: नए java.lang.RuntimeException java.lang.String का उपयोग करने की अनुमति नहीं लिपियों

आमतौर पर यह आपके जेनकींस में लिपियों के अनुमोदन पृष्ठ के लिए एक लिंक होता है (जिसे आप एक व्यवस्थापक होने पर अपडेट कर सकते हैं)।

मेरे अपने जेनकींस से उदाहरण के लिए:

लिपियों को विधि का उपयोग करने की अनुमति नहीं है। org.w3c.dom.lement setAttribute java.lang.String java.lang.String। व्यवस्थापक यह तय कर सकते हैं कि इस हस्ताक्षर को स्वीकार या अस्वीकार किया जाए या नहीं। org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: लिपियों को विधि का उपयोग करने की अनुमति नहीं है। org.w3c.dom.lement setAttribute java.lang.Stang java.lang.String

पाठ व्यवस्थापक यह तय कर सकते हैं कि इस हस्ताक्षर को स्वीकार या अस्वीकार किया जाए या नहीं। एक लिंक होना चाहिए जिसे आप जेनकिंस स्क्रिप्ट अनुमोदन पृष्ठ का अनुसरण कर सकते हैं। यह URL आमतौर पर है:

http://<Jenkins URL>/scriptApproval/

या इसके माध्यम से सुलभ होना चाहिए: जेनकिंस -> प्रबंधन -> इन-प्रोसेस स्क्रिप्ट अनुमोदन

इस पृष्ठ पर आप स्वीकृत कर सकते हैं कि स्क्रिप्ट इन विधियों / कक्षाओं का उपयोग कर सकती हैं।

हालाँकि आपको केवल एक अपवाद को फेंकने में सक्षम होना चाहिए - मैं इसे स्क्रिप्ट स्वीकृति के लिए आवश्यकता के बिना कर सकता हूं।

उदाहरण के लिए मेरी पाइपलाइन ग्रूवी लिपि के भीतर:

throw new Exception('Some error text')

जेनकींस कंसोल में यह आउटपुट कंसोल:

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)

यह काफी लंबी स्टैकट्रेस, पहली पंक्ति:

WorkflowScript.processProjectsToUpdate (वर्कफ़्लोस्क्रिप्ट: 106 )

लाइन 106 आपके पाइप लाइन ग्रूवी लिपि में वह रेखा होनी चाहिए जहां एक्सेप्शन फेंका गया था, जो आपके लिए उपयोगी जानकारी हो सकती है।

यदि आप स्टैकट्रेस में दिलचस्पी नहीं रखते हैं, जैसा कि अन्य उत्तरों में बस त्रुटि का उपयोग करें :

error('Some error text')

यह जेनकिंस पाइपलाइन प्रलेखन में उल्लेख किया गया है: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#error-error-signal

त्रुटि: त्रुटि संकेत सिग्नल एक त्रुटि। उपयोगी है यदि आप अपने कार्यक्रम के कुछ हिस्से को सशर्त रूप से निरस्त करना चाहते हैं। आप केवल नया अपवाद () भी फेंक सकते हैं, लेकिन यह चरण स्टैक ट्रेस को प्रिंट करने से बचाएगा।


0

कैसे के बारे में System.exit(1)अगर असफल स्थिति से मुलाकात की है?

private void test(boolean status){
    if(!status){
       printReport();
       System.exit(1);
     }
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.