暂存提交

This commit is contained in:
2026-07-30 22:36:54 +08:00
parent 1083c40070
commit 86547fc053
5 changed files with 48 additions and 25 deletions
Vendored
+30 -17
View File
@@ -67,7 +67,7 @@ pipeline {
choice(name: 'ENVIRONMENT', choices: ['test', 'staging', 'production'], description: '部署环境')
booleanParam(name: 'DEPLOY', defaultValue: false, description: '构建成功后是否部署')
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: '是否跳过测试;生产构建不建议勾选')
string(name: 'IMAGE_REPOSITORY', defaultValue: '8.163.30.128:10082/java-project/baiyue-firstleg', description: 'Gitea 容器镜像仓库地址')
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 基础镜像;默认使用国内镜像代理')
}
@@ -81,13 +81,18 @@ pipeline {
stage('确定构建范围') {
steps {
script {
String requestedRepository = params.IMAGE_REPOSITORY?.trim()
if (!requestedRepository || requestedRepository == 'registry.example.com/your-group/baiyue-firstleg') {
requestedRepository = '8.163.30.128:10082/java-project/baiyue-firstleg'
echo "检测到旧的镜像仓库占位地址,已自动切换为测试环境 Gitea:${requestedRepository}"
}
if (requestedRepository.contains('://') || !requestedRepository.contains('/')) {
error('IMAGE_REPOSITORY 格式错误:请填写 host[:port]/owner/image,不要包含 http:// 或 https://')
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') {
error('预发布/生产环境必须填写真实的 HTTPS 镜像仓库地址')
}
if (requestedRepository.contains('://') || !requestedRepository.contains('/')) {
error('IMAGE_REPOSITORY 格式错误:请填写 host[:port]/owner/image,不要包含 http:// 或 https://')
}
}
env.RESOLVED_IMAGE_REPOSITORY = requestedRepository
@@ -150,19 +155,27 @@ pipeline {
}
}
stage('构建并推送镜像') {
stage('构建镜像并按环境发布') {
when { expression { env.NO_SERVICE_CHANGE != 'true' } }
steps {
script {
String registryHost = env.RESOLVED_IMAGE_REPOSITORY.tokenize('/')[0]
withCredentials([usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')]) {
sh 'printf %s "$REGISTRY_PASSWORD" | docker login -u "$REGISTRY_USER" --password-stdin "' + registryHost + '"'
env.SELECTED_SERVICES.split(',').each { service ->
String image = "${env.RESOLVED_IMAGE_REPOSITORY}/${service}:${env.IMAGE_TAG}"
sh "docker build -f deploy/docker/Dockerfile --build-arg BASE_IMAGE=${params.DOCKER_BASE_IMAGE} --build-arg JAR_FILE=${serviceJars[service]} -t ${image} ."
sh "docker push ${image}"
env.SELECTED_SERVICES.split(',').each { service ->
String image = "${env.RESOLVED_IMAGE_REPOSITORY}/${service}:${env.IMAGE_TAG}"
sh "docker build -f deploy/docker/Dockerfile --build-arg BASE_IMAGE=${params.DOCKER_BASE_IMAGE} --build-arg JAR_FILE=${serviceJars[service]} -t ${image} ."
}
if (params.ENVIRONMENT == 'test') {
echo "测试镜像已保存在当前 Docker 节点:${env.RESOLVED_IMAGE_REPOSITORY}/*:${env.IMAGE_TAG}"
} else {
String registryHost = env.RESOLVED_IMAGE_REPOSITORY.tokenize('/')[0]
withCredentials([usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')]) {
sh 'printf %s "$REGISTRY_PASSWORD" | docker login -u "$REGISTRY_USER" --password-stdin "' + registryHost + '"'
env.SELECTED_SERVICES.split(',').each { service ->
String image = "${env.RESOLVED_IMAGE_REPOSITORY}/${service}:${env.IMAGE_TAG}"
sh "docker push ${image}"
}
sh "docker logout ${registryHost} || true"
}
sh "docker logout ${registryHost} || true"
}
}
}