将(相同)参数传递给Jenkins管道中的多个构建作业

时间:2019-05-15 07:51:10

标签: jenkins groovy jenkins-pipeline

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

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

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

//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'
         }
      }
   }
}

我已经找到了答案here,但这似乎使用了时髦的语法,而且我不确定是否也可以在上面的声明性pipline中使用它?

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

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

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

谢谢!

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

//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,

2 个答案:

答案 0 :(得分:0)

我认为您可以在管道级别使用这些参数,并且只需在df = pd.DataFrame({ 'A': [2,3,4,2,6], }) df['flag'] = df['A'].isin(df['A'].iloc[[0, -1]]).astype(int) #alternative #df['flag'] = np.where(df['A'].isin(df['A'].iloc[[0, -1]]), 1, 0) df.iloc[[0, -1], df.columns.get_loc('flag')] = 0 print (df) A flag 0 2 0 1 3 0 2 4 0 3 2 1 4 6 0 调用中传递这些参数即可。

build

答案 1 :(得分:0)

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

    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块中,并在声明性管道中使用它。

相关问题