Jenkins文件:使用返回“ null”的参数触发另一个项目

时间:2019-12-27 16:06:00

标签: spring maven jenkins jenkins-pipeline

我正尝试在jenkins上创建一个前端项目,以仅使用jenkinsfile来触发我的质量检查项目。

Atm前端项目有一个阶段,该阶段将使用以下代码触发我的工作:

    stage('Trigger QA job'){
          build '../qa-project/qatests'
          //parameters: [[$class: 'StringParameterValue', name: 'environment', value: "$environment"]]
          parameters: [string(name: 'environment', value: $env.environment)]
          wait: true

environment在管道的开头就这样声明:

node('PCF') {
    def environment = 'test'

...

在Qa项目上,我正在jenkinsfile上使用此方法运行测试:

    stage('tests'){
      runTests()
    }

def runTests(Map params = [:]){
  def targetFolder = $env.targetfolder ?: "tests"
  sh "chmod u+x mvnw"

    configFileProvider([configFile(fileId: jenkinsSettingsXmlId, variable: 'MAVEN_SETTINGS)]){
      def targetEnv = sh "echo $env.environment"
      sh "SPRING_PROFILE_ACTIVE=$targetEnvironment ./mvnw -B -s $MAVEN_SETTINGS -Dtest.folder=$targetFolder test

返回此错误:

[Pipeline] {
[Pipeline] sh
+ echo null
null

...

groovy.lang.MissingPropertyException: No such property: environment for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
    at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:291)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:295)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:271)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:41)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
    at sun.reflect.GeneratedMethodAccessor305.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.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:405)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:317)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:281)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    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.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)```


i've managed to make it before catch a "null" variable not letting me advance too, trying other approaches. I would like to know if is there a way to make this run without any plugin type of approach, as i wanted it to be the most abstract as possible.

Thanks in advance.

1 个答案:

答案 0 :(得分:0)

正如您在下游构建中声明的参数一样,

parameters {
        string(name: "environment", defaultValue: "", description: "The environment")
    }

您需要使用params访问参数的值:

echo "Environment is ${params.environment}"

另请参阅文档:https://jenkins.io/doc/book/pipeline/syntax/#parameters