2026-07-30 18:16:57 +08:00
|
|
|
|
def serviceModules = [
|
|
|
|
|
|
'gateway', 'auth', 'iam', 'firstleg', 'order', 'warehouse', 'file', 'print',
|
|
|
|
|
|
'integration-service', 'event-bridge-service', 'audit-service'
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2026-07-30 21:46:37 +08:00
|
|
|
|
def serviceCheckboxes = [
|
|
|
|
|
|
'gateway': 'SERVICE_GATEWAY',
|
|
|
|
|
|
'auth': 'SERVICE_AUTH',
|
|
|
|
|
|
'iam': 'SERVICE_IAM',
|
|
|
|
|
|
'firstleg': 'SERVICE_FIRSTLEG',
|
|
|
|
|
|
'order': 'SERVICE_ORDER',
|
|
|
|
|
|
'warehouse': 'SERVICE_WAREHOUSE',
|
|
|
|
|
|
'file': 'SERVICE_FILE',
|
|
|
|
|
|
'print': 'SERVICE_PRINT',
|
|
|
|
|
|
'integration-service': 'SERVICE_INTEGRATION',
|
|
|
|
|
|
'event-bridge-service': 'SERVICE_EVENT_BRIDGE',
|
|
|
|
|
|
'audit-service': 'SERVICE_AUDIT'
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2026-07-30 18:16:57 +08:00
|
|
|
|
def commonPaths = [
|
|
|
|
|
|
'pom.xml', 'platform-bom/', 'common-core/', 'common-web/', 'common-security/',
|
2026-08-03 13:55:39 +08:00
|
|
|
|
'common-openapi/', 'common-audit/', 'common-event-contract/', 'Jenkinsfile',
|
|
|
|
|
|
'Jenkinsfile.service', 'deploy/'
|
2026-07-30 18:16:57 +08:00
|
|
|
|
]
|
|
|
|
|
|
|
2026-08-03 13:55:39 +08:00
|
|
|
|
def downstreamResults = [:]
|
2026-07-30 18:16:57 +08:00
|
|
|
|
|
2026-08-03 13:55:39 +08:00
|
|
|
|
pipeline {
|
|
|
|
|
|
// 总控流水线只识别变更并调度独立的服务 Job;实际 Maven、镜像和部署在 Jenkinsfile.service 中完成。
|
|
|
|
|
|
agent any
|
2026-07-30 21:37:16 +08:00
|
|
|
|
|
2026-07-30 18:16:57 +08:00
|
|
|
|
options {
|
|
|
|
|
|
disableConcurrentBuilds()
|
|
|
|
|
|
timestamps()
|
|
|
|
|
|
buildDiscarder(logRotator(numToKeepStr: '30'))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parameters {
|
2026-07-30 21:46:37 +08:00
|
|
|
|
choice(name: 'BUILD_MODE', choices: ['AUTO', 'SELECT', 'ALL'], description: 'AUTO 按 Git 变更识别;SELECT 勾选服务;ALL 构建全部服务')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_GATEWAY', defaultValue: false, description: 'Gateway 网关服务(gateway)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_AUTH', defaultValue: false, description: '统一认证服务(auth)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_IAM', defaultValue: false, description: '租户与权限服务(iam)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_FIRSTLEG', defaultValue: false, description: '头程履约服务(firstleg)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_ORDER', defaultValue: false, description: '订单服务(order)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_WAREHOUSE', defaultValue: false, description: '仓库与往来单位服务(warehouse)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_FILE', defaultValue: false, description: '文件中心(file)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_PRINT', defaultValue: false, description: '打印服务(print)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_INTEGRATION', defaultValue: false, description: '第三方集成服务(integration-service)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_EVENT_BRIDGE', defaultValue: false, description: '跨区域事件桥(event-bridge-service)')
|
|
|
|
|
|
booleanParam(name: 'SERVICE_AUDIT', defaultValue: false, description: '审计服务(audit-service)')
|
2026-07-30 18:16:57 +08:00
|
|
|
|
choice(name: 'ENVIRONMENT', choices: ['test', 'staging', 'production'], description: '部署环境')
|
2026-08-03 13:55:39 +08:00
|
|
|
|
booleanParam(name: 'DEPLOY', defaultValue: false, description: '各服务构建成功后是否独立部署')
|
|
|
|
|
|
booleanParam(name: 'INITIALIZE_TEST_DATABASE', defaultValue: false, description: '仅 test:调度服务前幂等初始化测试数据库')
|
2026-07-30 18:16:57 +08:00
|
|
|
|
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: '是否跳过测试;生产构建不建议勾选')
|
2026-08-03 13:55:39 +08:00
|
|
|
|
string(name: 'IMAGE_REPOSITORY', defaultValue: '8.163.30.128:10082/java-project/baiyue-firstleg', description: '预发布/生产镜像仓库地址;test 环境自动使用本机镜像')
|
|
|
|
|
|
string(name: 'DOCKER_BASE_IMAGE', defaultValue: 'docker.m.daocloud.io/library/eclipse-temurin:17-jre-alpine', description: 'Java 17 基础镜像')
|
|
|
|
|
|
string(name: 'DOWNSTREAM_JOB_PREFIX', defaultValue: 'baiyue-firstleg-', description: '单服务 Jenkins Job 名称前缀,例如 baiyue-firstleg-auth')
|
2026-07-30 18:16:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stages {
|
|
|
|
|
|
stage('确定构建范围') {
|
|
|
|
|
|
steps {
|
|
|
|
|
|
script {
|
2026-07-30 22:36:54 +08:00
|
|
|
|
String requestedRepository
|
|
|
|
|
|
if (params.ENVIRONMENT == 'test') {
|
|
|
|
|
|
requestedRepository = 'baiyue-firstleg-test'
|
|
|
|
|
|
echo '测试环境使用 Jenkins 节点本机镜像,不登录、不推送远程 Registry'
|
|
|
|
|
|
} else {
|
|
|
|
|
|
requestedRepository = params.IMAGE_REPOSITORY?.trim()
|
|
|
|
|
|
if (!requestedRepository || requestedRepository == 'registry.example.com/your-group/baiyue-firstleg') {
|
2026-08-03 13:55:39 +08:00
|
|
|
|
error('预发布/生产环境必须填写真实镜像仓库地址')
|
2026-07-30 22:36:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (requestedRepository.contains('://') || !requestedRepository.contains('/')) {
|
|
|
|
|
|
error('IMAGE_REPOSITORY 格式错误:请填写 host[:port]/owner/image,不要包含 http:// 或 https://')
|
|
|
|
|
|
}
|
2026-07-30 22:23:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
env.RESOLVED_IMAGE_REPOSITORY = requestedRepository
|
|
|
|
|
|
|
2026-08-03 13:55:39 +08:00
|
|
|
|
String jobPrefix = params.DOWNSTREAM_JOB_PREFIX?.trim()
|
|
|
|
|
|
if (!jobPrefix) {
|
|
|
|
|
|
error('DOWNSTREAM_JOB_PREFIX 不能为空')
|
|
|
|
|
|
}
|
|
|
|
|
|
env.RESOLVED_JOB_PREFIX = jobPrefix
|
|
|
|
|
|
|
2026-07-30 18:16:57 +08:00
|
|
|
|
List selected = []
|
|
|
|
|
|
if (params.BUILD_MODE == 'ALL') {
|
|
|
|
|
|
selected = serviceModules
|
|
|
|
|
|
} else if (params.BUILD_MODE == 'SELECT') {
|
2026-07-30 21:46:37 +08:00
|
|
|
|
selected = serviceModules.findAll { service ->
|
|
|
|
|
|
params[serviceCheckboxes[service]] == true
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!selected) {
|
|
|
|
|
|
error('SELECT 模式至少需要勾选一个服务')
|
|
|
|
|
|
}
|
2026-07-30 18:16:57 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
String base = env.GIT_PREVIOUS_SUCCESSFUL_COMMIT?.trim()
|
|
|
|
|
|
if (!base || sh(script: "git cat-file -e ${base}^{commit} 2>/dev/null", returnStatus: true) != 0) {
|
|
|
|
|
|
selected = serviceModules
|
|
|
|
|
|
} else {
|
|
|
|
|
|
List changed = sh(script: "git diff --name-only ${base} HEAD", returnStdout: true)
|
|
|
|
|
|
.trim().split('\\n').collect { it.trim() }.findAll { it }
|
2026-08-03 13:55:39 +08:00
|
|
|
|
boolean commonChanged = changed.any { path ->
|
|
|
|
|
|
commonPaths.any { prefix -> path == prefix || path.startsWith(prefix) }
|
|
|
|
|
|
}
|
2026-07-30 18:16:57 +08:00
|
|
|
|
selected = commonChanged ? serviceModules : serviceModules.findAll { module ->
|
|
|
|
|
|
changed.any { it == module || it.startsWith("${module}/") }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List invalid = selected.findAll { !serviceModules.contains(it) }
|
|
|
|
|
|
if (invalid) {
|
|
|
|
|
|
error("未知服务: ${invalid.join(', ')};允许值: ${serviceModules.join(', ')}")
|
|
|
|
|
|
}
|
2026-08-03 13:55:39 +08:00
|
|
|
|
|
2026-07-30 18:16:57 +08:00
|
|
|
|
selected = selected.unique()
|
2026-08-03 13:55:39 +08:00
|
|
|
|
env.SELECTED_SERVICES = selected.join(',')
|
|
|
|
|
|
env.NO_SERVICE_CHANGE = selected ? 'false' : 'true'
|
2026-07-30 18:16:57 +08:00
|
|
|
|
if (!selected) {
|
|
|
|
|
|
currentBuild.description = '没有需要构建的业务服务'
|
|
|
|
|
|
} else {
|
2026-08-03 13:55:39 +08:00
|
|
|
|
env.SOURCE_COMMIT = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
2026-07-30 18:16:57 +08:00
|
|
|
|
env.IMAGE_TAG = sh(script: 'git rev-parse --short=12 HEAD', returnStdout: true).trim()
|
|
|
|
|
|
currentBuild.description = "${env.SELECTED_SERVICES} | ${env.IMAGE_TAG}"
|
2026-08-03 13:55:39 +08:00
|
|
|
|
echo "本次独立调度服务: ${env.SELECTED_SERVICES}"
|
2026-07-30 18:16:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-30 22:23:58 +08:00
|
|
|
|
stage('生产环境部署确认(仅 production 执行)') {
|
2026-07-30 18:16:57 +08:00
|
|
|
|
when {
|
|
|
|
|
|
allOf {
|
|
|
|
|
|
expression { params.DEPLOY && params.ENVIRONMENT == 'production' && env.NO_SERVICE_CHANGE != 'true' }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
steps {
|
2026-08-03 13:55:39 +08:00
|
|
|
|
input message: "确认将 ${env.SELECTED_SERVICES} 的镜像 ${env.IMAGE_TAG} 分别部署到生产环境?", ok: '确认部署'
|
2026-07-30 18:16:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-31 13:52:35 +08:00
|
|
|
|
stage('测试数据库初始化(可选)') {
|
|
|
|
|
|
when {
|
|
|
|
|
|
allOf {
|
|
|
|
|
|
expression { params.ENVIRONMENT == 'test' && params.INITIALIZE_TEST_DATABASE }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
steps {
|
|
|
|
|
|
sh 'sh deploy/scripts/initialize-test-databases.sh'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-03 13:55:39 +08:00
|
|
|
|
stage('并行调度独立服务 Job') {
|
|
|
|
|
|
when { expression { env.NO_SERVICE_CHANGE != 'true' } }
|
2026-07-30 18:16:57 +08:00
|
|
|
|
steps {
|
|
|
|
|
|
script {
|
2026-08-03 13:55:39 +08:00
|
|
|
|
Map branches = [:]
|
|
|
|
|
|
env.SELECTED_SERVICES.split(',').each { selectedService ->
|
|
|
|
|
|
String service = selectedService
|
|
|
|
|
|
branches[service] = {
|
|
|
|
|
|
String jobName = "${env.RESOLVED_JOB_PREFIX}${service}"
|
|
|
|
|
|
echo "触发单服务 Job: ${jobName}"
|
|
|
|
|
|
def downstreamBuild = build(
|
|
|
|
|
|
job: jobName,
|
|
|
|
|
|
wait: true,
|
|
|
|
|
|
propagate: false,
|
|
|
|
|
|
parameters: [
|
|
|
|
|
|
string(name: 'SOURCE_COMMIT', value: env.SOURCE_COMMIT),
|
|
|
|
|
|
string(name: 'IMAGE_TAG', value: env.IMAGE_TAG),
|
|
|
|
|
|
string(name: 'ENVIRONMENT', value: params.ENVIRONMENT),
|
|
|
|
|
|
booleanParam(name: 'DEPLOY', value: params.DEPLOY),
|
|
|
|
|
|
booleanParam(name: 'SKIP_TESTS', value: params.SKIP_TESTS),
|
|
|
|
|
|
string(name: 'IMAGE_REPOSITORY', value: env.RESOLVED_IMAGE_REPOSITORY),
|
|
|
|
|
|
string(name: 'DOCKER_BASE_IMAGE', value: params.DOCKER_BASE_IMAGE)
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
downstreamResults[service] = downstreamBuild.result ?: 'UNKNOWN'
|
|
|
|
|
|
echo "${service} 执行结果: ${downstreamResults[service]} (${downstreamBuild.absoluteUrl})"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
parallel branches
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stage('汇总服务结果') {
|
|
|
|
|
|
when { expression { env.NO_SERVICE_CHANGE != 'true' } }
|
|
|
|
|
|
steps {
|
|
|
|
|
|
script {
|
|
|
|
|
|
List failed = downstreamResults.findAll { service, result -> result != 'SUCCESS' }
|
|
|
|
|
|
.collect { service, result -> "${service}=${result}" }
|
|
|
|
|
|
String summary = downstreamResults.collect { service, result -> "${service}=${result}" }.join(', ')
|
|
|
|
|
|
echo "单服务执行汇总: ${summary}"
|
|
|
|
|
|
currentBuild.description = "${env.IMAGE_TAG} | ${summary}"
|
|
|
|
|
|
if (failed) {
|
|
|
|
|
|
unstable("部分服务执行失败,但其他服务已独立完成:${failed.join(', ')}")
|
2026-07-30 18:16:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
post {
|
|
|
|
|
|
always { cleanWs(deleteDirs: true, notFailBuild: true) }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|