gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:51:34 25 4
gpt4 key购买 nike

我正在使用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

有人对这种事情有经验吗?

最佳答案

这就是我如何将蛇形文字与jobDSL一起使用,以将配置与“应用程序”代码分开。
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}"
)
}
}
""")
}
}
}
}
}

关于Jenkins DSL-解析Yaml以进行复杂处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47443106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com