bhavicp
bhavicp

Reputation: 355

Pass (same) parameters to multiple build jobs in a Jenkins pipeline

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

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.

Thanks!

Edit: 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
         }
      }
   }
}

Error:

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,

Upvotes: 1

Views: 5600

Answers (2)

haridsv
haridsv

Reputation: 9683

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)])

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

Upvotes: 0

hakamairi
hakamairi

Reputation: 4678

I think you can use the parameters on the pipeline level and just pass the parameters in build calls.

//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: 'Verison' )
        string(name: 'service2', defaultValue: 'NA', description: 'Verison' )
        string(name: 'service3', defaultValue: 'NA', description: 'Verison' )
    }
   stages
   {
      stage('Invoke pipeline primary') {
         steps {
            build job: 'primary', parameters: ([] + params)
         }
      }
      stage('Invoke pipeline secondary') {
         steps {
            build job: 'secondary', parameters: ([] + params)
         }
      }
      stage('backup') {
         steps {
            build job: 'backup', parameters: ([] + params)
         }
      }
   }
} 

Upvotes: 1

Related Questions