Search code examples
jenkinsgroovycontinuous-integrationpipeline

Getting variables from Jenkins runtime


I have several choices in my jenkins pipeline script:

`booleanParam[ name: 'VAR1', defaultValue: true, description: '']
booleanParam[ name: 'VAR2', defaultValue: true, description: '']
booleanParam[ name: 'VAR3', defaultValue: true, description: '']`

I'm trying to make a map like a ["microservice1": true, "microservice2": true, etc.] if I chose params above (tick the params) So Is there any method to get these variables from runtime? I mean Jenkins makes some map|array with all variables by itself. And I can get it with built-in function or somehow else/ Thanks in advance

I wrote just a simble code like a

def getParams() {
  def myArray = []
  if (params.VAR1) {myArray.add("var1")}
  if (params.VAR2) {myArray.add("var2")}
  if (params.VAR3) {myArray.add("var3")}
  return myArray
}

But I feel it's stupid


Solution

  • def toggleValues = [
        'VAR1': 'value1',
        'VAR2': 'value2',
        'VAR3': 'value3'
    ]
    
    toggleValues.findAll{params[it.key] == true}.collect{it.value}