Jenkins文件环境变量

oprakyz7  于 2023-02-18  发布在  Jenkins
关注(0)|答案(1)|浏览(177)

我有下面的Jenkins管道为我的项目,我设置一个变量COLLECTOR_TOKEN,这将通过 shell 脚本设置。
我想处理一个负面的场景,假设如果shell脚本失败,我发送并退出代码1,这将导致整个管道失败(这是我们所期望的),但问题是在日志中,我们看不到正确的错误消息,因此很难调试。

pipeline {
    agent { label 'maven' }
    options { timeout(time: 300, unit: 'MINUTES') }
    environment {        
        def COLLECTOR_TOKEN = sh(script: "chmod -R 777 *; hello.sh moshin", returnStdout: true).trim()
    }
}

例外:我们可以看到下面的例外。
我想处理下面的异常,并抛出适当的消息.我们怎样才能做到呢?

Error when executing always post condition:
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
    at org.jenkinsci.plugins.workflow.steps.StepDescriptor.checkContextAvailability(StepDescriptor.java:266)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:263)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
    at stepFinalise.call(stepFinalise.groovy:3)
    at WorkflowScript.run(WorkflowScript:56)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.runPostConditions(ModelInterpreter.groovy:756)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:395)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:393)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.runPostConditions(ModelInterpreter.groovy:755)
    at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2030)
    at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2015)
    at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2056)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.runPostConditions(ModelInterpreter.groovy:745)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.runPostConditions(ModelInterpreter.groovy)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executePostBuild(ModelInterpreter.groovy:723)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at jdk.internal.reflect.GeneratedMethodAccessor516.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.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:107)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at jdk.internal.reflect.GeneratedMethodAccessor516.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.ContinuationGroup.methodCall(ContinuationGroup.java:89)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at jdk.internal.reflect.GeneratedMethodAccessor516.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:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
lawou6xi

lawou6xi1#

我有办法了:由于我将returnStdout与shell脚本配合使用,并且该shell脚本的结果有时并不总是正确的输出,有时我会得到一些Jenkins文件中未处理的异常。因此,我必须通过发送错误消息而不是exit 1来处理shell脚本的异常。然后,在管道阶段步骤中,我们必须验证是得到了标记还是得到了异常(基于我们从shell脚本发送的消息)。
如果出现异常,则停止构建。
以下代码仅供参考。

moshin.sh file: 

#!/bin/bash

if [[ $1 == "moshin" ]]; then
  echo "moshin"
else
  echo "not_moshin"
fi
Jenkins Pipeline: 
pipeline {
    agent { label 'maven' }
    environment {
        COLLECTOR_TOKEN = sh(script: "chmod -R 777 *; moshin.sh moshin" , returnStdout: true).trim()
        COLLECTOR_TOKEN_1 = sh(script: "chmod -R 777 *; moshin.sh not_moshin" , returnStdout: true).trim()
    }
    stages {
        stage('PASS') {
            steps {
                script {
                    if (env.COLLECTOR_TOKEN == "moshin") {
                        println "hello moshin"
                    } else {
                        println "Unable to fetch COLLECTOR_TOKEN"
                        currentBuild.result = 'FAILED'
                        sh "exit 1"
                    }
                }
            }
        }
        stage('FAIL') {
            steps {
                script {
                    if (env.COLLECTOR_TOKEN_1 == "moshin") {
                        println "hello moshin"
                    } else {
                        println "Unable to fetch COLLECTOR_TOKEN"
                        currentBuild.result = 'FAILED'
                        sh "exit 1"
                    }
                }
            }
        }
    }
}
Build output: 

[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ chmod -R 777 Jenkinsfile moshin.sh
+ moshin.sh not_moshin
[Pipeline] sh
+ chmod -R 777 Jenkinsfile moshin.sh
+ moshin.sh moshin
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (PASS)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
hello moshin 
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (FAIL)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Unable to fetch COLLECTOR_TOKEN
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: FAILURE

相关问题