Jenkinsfile에서 빌드 실패


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파이프 라인 DSL 의 단계를 사용 하여 현재 빌드를 실패 할 수 있습니다.

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{...}}파이프 라인 단계 에서 변수 저장소를 사용할 수 있습니다 .
sakurashinken

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

"failFast false"이면 다른 병렬 작업을 계속 실행합니다.

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

Jenkins 파이프 라인 스크립트의 Try-catch 블록

선언적 파이프 라인 스타일에서는 try / catch 블록 (스크립팅 된 파이프 라인 용)을 사용해서는 안됩니다. 핵심은 선언적 파이프 라인 구문의 스크립트 블록에 try ... catch를 넣는 것입니다. 그러면 작동합니다. 이는 실패에도 불구하고 파이프 라인 실행을 계속하려는 경우 유용 할 수 있습니다 (예 : 테스트 실패, 여전히 보고서가 필요합니다.).

참고 : sh 'invalid command'-> 스크립트의 일부인 경우에도 스테이지가 실패 할 수 있습니다.

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.

Jenkins 파이프 라인 작업의 빌드 결과를 조작하는 방법은 무엇입니까? 의도적으로 "결과는 더 나빠질 수 있지만 그렇지 않으면 세트는 무시됩니다"-> @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

젠킨스 파이프 라인에서 예외를 던지는 방법은 무엇입니까?

예외 발생은 원활한 출력이 아닙니다.

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을 사용할 수 없습니다.

일반적으로 Jenkins의 Scripts Approval 페이지에 대한 링크가 이어집니다 (관리자 인 경우 업데이트 할 수 있음).

예를 들어 내 자신의 Jenkins에서 :

스크립트는 org.w3c.dom.Element 메소드를 사용할 수 없습니다. setAttribute java.lang.String java.lang.String. 관리자는이 서명을 승인할지 거부할지 결정할 수 있습니다. org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException : 스크립트는 org.w3c.dom.Element setAttribute java.lang.String java.lang.String 메소드를 사용할 수 없습니다.

텍스트 관리자는이 서명을 승인할지 거부할지 결정할 수 있습니다. Jenkins 스크립트 승인 페이지로 이동할 수있는 링크 여야합니다. URL은 일반적으로 다음과 같습니다.

http://<Jenkins URL>/scriptApproval/

또는 다음을 통해 액세스 할 수 있어야합니다. Jenkins- > 관리 -> In-process Script Approval

이 페이지에서 스크립트가 이러한 메소드 / 클래스를 사용할 수 있음을 승인 할 수 있습니다.

그러나 예외 를 던질 수 있어야합니다 . 스크립트 승인 없이도이 작업을 수행 할 수 있습니다.

예를 들어 내 파이프 라인 그루비 스크립트 내에서 :

throw new Exception('Some error text')

Jenkins 빌드 콘솔의 다음 출력 :

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 (WorkflowScript : 106 )에서

106 번 줄은 파이프 라인 그루비 스크립트에서 예외가 발생한 줄이어야하며, 이는 유용한 정보가 될 수 있습니다.

다른 답변에서와 같이 stacktrace에 관심이 없다면 error 사용하십시오 .

error('Some error text')

이는 Jenkins Pipeline 문서 ( https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#error-error-signal )에 언급되어 있습니다.

오류 : 오류 신호 오류를 나타냅니다. 프로그램의 일부를 조건부로 중단하려는 경우 유용합니다. new Exception ()을 던질 수도 있지만이 단계에서는 스택 추적을 인쇄하지 않습니다.


0

방법에 대한 System.exit(1)실패한 조건이 충족되는 경우?

private void test(boolean status){
    if(!status){
       printReport();
       System.exit(1);
     }
}
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.