groovy 在Jenkins管道中使用代理none定义环境变量

u4dcyp6a  于 2023-06-28  发布在  Jenkins
关注(0)|答案(2)|浏览(206)

我有一个管道,我这样做:

#!/bin/groovy

pipeline {
    agent none
    environment {
        agent any
        INSTANCE_NAME = "Jenk"
  }

  stages {
    stage("Print Debug Vars"){
        steps {
            sh 'echo "INSTANCE_NAME is $INSTANCE_NAME"'
        }
    }

    stage("Git Checkout") {
        agent { label 'docker' }
        steps  {
            // ...
            }
      }
    }

    stage("Ansible Galaxy"){
        agent { label 'ansible' }
        steps  {
            // ...
            }
    }
  }
}

问题是如果我把

agent any

在环境部分,它说:

WorkflowScript: 6: "agent any" is not a valid environment expression. Use "key = value" pairs with valid Java/shell keys. @ line 6, column 5.
       agent any
       ^

WorkflowScript: 5: Expected name=value pairs @ line 5, column 5.
       environment {
       ^

如果我不放,我会得到:

Required context class hudson.Launcher is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] End of Pipeline
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.Launcher is missing
    at org.jenkinsci.plugins.workflow.steps.StepDescriptor.checkContextAvailability(StepDescriptor.java:253)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:179)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
    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:18)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:239)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:238)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:76)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inWrappers(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:346)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inWrappers(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:345)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:75)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:218)
    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.GeneratedMethodAccessor279.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.ClosureBlock.eval(ClosureBlock.java:46)
    at com.cloudbees.groovy.cps.Next.step(Next.java:74)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:328)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:240)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:228)
    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:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我该怎么做?

w1jd8yoj

w1jd8yoj1#

environment可以全局设置(针对整个流水线)和每级设置(check official documentation)。但是如果agent none应用于整个管道,它就不能全局设置-这是有意义的,因为如果没有代理,那么环境变量就不能应用于它。
如果你真的需要在流水线级别使用agent none,那么将你的环境块添加到你需要它的阶段:

#!/bin/groovy

pipeline {
  agent none
  stages {
    stage("Print Debug Vars"){
      agent any
      environment {
        INSTANCE_NAME = "Jenk"
      }
      steps {
        sh 'echo "INSTANCE_NAME is $INSTANCE_NAME"'
      }
  }
  ...
}

此外,agent any应直接应用于stage-在environment块中使用它是无效的
当然,首先要考虑使用agent none--它应该使用例如。这样在等待用户输入时没有代理被阻塞。如果你的管道不能做到这一点,也许你可以在顶层使用agent any

envsm3lx

envsm3lx2#

你有没有试过只使用任何值的agent,像这样:

#!/bin/groovy

pipeline {

  agent any

  stages {
    stage("Print Debug Vars"){
        steps {
            sh 'echo "INSTANCE_NAME is $INSTANCE_NAME"'
        }
    }

    stage("Git Checkout") {
        agent { label 'docker' }
        steps  {
            // ...
            }
      }
    }

    stage("Ansible Galaxy"){
        agent { label 'ansible' }
        steps  {
            // ...
            }
    }
  }
}

相关问题