161 lines
7.5 KiB
Groovy
161 lines
7.5 KiB
Groovy
def serviceModules = [
|
|
'gateway', 'auth', 'iam', 'firstleg', 'order', 'warehouse', 'file', 'print',
|
|
'integration-service', 'event-bridge-service', 'audit-service'
|
|
]
|
|
|
|
def serviceJars = [
|
|
'gateway': 'gateway/target/gateway-1.0.0-SNAPSHOT.jar',
|
|
'auth': 'auth/target/auth-service-1.0.0-SNAPSHOT.jar',
|
|
'iam': 'iam/target/iam-service-1.0.0-SNAPSHOT.jar',
|
|
'firstleg': 'firstleg/target/firstleg-service-1.0.0-SNAPSHOT.jar',
|
|
'order': 'order/target/order-service-1.0.0-SNAPSHOT.jar',
|
|
'warehouse': 'warehouse/target/warehouse-service-1.0.0-SNAPSHOT.jar',
|
|
'file': 'file/target/file-service-1.0.0-SNAPSHOT.jar',
|
|
'print': 'print/target/print-service-1.0.0-SNAPSHOT.jar',
|
|
'integration-service': 'integration-service/target/integration-service-1.0.0-SNAPSHOT.jar',
|
|
'event-bridge-service': 'event-bridge-service/target/event-bridge-service-1.0.0-SNAPSHOT.jar',
|
|
'audit-service': 'audit-service/target/audit-service-1.0.0-SNAPSHOT.jar'
|
|
]
|
|
|
|
def commonPaths = [
|
|
'pom.xml', 'platform-bom/', 'common-core/', 'common-web/', 'common-security/',
|
|
'common-openapi/', 'common-audit/', 'common-event-contract/', 'Jenkinsfile', 'deploy/'
|
|
]
|
|
|
|
pipeline {
|
|
// 测试阶段使用当前任意可用节点,避免因节点未配置 docker-java17 标签而一直排队。
|
|
// 正式环境配置好专用构建节点后,可恢复为:agent { label 'docker-java17' }
|
|
agent any
|
|
|
|
tools {
|
|
maven 'maven-3.8.8'
|
|
}
|
|
|
|
options {
|
|
disableConcurrentBuilds()
|
|
timestamps()
|
|
buildDiscarder(logRotator(numToKeepStr: '30'))
|
|
}
|
|
|
|
parameters {
|
|
choice(name: 'BUILD_MODE', choices: ['AUTO', 'SELECT', 'ALL'], description: 'AUTO 按 Git 变更识别;SELECT 手工指定;ALL 构建全部服务')
|
|
string(name: 'SERVICE', defaultValue: 'gateway', description: 'SELECT 模式填写服务名,多个服务用英文逗号分隔')
|
|
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: 'registry.example.com/your-group/baiyue-firstleg', description: 'GitLab Container Registry 项目地址')
|
|
}
|
|
|
|
environment {
|
|
REGISTRY_CREDENTIALS = 'gitlab-registry-credentials'
|
|
}
|
|
|
|
stages {
|
|
stage('确定构建范围') {
|
|
steps {
|
|
script {
|
|
List selected = []
|
|
if (params.BUILD_MODE == 'ALL') {
|
|
selected = serviceModules
|
|
} else if (params.BUILD_MODE == 'SELECT') {
|
|
selected = params.SERVICE.split(',').collect { it.trim() }.findAll { it }
|
|
} 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 }
|
|
boolean commonChanged = changed.any { path -> commonPaths.any { prefix -> path == prefix || path.startsWith(prefix) } }
|
|
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(', ')}")
|
|
}
|
|
selected = selected.unique()
|
|
if (!selected) {
|
|
currentBuild.description = '没有需要构建的业务服务'
|
|
env.NO_SERVICE_CHANGE = 'true'
|
|
env.SELECTED_SERVICES = ''
|
|
} else {
|
|
env.NO_SERVICE_CHANGE = 'false'
|
|
env.SELECTED_SERVICES = selected.join(',')
|
|
env.IMAGE_TAG = sh(script: 'git rev-parse --short=12 HEAD', returnStdout: true).trim()
|
|
currentBuild.description = "${env.SELECTED_SERVICES} | ${env.IMAGE_TAG}"
|
|
echo "本次构建服务: ${env.SELECTED_SERVICES}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Maven 测试与打包') {
|
|
when { expression { env.NO_SERVICE_CHANGE != 'true' } }
|
|
steps {
|
|
script {
|
|
String testOption = params.SKIP_TESTS ? '-DskipTests' : ''
|
|
sh "mvn --batch-mode --errors --fail-at-end -pl ${env.SELECTED_SERVICES} -am clean package ${testOption}"
|
|
}
|
|
}
|
|
post {
|
|
always {
|
|
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/TEST-*.xml'
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('构建并推送镜像') {
|
|
when { expression { env.NO_SERVICE_CHANGE != 'true' } }
|
|
steps {
|
|
script {
|
|
String registryHost = params.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 = "${params.IMAGE_REPOSITORY}/${service}:${env.IMAGE_TAG}"
|
|
sh "docker build -f deploy/docker/Dockerfile --build-arg JAR_FILE=${serviceJars[service]} -t ${image} ."
|
|
sh "docker push ${image}"
|
|
}
|
|
sh "docker logout ${registryHost} || true"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('生产部署确认') {
|
|
when {
|
|
allOf {
|
|
expression { params.DEPLOY && params.ENVIRONMENT == 'production' && env.NO_SERVICE_CHANGE != 'true' }
|
|
}
|
|
}
|
|
steps {
|
|
input message: "确认将 ${env.SELECTED_SERVICES} 的镜像 ${env.IMAGE_TAG} 部署到生产环境?", ok: '确认部署'
|
|
}
|
|
}
|
|
|
|
stage('部署') {
|
|
when {
|
|
allOf {
|
|
expression { params.DEPLOY && env.NO_SERVICE_CHANGE != 'true' }
|
|
}
|
|
}
|
|
steps {
|
|
script {
|
|
String credentialId = "firstleg-${params.ENVIRONMENT}-env"
|
|
withCredentials([file(credentialsId: credentialId, variable: 'DEPLOY_ENV_FILE')]) {
|
|
sh "sh deploy/scripts/deploy-services.sh '${params.ENVIRONMENT}' '${params.IMAGE_REPOSITORY}' '${env.IMAGE_TAG}' '${env.SELECTED_SERVICES}' \"\$DEPLOY_ENV_FILE\""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
always { cleanWs(deleteDirs: true, notFailBuild: true) }
|
|
}
|
|
}
|