如何访问Jenkins管道脚本中的参数?

问题描述 投票:1回答:2

我正在尝试在管道脚本中使用job参数,跟随Parametrized pipeline using template documentation

我的剧本:

node { 
  // Display the parameter value of the parameter name "myparam" 
  println myparam 
  sh "echo '${myparam}'" 
}

但詹金斯找不到我的参数:

groovy.lang.MissingPropertyException: No such property: myparam for class: WorkflowScript
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:3)
    at ___cps.transform___(Native Method)

我错过了什么?

詹金斯版本:2.8

我的全职xml看起来像这样:

<flow-definition plugin="[email protected]">
   <actions />
   <description />
   <keepDependencies>false</keepDependencies>
   <properties>
      <com.synopsys.arc.jenkinsci.plugins.jobrestrictions.jobs.JobRestrictionProperty plugin="[email protected]" />
      <hudson.model.ParametersDefinitionProperty>
         <parameterDefinitions>
            <hudson.model.StringParameterDefinition>
               <name>myparam</name>
               <description>bar</description>
               <defaultValue>foo</defaultValue>
            </hudson.model.StringParameterDefinition>
         </parameterDefinitions>
      </hudson.model.ParametersDefinitionProperty>
   </properties>
   <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="[email protected]">
      <script>node { //Dislay the parameter value of the parameter name "myparam" println myparam sh "echo '${myparam}'" }</script>
      <sandbox>false</sandbox>
   </definition>
   <triggers />
</flow-definition>
jenkins jenkins-pipeline
2个回答
6
投票

首先定义自定义构建参数:

pipeline {
  parameters {
    string( name: 'BuildConfiguration', 
            defaultValue: 'Release', 
            description: 'Configuration to build (Debug/Release/...)')
  }

单击Jenkins作业页面中的“使用参数构建”后,它将自动显示在页面中。

然后访问脚本中的变量:

echo "Building configuration: ${params.BuildConfiguration}"
echo "Building configuration: " + params.BuildConfiguration

0
投票

如果您的参数名称中包含特殊字符(如点或连字符),则可以通过以下方式访问它:

pipeline {
    stages {
        stage('Test') {
            steps {
                echo "${params['app.jms.jndi-provider-url']}"
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.