Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins DSL - Parse Yaml for complex processing

Tags:

jenkins

groovy

I'm using Jenkins Job DSL to construct pipelines for multiple SOA style services. All these service pipelines are identical.

job('wibble') {
  publishers {
    downstreamParameterized {
      trigger("SOA_Pipeline_Builder") {
        condition('SUCCESS')
        parameters {
          predefinedProp('PROJECT_NAME', "myproject-2"             )
          predefinedProp('PROJECT_REPO', "[email protected]" )
        }
      }
      trigger("SOA_Pipeline_Builder") {
        condition('SUCCESS')
        parameters {
          predefinedProp('PROJECT_NAME', "myproject-1"             )
          predefinedProp('PROJECT_REPO', "[email protected]" )
        }
      }
    }
  }
}

Given I'm adding in new projects everyday, I have to keep manipulating the DSL. I've decided that i'd rather have all the config in a yaml file outside of the DSL. I know I can use groovy to create arrays, do loops etc, but I'm not having much luck. I'm trying to do something like this...

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
List projects = new Yaml().load(("conf/projects.yml" as File).text)

job('wibble') {
  publishers {
    downstreamParameterized {
      projects.each {
        trigger("SOA_Pipeline_Builder") {
          condition('SUCCESS')
          parameters {
            predefinedProp('PROJECT_NAME', it.name )
            predefinedProp('PROJECT_REPO', it.repo )
          }
        }
      }
    }
  }
}

conf/projects.yml

---
- name: myproject-1
  repo: [email protected]
- name: myproject-2
  repo: [email protected]

Does anyone have any experience with this sort of thing?

like image 879
Steve Stevens Avatar asked Nov 22 '17 20:11

Steve Stevens


1 Answers

This is how I'm using snakeyaml with jobDSL to separate configuration from "application" code.

config.yml

services:
    - some-service-1
    - some-service-2
target_envs:
    - stage
    - prod
folder_path: "promotion-jobs"

seed_job.groovy

#!/usr/bin/groovy
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml

def workDir = SEED_JOB.getWorkspace()
print("Loading config from ${workDir}/config.yml")
def config = new Yaml().load(("${workDir}/config.yml" as File).text)

for (service in config.services) {
    for (stage in config.target_envs) {
        folder("${config.folder_path}/to-${stage}") {
            displayName("Deploy to ${stage} jobs")
            description("Deploy ECS services to ${stage}")
        }

        if (stage == "stage") {
            stage_trigger = """
            pipelineTriggers([cron["1 1 * * 1"])
"""
        } else {
            stage_trigger = ""
        }

        pipelineJob("${config.folder_path}/to-${stage}/${service}") {
            definition {
                cps {
                    sandbox()
                    script("""
    node {
        properties([
            ${stage_trigger}
            parameters([
                choice(
                    choices: ['dev,stage'],
                    description: 'The source environment to promote',
                    name: 'sourceEnv'
                ),
                string(
                    defaultValue: '',
                    description: 'Specify a specific Docker image tag to deploy. This will override sourceEnv and should be left blank',
                    name: 'sourceTag',
                    trim: true
                )
            ])
        ])

        properties([
            disableConcurrentBuilds(),
        ])

        stage('init') {
            dockerPromote(
                app="${service}",
                destinationEnv="${stage}"
            )
        }
    }
                    """)
                }
            }
        }
    }
}
like image 150
chizou Avatar answered Nov 18 '22 03:11

chizou