17

I have a pipeline job named buildall which looks like this:

pipeline {
    stages {
        stage("job1") {
            build job: "job1"
        }
    }
}

The buildall job has 25 parameters. I would like to pass all of buildall's parameters down to job1. Is there an easy way I can do that, instead of manually specifying each parameter?

In this question: Pipeline pass parameters to downstream jobs a sub-question was asked but never answered: Or even better, is there a less cumbersome way in which I can just pass ALL the pipeline parameters to the downstream job.

That's the same question that I have.

Craig Rodrigues
  • 771
  • 2
  • 7
  • 16

3 Answers3

15

The following seems to work (I haven't tested it extensively though):

pipeline {
    agent any
    parameters {
        string(name: 'PARAM1', description: 'Param 1?')
        string(name: 'PARAM2', description: 'Param 2?')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params}"
                script {
                    def myparams = currentBuild.rawBuild.getAction(ParametersAction).getParameters()
                    build job: 'downstream-pipeline-with-params', parameters: myparams
                }    
            }
        }
    }
}

Drawback: to access rawBuild and getAction you have to disable the Groove sandbox or approve these signatures in Jenkins under Manage Jenkins > In-process Script Approval. This dialog will show you that you might introduced a security vulnerability. So it depends on your environment if you want to take this risk or not.

Philip
  • 2,959
  • 1
  • 17
  • 22
  • Thanks @Philip! It works. I wouldn't have figured it out without your help. – Craig Rodrigues Jun 09 '17 at 16:58
  • 1
    why the hell they have done it like that? they have a weird set of *ParameterValue classes, used to call build, but not exposed the whole set to the current build? I was starting with this Jenkins Pipelines but I am thinking to postpone it, specially when there was a specific use case to pass " Current build parameters " that is not supported in pipelines – lqbweb Aug 16 '17 at 14:55
  • Can you also explain how to send "choice parameter" to downstream pipeline job? – MeowRude May 17 '19 at 00:21
13

If you don't care about the parameter types, this approach does not require disabling the Groovy Sandbox - it simply assumes that all parameters can be treated as a string (won't work for "File", for example):

def myparams = params.collect{
    string(name: it.key, value: it.value)
}
build job: 'downstream-job', parameters: myparams

It wouldn't be too hard to expand the logic to handle predefined non-string parameter types, but I agree that this should not be necessary. A better approach would be to expose parameters in the format required by the build() DSL closure, including type specifics that are currently not visible via the "params" global variable, or perhaps to add a boolean, eg:

// I wish:
build job: 'downstream-job', includeMyParameters: true, parameters: anyExtras
Akom
  • 1,484
  • 19
  • 25
1

You can pass all your pipeline parameters with this :

def params=[]
env.getEnvironment().each{ k, v ->
        params.add(string(name:"${k}", value:"${v}"))
   }

def slaveJob = build job: 'BuildJob', parameters:params
851Marc
  • 39
  • 3
  • For anyone coming to this answer, it's not sandbox safe. Good answer, but in my case, Jenkinsfiles should stay sandbox safe. But replace "env.getEnvironment()" with "params" then it stays sandboxed. (see Akom's answer for reference) – trash80 May 13 '20 at 13:36