Is it possible to import yaml as a map object for active-choices-reactive parameters?

17 views
Skip to first unread message

Avi Kessner

unread,
Jun 7, 2024, 8:22:31 AM6/7/24
to jenkins-job-builder
If this is the wrong place to ask this, then please let me know the correct place.

I have the following code for parameter configuration in my jenkins-job-builder yaml files, written across multiple jobs.

```
      - choice:
          name: CLUSTER
          choices:
            !include: ../clusters.yaml.inc
          description: 'a cluster name'
      - active-choices-reactive:
          name: NAMESPACE
          description: '(required) a namespace'
          script:
              groovy: |-
                  List namespaces1 = ["name", "space"]
                  List namespaces2 = ["none", "names", "spaces"]
                  // ... 10 more lists
                  def namespaces = []
                  if (CLUSTER == "cluster-1") (
                      namespaces = namespaces1
                  ) else if (CLUSTER == "cluster-2") (
                      namespaces = namespaces2
                  //... 10 more if statments
                  ) else (
                      namespaces = defaultList
                  )
                  return namespaces
              use-groovy-sandbox: true
          fallback-script:
              groovy: |-
                  return ['unknown']
              use-groovy-sandbox: true
          choice-type: single-select
          enable-filters: false
          referenced-parameters: CLUSTER

```

I would like to convert this code to something easier to maintain, such as the following.

```
      - hidden:
          name: CLUSTER_NS_MAP
          default:
             !include: ../cluster_namespace_map.yaml.inc
      - choice:
          name: CLUSTER
          choices:
            !include: ../clusters.yaml.inc
          description: 'a cluster name'
      - active-choices-reactive:
          name: NAMESPACE
          description: '(required) a namespace'
          script:
              groovy: |-
                  def namespaces = CLUSTER_NS_MAP[CLUSTER]
                  return namespaces
              use-groovy-sandbox: true
          fallback-script:
              groovy: |-
                  return ['unknown']
              use-groovy-sandbox: true
          choice-type: single-select
          enable-filters: false
          referenced-parameters: CLUSTER,CLUSTER_NS_MAP
```

All of my attempts to get this code or something similar to work have failed.
I tried using JSONSlurper to read the list as json, I've tried using `{% set clustermap =  !include %}, and I've tried hard coded in some json to debug the issue, but that throws errors that : and { are invalid formatting characters.

Is it possible to do this? Is there a better way to do it? Thank you.

Avi Kessner

unread,
Jun 9, 2024, 4:55:07 AM6/9/24
to jenkins-job-builder
It took me a while, but I finally figured it out.

Here were the issues.
1. The form can not load the contents of the file at run time, nor could it import any external libraries.  So the file had to be rendered into a different parameter to start.
2. The groovy version supported by the form was older than I expected. So I could not use any yaml parsers. I needed to convert the yaml output to valid json by replacing all ' with "
3. The fallback script was hard to debug, leading to some false starts. I figured out I could output .size() as a string inside of an array, to debug navigating the yaml turned json properly.  In the end, this was the script I used:

import groovy.json.*
def rawText = CLUSTER_MAP
def jsonified = rawText.replaceAll("\'", "\"")
def jslurp = new groovy.json.JsonSlurper().parseText(jsonified)
return jslurp[CLUSTER]
Reply all
Reply to author
Forward
0 new messages