diff --git a/Jenkinsfile b/Jenkinsfile index 15413a2..9e7aa5f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,21 +2,23 @@ pipeline { agent any parameters { - choice(name: 'SERVICE', choices: ['all', 'tenant-service', 'im-service', 'push-service', 'update-service', 'demo-service', 'file-service', 'license-service'], description: '要构建的服务模块(all = 全部)') - choice(name: 'VERSION_STRATEGY', choices: ['patch', 'minor', 'major', 'date'], description: '版本升级策略:patch=修复, minor=新功能, major=大版本, date=日期版本') - string(name: 'CUSTOM_VERSION', defaultValue: '', description: '自定义版本号(留空则自动计算)') - string(name: 'CHANGELOG', defaultValue: '', description: '更新日志(多行文本)') - booleanParam(name: 'DEPLOY', defaultValue: true, description: '构建后是否自动部署到生产服务器') + choice( + name: 'SERVICE', + choices: ['all', 'tenant-service', 'im-service', 'push-service', 'update-service', 'file-service', 'license-service'], + description: '要构建的服务模块(all = 全部)' + ) } environment { - ACR_REGISTRY = 'crpi-n44qjpuucgjt8e8c.cn-beijing.personal.cr.aliyuncs.com' - ACR_NAMESPACE = 'xuqmgroup' - ACR_USERNAME = 'xuqinmin12' - PROD_HOST = '106.54.23.149' - PROD_USER = 'ubuntu' - COMPOSE_FILE = '/opt/xuqm/deploy/compose.production.yaml' + ACR_REGISTRY = 'crpi-n44qjpuucgjt8e8c.cn-beijing.personal.cr.aliyuncs.com' + ACR_NAMESPACE = 'xuqmgroup' + ACR_USERNAME = 'xuqinmin12' + PROD_HOST = '106.54.23.149' + PROD_USER = 'ubuntu' + COMPOSE_FILE = '/opt/xuqm/deploy/compose.production.yaml' + VERSIONS_FILE = '/opt/xuqm/deploy/versions.json' DOCKER_BUILDKIT = '1' + VERSION_FILE = 'VERSION' } options { @@ -37,57 +39,16 @@ pipeline { } } - stage('Calculate Version') { + stage('Resolve Version') { steps { script { - def strategy = params.VERSION_STRATEGY - def custom = params.CUSTOM_VERSION?.trim() - - if (custom) { - env.SERVICE_VERSION = custom - } else if (strategy == 'date') { - def now = new Date() - def datePart = now.format('yyyy.M.d') - def buildNum = env.BUILD_NUMBER - env.SERVICE_VERSION = "${datePart}.${buildNum}" - } else { - // 读取当前版本并按策略递增 - def versionFile = 'VERSION' - def current = '1.0.0' - if (fileExists(versionFile)) { - current = readFile(versionFile).trim() - // 兼容旧格式 2026.05.20-private.3 → 取最后三段 - def parts = current.replaceAll(/[^0-9.]/, '').split('\\.') - if (parts.length >= 3) { - current = "${parts[-3]}.${parts[-2]}.${parts[-1]}" - } - } - def vParts = current.split('\\.') - def major = (vParts[0] ?: '1').toInteger() - def minor = (vParts[1] ?: '0').toInteger() - def patch = (vParts[2] ?: '0').toInteger() - - switch (strategy) { - case 'major': - major++; minor = 0; patch = 0; break - case 'minor': - minor++; patch = 0; break - case 'patch': - default: - patch++; break - } - env.SERVICE_VERSION = "${major}.${minor}.${patch}" - } - - echo "Service version: ${env.SERVICE_VERSION}" - writeFile file: 'SERVICE_VERSION', text: env.SERVICE_VERSION - - // 更新 VERSION 文件(平台版本) - def now = new Date() - def platformVersion = now.format('yyyy.M.d') + ".${env.BUILD_NUMBER}" - env.PLATFORM_VERSION = platformVersion - writeFile file: 'VERSION', text: platformVersion - echo "Platform version: ${platformVersion}" + def current = fileExists(env.VERSION_FILE) ? readFile(env.VERSION_FILE).trim() : '1.0.0' + def parts = current.tokenize('.') + while (parts.size() < 3) parts.add('0') + def newVer = "${parts[0]}.${parts[1]}.${parts[2].toInteger() + 1}" + env.SERVICE_VERSION = newVer + writeFile file: env.VERSION_FILE, text: newVer + echo "Server: ${current} → ${newVer}" } } } @@ -101,18 +62,21 @@ pipeline { : [params.SERVICE] for (svc in services) { - def imageName = "${ACR_REGISTRY}/${ACR_NAMESPACE}/${svc}:${env.PLATFORM_VERSION}" - echo "Building ${svc} version ${env.SERVICE_VERSION}..." - + def base = "${ACR_REGISTRY}/${ACR_NAMESPACE}/${svc}" + def versionedImage = "${base}:${env.SERVICE_VERSION}" + def latestImage = "${base}:latest" + echo "Building ${svc} ${env.SERVICE_VERSION}..." bat """ docker login ${ACR_REGISTRY} -u ${ACR_USERNAME} -p %ACR_PASS% if %errorlevel% neq 0 exit /b 1 - docker pull --platform=linux/amd64 ${imageName} || echo Pull failed, will build fresh - docker build --platform=linux/amd64 --build-arg SERVICE_MODULE=${svc} --build-arg SERVICE_VERSION=${env.SERVICE_VERSION} --build-arg BUILDKIT_INLINE_CACHE=1 --cache-from ${imageName} -t ${imageName} . + docker pull --platform=linux/amd64 ${latestImage} || echo Pull failed, will build fresh + docker build --platform=linux/amd64 --build-arg SERVICE_MODULE=${svc} --build-arg SERVICE_VERSION=${env.SERVICE_VERSION} --build-arg BUILDKIT_INLINE_CACHE=1 --cache-from ${latestImage} -t ${versionedImage} -t ${latestImage} . if %errorlevel% neq 0 exit /b 1 - docker push ${imageName} + docker push ${versionedImage} if %errorlevel% neq 0 exit /b 1 - docker rmi ${imageName} + docker push ${latestImage} + if %errorlevel% neq 0 exit /b 1 + docker rmi ${versionedImage} ${latestImage} exit /b 0 """ } @@ -121,44 +85,7 @@ pipeline { } } - stage('Generate versions.json') { - steps { - script { - def services = [ - 'tenant-service', 'im-service', 'push-service', - 'update-service', 'file-service', 'license-service', - 'nginx', 'tenant-web' - ] - - def servicesMap = [:] - for (svc in services) { - servicesMap[svc] = [ - version: env.SERVICE_VERSION, - changed: params.SERVICE == 'all' || params.SERVICE == svc - ] - } - - def versionsJson = groovy.json.JsonOutput.prettyPrint(groovy.json.JsonOutput.toJson([ - platformVersion: env.PLATFORM_VERSION, - serviceVersion: env.SERVICE_VERSION, - releasedAt: new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC')), - changelog: params.CHANGELOG ?: '', - services: servicesMap - ])) - - writeFile file: 'versions.json', text: versionsJson - echo "versions.json generated:" - echo versionsJson - - // 推送到 Registry(作为 OCI artifact 或上传到 CDN) - // 这里先保存为构建产物,后续可配置推送到 OSS/CDN - archiveArtifacts artifacts: 'versions.json', fingerprint: true - } - } - } - stage('Deploy to Production') { - when { expression { return params.DEPLOY } } steps { lock('prod-deploy') { withCredentials([sshUserPrivateKey(credentialsId: 'PROD_SSH_KEY', keyFileVariable: 'SSH_KEY')]) { @@ -168,40 +95,61 @@ pipeline { : [params.SERVICE] for (svc in services) { - def imageName = "${ACR_REGISTRY}/${ACR_NAMESPACE}/${svc}:${env.PLATFORM_VERSION}" - def remoteCmd = """ - set -e - docker image prune -f 2>/dev/null || true - if ! docker pull ${imageName}; then - echo 'Pull failed, cleaning containerd cache and retrying...' - docker system prune -f - docker pull ${imageName} - fi - docker compose -f ${COMPOSE_FILE} up -d --no-deps --force-recreate ${svc} - docker image prune -f - """.stripIndent() - + def latestImage = "${ACR_REGISTRY}/${ACR_NAMESPACE}/${svc}:latest" echo "Deploying ${svc}..." retry(2) { bat """ - ssh -i "%SSH_KEY%" -o StrictHostKeyChecking=no ${PROD_USER}@${PROD_HOST} "${remoteCmd}" + ssh -i "%SSH_KEY%" -o StrictHostKeyChecking=no ${PROD_USER}@${PROD_HOST} "docker image prune -f 2>/dev/null || true; docker pull ${latestImage} || exit 1; docker compose -f ${COMPOSE_FILE} up -d --no-deps --force-recreate ${svc} || exit 1; docker image prune -f" """ } } - // 上传 versions.json 到服务器 + // Update versions.json on server for deployed services + def deployedServices = params.SERVICE == 'all' + ? ['tenant-service', 'im-service', 'push-service', 'update-service', 'file-service', 'license-service'] + : [params.SERVICE] + def releasedAt = new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC')) + def serviceEntries = deployedServices.collect { svc -> + "d.setdefault('services', {})['${svc}'] = {'version': '${env.SERVICE_VERSION}', 'changed': True}" + }.join('\n') + def updateScript = """\ +import json, os +path = '${env.VERSIONS_FILE}' +d = json.load(open(path)) if os.path.exists(path) else {} +${serviceEntries} +d['serviceVersion'] = '${env.SERVICE_VERSION}' +d['releasedAt'] = '${releasedAt}' +json.dump(d, open(path, 'w'), indent=2, ensure_ascii=False) +print('versions.json updated: ${params.SERVICE} = ${env.SERVICE_VERSION}') +""".stripIndent() + writeFile file: 'update_versions.py', text: updateScript bat """ - scp -i "%SSH_KEY%" -o StrictHostKeyChecking=no versions.json ${PROD_USER}@${PROD_HOST}:/opt/xuqm/deploy/versions.json + scp -i "%SSH_KEY%" -o StrictHostKeyChecking=no update_versions.py ${PROD_USER}@${PROD_HOST}:/tmp/update_versions.py + ssh -i "%SSH_KEY%" -o StrictHostKeyChecking=no ${PROD_USER}@${PROD_HOST} "python3 /tmp/update_versions.py && rm /tmp/update_versions.py" """ } } } } } + + stage('Commit Version') { + steps { + script { + bat """ + git config user.email "jenkins@xuqm.com" + git config user.name "Jenkins CI" + git add ${env.VERSION_FILE} + git diff --cached --quiet || git commit -m "ci: bump server to ${env.SERVICE_VERSION} [skip ci]" + git push origin HEAD:main + """ + } + } + } } post { - success { echo "✅ ${params.SERVICE} v${env.SERVICE_VERSION} (platform ${env.PLATFORM_VERSION}) 构建部署成功" } + success { echo "✅ ${params.SERVICE} v${env.SERVICE_VERSION} 构建部署成功" } failure { echo "❌ 构建失败,请检查日志" } } } diff --git a/VERSION b/VERSION index 93f609d..3eefcb9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2026.05.20-private.3 +1.0.0