Jenkins DSL - 解析Yaml进行复杂处理

时间:2017-11-22 20:07:14

标签: jenkins groovy

我使用Jenkins Job DSL为多个SOA样式服务构建管道。所有这些服务管道都是相同的。

job('wibble') {
  publishers {
    downstreamParameterized {
      trigger("SOA_Pipeline_Builder") {
        condition('SUCCESS')
        parameters {
          predefinedProp('PROJECT_NAME', "myproject-2"             )
          predefinedProp('PROJECT_REPO', "myprojecttwo@gitrepo.com" )
        }
      }
      trigger("SOA_Pipeline_Builder") {
        condition('SUCCESS')
        parameters {
          predefinedProp('PROJECT_NAME', "myproject-1"             )
          predefinedProp('PROJECT_REPO', "myprojectone@gitrepo.com" )
        }
      }
    }
  }
}

鉴于我每天都在添加新项目,我必须继续操纵DSL。我已经决定将所有配置都放在DSL之外的yaml文件中。我知道我可以使用groovy创建数组,做循环等,但我没有太多运气。 我试图做这样的事情......

@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: myprojectone@gitrepo.com
- name: myproject-2
  repo: myprojecttwo@gitrepo.com

有没有人有这方面的经验?

1 个答案:

答案 0 :(得分:0)

这就是我在工作DSL上使用snakeyaml将配置与“应用程序”代码分开的方法。

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