Jenkins管道-步骤失败,但如果字符串/样式错误出现则继续

时间:2019-05-14 19:11:41

标签: jenkins jenkins-pipeline

如果错误消息中出现字符串“无法撤消保管库租约”,我如何让构建继续/忽略失败? 我在Azure机密引擎上使用了HashiCorp Vault插件,但它不能很好地处理吊销。这不是问题 我是因为撤销ttl是在事物的保险柜侧配置的。

def configuration = [
  $class: 'VaultConfiguration',
  vaultUrl: 'https://vault-url',
  vaultCredentialId: 'my-approle'
]

def secrets = [
  [ $class: 'VaultSecret', path: 'mypath/mysecret', secretValues: [
    [ $class: 'VaultSecretValue', envVar: 'CLIENT_ID', vaultKey: 'client_id' ],
    [ $class: 'VaultSecretValue', envVar: 'CLIENT_SECRET', vaultKey: 'client_secret' ]
  ]]
]

pipeline {
  agent any
  stages {
    stage('default stage') {
      steps {
        script {
          try {
            wrap([$class: 'VaultBuildWrapper', configuration: configuration, vaultSecrets: secrets]) {
              sh '''
                echo CLIENT_ID is $CLIENT_ID
                echo CLIENT_SECRET is $CLIENT_SECRET
              '''
            }
          } catch (err) {
            echo err
          }
        }
      }
    }
    stage('second') {
      steps {
        sh 'echo second stage first step'
      }
    }
  }
}

这是詹金斯工作的输出

Started by user mr.gru
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/jobs/testfolder/jobs/demo-vault-declarative-pipeline-sp/workspace
[Pipeline] {
[Pipeline] stage
[Pipeline] { (default stage)
[Pipeline] script
[Pipeline] {
[Pipeline] wrap
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ echo CLIENT_ID is ****
CLIENT_ID is ****
+ echo CLIENT_SECRET is ****
CLIENT_SECRET is ****
[Pipeline] }
[Pipeline] // wrap
[Pipeline] echo
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (second)
Stage 'second' skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class com.datapipe.jenkins.vault.exception.VaultPluginException
  at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:417)
  at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:341)
  at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:282)
  at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:201)
  at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:205)
  at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:150)
  at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
  at sun.reflect.GeneratedMethodAccessor517.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
  at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
  at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
  at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
  at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:133)
  at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
  at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
Caused: java.lang.IllegalArgumentException: Could not instantiate {message=com.datapipe.jenkins.vault.exception.VaultPluginException: could not revoke vault lease (mypath/mysecret/tyUslZLnYqMxdeUYuSwHnJys):Expecting HTTP status 204, but instead receiving 403} for EchoStep(message: String)
  at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:287)
  at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:201)
  at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:205)
  at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:150)
  at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
  at sun.reflect.GeneratedMethodAccessor517.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
  at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
  at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
  at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
  at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:133)
  at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
  at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
  at WorkflowScript.run(WorkflowScript:28)
  at ___cps.transform___(Native Method)
  at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
  at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
  at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
  at sun.reflect.GeneratedMethodAccessor453.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
  at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
  at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
  at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
  at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
  at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
  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:122)
  at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
  at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:19)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:35)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:32)
  at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:331)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231)
  at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
  at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

2 个答案:

答案 0 :(得分:0)

要在评论中添加评论,可以使用以下

      try {
        //...
      } catch (err) {
        echo err.message //option 1
      }

或者可以选择echo("${err}")print errecho err.toString()代替选项1。

答案 1 :(得分:0)

感谢您的评论和答案,但我不太关心正确显示错误,而更多地关注原始问题如果存在匹配的字符串,则忽略失败。但是,我仍然采纳了您的建议,并且以下代码段有效:

  } catch (err) {
    // could not revoke vault lease (mypath/mysecret/s689sRpS6t15RYkMOZfw9nj3):Expecting HTTP status 204, but instead receiving 403
    if (!err.message.contains('could not revoke vault lease')) {
      currentBuild.result = 'FAILED'
      error(err.message)
    }
  }