fix(ci): serialize deployments per service

This commit is contained in:
2026-08-25 11:53:59 +08:00
parent 4fbf8bfb53
commit 322de03312
2 changed files with 36 additions and 27 deletions
+32 -27
View File
@@ -194,38 +194,43 @@ pipeline {
steps { steps {
script { script {
String deploySlotLabel = "firstleg-${params.ENVIRONMENT}-deploy-slot" String deploySlotLabel = "firstleg-${params.ENVIRONMENT}-deploy-slot"
String serviceDeployResource = "firstleg-${params.ENVIRONMENT}-${env.RESOLVED_SERVICE}-deploy"
String credentialId = "firstleg-${params.ENVIRONMENT}-env" String credentialId = "firstleg-${params.ENVIRONMENT}-env"
String targetLabel = params.ENVIRONMENT == 'development' ? 'java-dev' : 'java-test' String targetLabel = params.ENVIRONMENT == 'development' ? 'java-dev' : 'java-test'
lock(label: deploySlotLabel, quantity: 1, inversePrecedence: true) { // 不同服务可以并行,但同一环境的同一服务只能有一个任务更新容器。
node(targetLabel) { // resource 未预配置时 Lockable Resources 会创建仅在构建期间存在的临时资源。
deleteDir() lock(resource: serviceDeployResource, inversePrecedence: true) {
try { lock(label: deploySlotLabel, quantity: 1, inversePrecedence: true) {
unstash 'deployment-files' node(targetLabel) {
echo "在目标节点 ${env.NODE_NAME} 本机部署到 ${params.ENVIRONMENT}" deleteDir()
List bridgeRegions = (env.RESOLVED_SERVICE == 'event-bridge-service' && try {
(env.DEPLOY_BOTH_EVENT_BRIDGES == 'true' || unstash 'deployment-files'
['BOTH', 'SELECT'].contains(params.EVENT_BRIDGE_REGION))) echo "在目标节点 ${env.NODE_NAME} 本机部署到 ${params.ENVIRONMENT}"
? ['CN', 'OVERSEAS'] List bridgeRegions = (env.RESOLVED_SERVICE == 'event-bridge-service' &&
: [params.EVENT_BRIDGE_REGION] (env.DEPLOY_BOTH_EVENT_BRIDGES == 'true' ||
withCredentials([ ['BOTH', 'SELECT'].contains(params.EVENT_BRIDGE_REGION)))
file(credentialsId: credentialId, variable: 'DEPLOY_ENV_FILE'), ? ['CN', 'OVERSEAS']
usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS, : [params.EVENT_BRIDGE_REGION]
usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD') withCredentials([
]) { file(credentialsId: credentialId, variable: 'DEPLOY_ENV_FILE'),
bridgeRegions.each { bridgeRegion -> usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS,
if (env.RESOLVED_SERVICE == 'event-bridge-service') { usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')
echo "部署事件桥实例: ${bridgeRegion}" ]) {
} bridgeRegions.each { bridgeRegion ->
withEnv([ if (env.RESOLVED_SERVICE == 'event-bridge-service') {
'TARGET_IS_LOCAL=true', echo "部署事件桥实例: ${bridgeRegion}"
"EVENT_BRIDGE_REGION_OVERRIDE=${bridgeRegion}" }
]) { withEnv([
sh "sh deploy/scripts/deploy-built-service.sh '${params.ENVIRONMENT}' '${env.RESOLVED_IMAGE_REPOSITORY}' '${env.RESOLVED_IMAGE_TAG}' '${env.RESOLVED_SERVICE}' \"\$DEPLOY_ENV_FILE\"" 'TARGET_IS_LOCAL=true',
"EVENT_BRIDGE_REGION_OVERRIDE=${bridgeRegion}"
]) {
sh "sh deploy/scripts/deploy-built-service.sh '${params.ENVIRONMENT}' '${env.RESOLVED_IMAGE_REPOSITORY}' '${env.RESOLVED_IMAGE_TAG}' '${env.RESOLVED_SERVICE}' \"\$DEPLOY_ENV_FILE\""
}
} }
} }
} finally {
cleanWs(deleteDirs: true, notFailBuild: true)
} }
} finally {
cleanWs(deleteDirs: true, notFailBuild: true)
} }
} }
} }
@@ -71,13 +71,17 @@ test('development and test deployment locks are acquired before target executors
pipeline.indexOf("stage('部署开发或测试环境')"), pipeline.indexOf("stage('部署开发或测试环境')"),
pipeline.indexOf("stage('部署单个服务')") pipeline.indexOf("stage('部署单个服务')")
) )
const serviceLockIndex = directDeployStage.indexOf('lock(resource: serviceDeployResource')
const lockIndex = directDeployStage.indexOf('lock(label: deploySlotLabel') const lockIndex = directDeployStage.indexOf('lock(label: deploySlotLabel')
const nodeIndex = directDeployStage.indexOf('node(targetLabel)') const nodeIndex = directDeployStage.indexOf('node(targetLabel)')
assert.match(directDeployStage, /agent none/) assert.match(directDeployStage, /agent none/)
assert.match(directDeployStage, /String targetLabel = params\.ENVIRONMENT == 'development' \? 'java-dev' : 'java-test'/) assert.match(directDeployStage, /String targetLabel = params\.ENVIRONMENT == 'development' \? 'java-dev' : 'java-test'/)
assert.match(directDeployStage, /String serviceDeployResource = "firstleg-\$\{params\.ENVIRONMENT\}-\$\{env\.RESOLVED_SERVICE\}-deploy"/)
assert.ok(serviceLockIndex >= 0, 'per-service deployment lock must exist')
assert.ok(lockIndex >= 0, 'deployment slot lock must exist') assert.ok(lockIndex >= 0, 'deployment slot lock must exist')
assert.ok(nodeIndex >= 0, 'target node allocation must exist') assert.ok(nodeIndex >= 0, 'target node allocation must exist')
assert.ok(serviceLockIndex < lockIndex, 'per-service lock must be acquired before the shared deployment slot')
assert.ok(lockIndex < nodeIndex, 'deployment lock must be acquired before target node allocation') assert.ok(lockIndex < nodeIndex, 'deployment lock must be acquired before target node allocation')
}) })