ci: add safe dev job provisioning script
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import hudson.model.Items
|
||||
import jenkins.model.Jenkins
|
||||
import org.jenkinsci.plugins.workflow.job.WorkflowJob
|
||||
|
||||
// 首次在 Jenkins Script Console 执行时保持 false,只输出计划;人工核对后再改为 true。
|
||||
boolean applyChanges = false
|
||||
String templateName = 'baiyue-firstleg-dev-print'
|
||||
List<String> serviceModules = [
|
||||
'gateway', 'gateway-overseas', 'auth', 'iam', 'firstleg', 'finance', 'order', 'warehouse', 'file', 'print',
|
||||
'integration-service', 'lastleg-service', 'warehouse-overseas-service', 'event-bridge-service', 'audit-service'
|
||||
]
|
||||
|
||||
Jenkins jenkins = Jenkins.get()
|
||||
List<WorkflowJob> templates = jenkins.getAllItems(WorkflowJob).findAll { it.name == templateName }
|
||||
if (templates.size() != 1) {
|
||||
throw new IllegalStateException("必须且只能找到一个模板 Job ${templateName},当前数量: ${templates.size()}")
|
||||
}
|
||||
|
||||
WorkflowJob template = templates.first()
|
||||
def parent = template.parent
|
||||
println "模板 Job: ${template.fullName}"
|
||||
println "目标位置: ${parent.fullName ?: '/'}"
|
||||
|
||||
serviceModules.each { service ->
|
||||
String jobName = "baiyue-firstleg-dev-${service}"
|
||||
def existing = parent.getItem(jobName)
|
||||
if (existing != null) {
|
||||
println "保留已有 Job: ${existing.fullName}"
|
||||
} else if (applyChanges) {
|
||||
def created = Items.copy(template, parent, jobName)
|
||||
created.description = "${service} 服务开发环境独立构建与部署"
|
||||
created.save()
|
||||
println "已创建 Job: ${created.fullName}"
|
||||
} else {
|
||||
println "计划创建 Job: ${parent.fullName ? parent.fullName + '/' : ''}${jobName}"
|
||||
}
|
||||
}
|
||||
|
||||
println applyChanges ? '执行完成' : '当前为预览模式,未修改 Jenkins'
|
||||
Reference in New Issue
Block a user