This commit is contained in:
2026-08-20 12:49:54 +08:00
parent c83620a86a
commit b029f4ce28
4 changed files with 237 additions and 19 deletions
+58 -12
View File
@@ -64,7 +64,7 @@ pipeline {
String sourceCommit = params.SOURCE_COMMIT?.trim()
String imageTag = params.IMAGE_TAG?.trim()
String imageRepository = ['development', 'test'].contains(params.ENVIRONMENT)
? '172.31.198.7:10082/java-project/baiyue-firstleg'
? "baiyue-firstleg-${params.ENVIRONMENT}"
: params.IMAGE_REPOSITORY?.trim()
if (!sourceCommit) {
@@ -83,8 +83,11 @@ pipeline {
if (!imageTag || !(imageTag ==~ /[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}/)) {
error('IMAGE_TAG 不是合法的 Docker 镜像标签')
}
if (!imageRepository || imageRepository.contains('://') || !imageRepository.contains('/')) {
error('IMAGE_REPOSITORY 必须使用 host[:port]/owner/image 格式,且不能包含协议前缀')
if (!imageRepository || imageRepository.contains('://')) {
error('IMAGE_REPOSITORY 不能为空且不能包含协议前缀')
}
if (!['development', 'test'].contains(params.ENVIRONMENT) && !imageRepository.contains('/')) {
error('预发布/生产环境 IMAGE_REPOSITORY 必须使用 host[:port]/owner/image 格式')
}
env.RESOLVED_SERVICE = service
@@ -140,16 +143,59 @@ pipeline {
sh "mkdir -p '${contextDir}' && cp -f '${jar}' '${contextDir}/app.jar'"
sh "docker build -f deploy/docker/Dockerfile --build-arg BASE_IMAGE=${params.DOCKER_BASE_IMAGE} --build-arg JAR_FILE=app.jar -t ${image} '${contextDir}'"
echo "推送共享镜像: ${image}"
withCredentials([usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')]) {
try {
sh 'printf %s "$REGISTRY_PASSWORD" | docker login -u "$REGISTRY_USER" --password-stdin "' + registryHost + '"'
sh "docker push ${image}"
} finally {
sh "docker logout ${registryHost} || true"
if (['development', 'test'].contains(params.ENVIRONMENT)) {
echo "${params.ENVIRONMENT} 镜像已保存在构建节点 ${env.NODE_NAME}: ${image}"
} else {
echo "推送共享镜像: ${image}"
withCredentials([usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')]) {
try {
sh 'printf %s "$REGISTRY_PASSWORD" | docker login -u "$REGISTRY_USER" --password-stdin "' + registryHost + '"'
sh "docker push ${image}"
} finally {
sh "docker logout ${registryHost} || true"
}
}
if (params.DEPLOY) {
stash name: 'deployment-files', includes: 'deploy/**', useDefaultExcludes: true
}
}
}
}
}
stage('直接部署开发或测试环境') {
when {
expression { params.DEPLOY && ['development', 'test'].contains(params.ENVIRONMENT) }
}
steps {
script {
boolean development = params.ENVIRONMENT == 'development'
boolean targetIsLocal = development
? env.NODE_LABELS?.tokenize()?.contains('java-dev')
: env.NODE_NAME == 'master'
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}",
"TARGET_SSH_TARGET=${remoteTarget}",
"TARGET_SSH_KEY=${remoteKey}",
"TARGET_SSH_KNOWN_HOSTS=${remoteKnownHosts}",
"TARGET_DEPLOY_DIR=${remoteDir}"
]) {
withCredentials([file(credentialsId: credentialId, variable: '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\""
}
}
stash name: 'deployment-files', includes: 'deploy/**', useDefaultExcludes: true
}
}
}
@@ -164,7 +210,7 @@ pipeline {
stage('部署单个服务') {
when {
beforeAgent true
expression { params.DEPLOY }
expression { params.DEPLOY && !['development', 'test'].contains(params.ENVIRONMENT) }
}
agent {
label "${params.ENVIRONMENT == 'development' ? 'java-dev' : 'master'}"