将(相同)参数传递给Jenkins管道中的多个构建作业 [英] Pass (same) parameters to multiple build jobs in a Jenkins pipeline

查看:150
本文介绍了将(相同)参数传递给Jenkins管道中的多个构建作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有多个作业,主要",次要"和备份"-所有作业都必须具有相同的参数(发行版,即"1.5.1")-大约有15个.

We have multiple jobs, 'primary', 'secondary' and 'backup' - All need to have the same parameters (release versions i.e '1.5.1') - There at around 15 of them.

parameters{
    string(name: 'service1', defaultValue: 'NA', description: 'Verison' )
    string(name: 'service2', defaultValue: 'NA', description: 'Verison' )
    string(name: 'service3', defaultValue: 'NA', description: 'Verison' )
}

我的管道如下所示,如何在所有3个构建作业中都使用相同的上述参数,而不必三遍指定参数?

My pipeline is like the below, how can I use the same above paramaters for all 3 build jobs without having to specify the parameters three times?

//This will kick of the three pipeline scripts required to do a release in PROD 
pipeline {
   agent any
   stages
   {
      stage('Invoke pipeline primary') {
         steps {
            build job: 'primary'
         }
      }
      stage('Invoke pipeline secondary') {
         steps {
            build job: 'secondary'
         }
      }
      stage('backup') {
         steps {
            build job: 'backup'
         }
      }
   }
}

我找到了这个答案

I've found this answer here, but this seems to use groovy syntax and i'm not sure if this can also be used in a declarative pipline like the above?

尝试时,我得到以下信息:

When I tried it, I get the below:

Running on Jenkins in PipelineTest
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Invoke pipeline primary)
[Pipeline] build
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: No item named null found
Finished: FAILURE

当我自己运行此主管道时,它会按预期运行.

When I run this primary pipeline by itself, it runs as expected.

谢谢!

尝试了@hakamairi提供的答案,但得到了以下答案,我对DSL不太满意,但尝试了几种不同的变体,但没有一个可行的/都在期望Pa​​ramValue周围出现类似类型的错误.

Tried the answer provided by @hakamairi but get the below, I'm not great at DSL but tried a few different variations and none worked / all had similar types of errors around expecting a ParamValue.

//This will kick of the three pipeline scripts required to do a release in PROD 
pipeline {
   agent any

   parameters{
       string(name: 'service1', defaultValue: 'NA', description: 'Version' )
       string(name: 'service2', defaultValue: 'NA', description: 'Version' )
  }

   stages
   {
      stage('Invoke pipeline PrimaryRelease') {
         steps {
             build job: 'PythonBuildTest', parameters: params
         }
      }
   }
}

错误:

java.lang.UnsupportedOperationException:必须指定$ class和 接口java.util.List的实现,位于 org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:503) 在 org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:402) 在 org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:361) 在 org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:284) 在 org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:201) 在org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:208) 在org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153) 在 org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122) 在sun.reflect.GeneratedMethodAccessor956.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) 在groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)处 groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)在 groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)在 org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42) 在 org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 在org.kohsuke.groovy.sandbox.impl.Checker $ 1.call(Checker.java:157) 在 org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23) 在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:133) 在org.kohsuke.groovy.sandbox.impl.Checker $ 1.call(Checker.java:155) 在 org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159) 在 org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129) 在 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17) 原因:java.lang.IllegalArgumentException:无法实例化 {job = PythonBuildTest,参数= {service1 = NA,

java.lang.UnsupportedOperationException: must specify $class with an implementation of interface java.util.List at org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:503) at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:402) at org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:361) at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:284) at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:201) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:208) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122) at sun.reflect.GeneratedMethodAccessor956.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 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17) Caused: java.lang.IllegalArgumentException: Could not instantiate {job=PythonBuildTest, parameters={service1=NA,

推荐答案

我主要使用脚本化方法,如下所示:

I mostly use scripted approach and something like the below works:

    def all_params = [
            string(name: 'service1', defaultValue: 'NA', description: 'Version' ),
            string(name: 'service2', defaultValue: 'NA', description: 'Version' ),
            string(name: 'service3', defaultValue: 'NA', description: 'Version' ),
        ]
    properties([parameters(all_params)])

应该可以将上述代码包装在script块中,并在声明性管道中使用它.

It should be possible to wrap the above code in a script block and use it in a declarative pipeline as well.

这篇关于将(相同)参数传递给Jenkins管道中的多个构建作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆