fix(ci): deploy on target environment agents

This commit is contained in:
2026-08-22 17:43:20 +08:00
parent e398f1f7e0
commit 232c2657f1
2 changed files with 36 additions and 47 deletions
+33 -46
View File
@@ -47,9 +47,6 @@ pipeline {
REGISTRY_CREDENTIALS = 'gitea-registry-token'
MAVEN_SETTINGS = 'deploy/maven/settings-ci.xml'
MAVEN_OPTS = '-Dfile.encoding=UTF-8 -Dmaven.wagon.http.retryHandler.count=3'
DEVELOPMENT_SSH_TARGET = 'deploy@172.31.198.8'
DEVELOPMENT_SSH_KEY = '/var/jenkins_home/.ssh/dev_deploy'
DEVELOPMENT_DEPLOY_DIR = '/opt/firstleg/application'
}
stages {
@@ -154,56 +151,46 @@ pipeline {
sh "docker logout ${registryHost} || true"
}
}
if (params.DEPLOY && !['development', 'test'].contains(params.ENVIRONMENT)) {
if (params.DEPLOY) {
stash name: 'deployment-files', includes: 'deploy/**', useDefaultExcludes: true
}
}
}
}
}
post {
always {
cleanWs(deleteDirs: true, notFailBuild: true)
}
}
}
stage('直接部署开发或测试环境') {
when {
expression { params.DEPLOY && ['development', 'test'].contains(params.ENVIRONMENT) }
}
steps {
script {
String deployLock = "firstleg-${params.ENVIRONMENT}-deploy"
lock(resource: deployLock, inversePrecedence: true) {
boolean development = params.ENVIRONMENT == 'development'
boolean testTargetIsLocal = ['master', 'built-in'].contains(env.NODE_NAME) ||
['master', 'built-in'].any { label -> env.NODE_LABELS?.tokenize()?.contains(label) }
boolean targetIsLocal = development
? env.NODE_LABELS?.tokenize()?.contains('java-dev')
: testTargetIsLocal
echo "构建节点 ${env.NODE_NAME} 部署到 ${params.ENVIRONMENT},目标是否为本机: ${targetIsLocal}"
String credentialId = "firstleg-${params.ENVIRONMENT}-env"
String remoteTarget = development ? 'deploy@172.31.198.8' : 'deploy@172.31.198.7'
String remoteKey = development
? '/var/jenkins_home/.ssh/dev_deploy'
: '/home/deploy/.ssh/test_deploy'
String remoteKnownHosts = development
? '/var/jenkins_home/.ssh/known_hosts'
: '/home/deploy/.ssh/known_hosts'
String remoteDir = development
? '/opt/firstleg/application'
: '/opt/firstleg/test-application'
withEnv([
"TARGET_IS_LOCAL=${targetIsLocal}",
"EVENT_BRIDGE_REGION_OVERRIDE=${params.EVENT_BRIDGE_REGION}",
"TARGET_SSH_TARGET=${remoteTarget}",
"TARGET_SSH_KEY=${remoteKey}",
"TARGET_SSH_KNOWN_HOSTS=${remoteKnownHosts}",
"TARGET_DEPLOY_DIR=${remoteDir}"
stage('部署开发或测试环境') {
when {
beforeAgent true
expression { params.DEPLOY && ['development', 'test'].contains(params.ENVIRONMENT) }
}
agent {
label "${params.ENVIRONMENT == 'development' ? 'java-dev' : 'java-test'}"
}
steps {
deleteDir()
unstash 'deployment-files'
script {
String deployLock = "firstleg-${params.ENVIRONMENT}-deploy"
String credentialId = "firstleg-${params.ENVIRONMENT}-env"
lock(resource: deployLock, inversePrecedence: true) {
echo "在目标节点 ${env.NODE_NAME} 本机部署到 ${params.ENVIRONMENT}"
withEnv([
'TARGET_IS_LOCAL=true',
"EVENT_BRIDGE_REGION_OVERRIDE=${params.EVENT_BRIDGE_REGION}"
]) {
withCredentials([
file(credentialsId: credentialId, variable: 'DEPLOY_ENV_FILE'),
usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS,
usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')
]) {
withCredentials([
file(credentialsId: credentialId, variable: 'DEPLOY_ENV_FILE'),
usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS,
usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')
]) {
sh "sh deploy/scripts/deploy-built-service.sh '${params.ENVIRONMENT}' '${env.RESOLVED_IMAGE_REPOSITORY}' '${env.RESOLVED_IMAGE_TAG}' '${env.RESOLVED_SERVICE}' \"\$DEPLOY_ENV_FILE\""
}
}
sh "sh deploy/scripts/deploy-built-service.sh '${params.ENVIRONMENT}' '${env.RESOLVED_IMAGE_REPOSITORY}' '${env.RESOLVED_IMAGE_TAG}' '${env.RESOLVED_SERVICE}' \"\$DEPLOY_ENV_FILE\""
}
}
}
@@ -37,10 +37,12 @@ test('production deployments cannot skip tests', () => {
test('Java builds use the shared builder pool and serialize incremental target deployments', () => {
assert.match(pipeline, /agent \{ label 'java-builder' \}/)
assert.match(pipeline, /\['development', 'test'\]\.contains\(params\.ENVIRONMENT\)/)
assert.match(pipeline, /\['master', 'built-in'\]\.contains\(env\.NODE_NAME\)/)
assert.match(pipeline, /params\.ENVIRONMENT == 'development' \? 'java-dev' : 'java-test'/)
assert.match(pipeline, /'TARGET_IS_LOCAL=true'/)
assert.match(pipeline, /String deployLock = "firstleg-\$\{params\.ENVIRONMENT\}-deploy"/)
assert.match(pipeline, /lock\(resource: deployLock, inversePrecedence: true\)/)
assert.match(pipeline, /deploy-built-service\.sh/)
assert.doesNotMatch(pipeline, /dev_deploy|test_deploy|TARGET_SSH_KEY/)
assert.match(directDeployScript, /docker pull "\$IMAGE"/)
assert.match(directDeployScript, /run_ssh docker pull "\$IMAGE"/)
assert.doesNotMatch(directDeployScript, /docker save "\$IMAGE"/)