SNAPSHOT 和正式发布时 MOD_LOG 勾选后 packages/log 不会被跳过 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
256 行
14 KiB
Groovy
256 行
14 KiB
Groovy
pipeline {
|
||
agent any
|
||
|
||
environment {
|
||
NEXUS_REGISTRY = 'https://nexus.xuqinmin.com/repository/npm-hosted/'
|
||
GIT_URL = 'https://xuqinmin.com/xuqmGroup/XuqmGroup-RNSDK.git'
|
||
}
|
||
|
||
options {
|
||
timeout(time: 30, unit: 'MINUTES')
|
||
buildDiscarder(logRotator(numToKeepStr: '20'))
|
||
disableConcurrentBuilds()
|
||
}
|
||
|
||
parameters {
|
||
// ── 发布模式 ─────────────────────────────────────────────────────────
|
||
booleanParam(name: 'SNAPSHOT', defaultValue: true,
|
||
description: '勾选=预发布(alpha tag,版本号追加 -alpha.1,不写回 package.json,不 commit);不勾选=正式 Release(升级版本号并 commit)')
|
||
|
||
// ── 版本升级策略(仅正式 Release 时生效)────────────────────────────
|
||
choice(name: 'VERSION_BUMP', choices: ['patch', 'minor', 'major'],
|
||
description: '版本升级策略: major(1.0.0→2.0.0), minor(1.0.0→1.1.0), patch(1.0.0→1.0.1)(SNAPSHOT 模式下忽略)')
|
||
|
||
// ── 模块选择(勾选即发布)──────────────────────────────────────────
|
||
booleanParam(name: 'MOD_COMMON', defaultValue: false, description: '发布 @xuqm/rn-common')
|
||
booleanParam(name: 'MOD_IM', defaultValue: false, description: '发布 @xuqm/rn-im')
|
||
booleanParam(name: 'MOD_PUSH', defaultValue: false, description: '发布 @xuqm/rn-push')
|
||
booleanParam(name: 'MOD_UPDATE', defaultValue: false, description: '发布 @xuqm/rn-update')
|
||
booleanParam(name: 'MOD_XWEBVIEW', defaultValue: false, description: '发布 @xuqm/rn-xwebview')
|
||
booleanParam(name: 'MOD_LICENSE', defaultValue: false, description: '发布 @xuqm/rn-license')
|
||
booleanParam(name: 'MOD_LOG', defaultValue: false, description: '发布 @xuqm/rn-log')
|
||
}
|
||
|
||
stages {
|
||
|
||
// ── 1. 检出代码 ─────────────────────────────────────────────────────────
|
||
stage('Checkout') {
|
||
steps {
|
||
checkout([
|
||
$class: 'GitSCM',
|
||
branches: [[name: 'main']],
|
||
extensions: [[$class: 'CleanBeforeCheckout']],
|
||
userRemoteConfigs: scm.userRemoteConfigs
|
||
])
|
||
script {
|
||
env.GIT_COMMIT_SHORT = bat(
|
||
script: '@git rev-parse --short HEAD',
|
||
returnStdout: true
|
||
).trim()
|
||
|
||
// ── 根据复选框收集选中的模块 ──────────────────────────────
|
||
def moduleMap = [
|
||
common: ['packages/common', '@xuqm/rn-common'],
|
||
im: ['packages/im', '@xuqm/rn-im'],
|
||
push: ['packages/push', '@xuqm/rn-push'],
|
||
update: ['packages/update', '@xuqm/rn-update'],
|
||
xwebview: ['packages/xwebview', '@xuqm/rn-xwebview'],
|
||
license: ['packages/license', '@xuqm/rn-license'],
|
||
log: ['packages/log', '@xuqm/rn-log'],
|
||
]
|
||
|
||
def moduleChecks = [
|
||
'common': params.MOD_COMMON,
|
||
'im': params.MOD_IM,
|
||
'push': params.MOD_PUSH,
|
||
'update': params.MOD_UPDATE,
|
||
'xwebview': params.MOD_XWEBVIEW,
|
||
'license': params.MOD_LICENSE,
|
||
'log': params.MOD_LOG,
|
||
]
|
||
def requestedModules = moduleChecks.findAll { k, v -> v }.collect { k, v -> k }
|
||
|
||
if (requestedModules.isEmpty()) {
|
||
error "没有选择任何模块,请至少勾选一个"
|
||
}
|
||
|
||
env.SELECTED_MODULES = requestedModules.join(',')
|
||
|
||
// ── 版本号处理 ──────────────────────────────────────────
|
||
env.NPM_TAG = params.SNAPSHOT ? 'alpha' : 'latest'
|
||
for (mod in requestedModules) {
|
||
def (dir, pkg) = moduleMap[mod]
|
||
def currentVer = bat(
|
||
script: "@node -p \"require('./${dir}/package.json').version\"",
|
||
returnStdout: true
|
||
).trim()
|
||
// 去掉已有的预发布后缀,取纯版本号
|
||
def baseVer = currentVer.replaceAll(/-alpha\.\d+$/, '').replaceAll(/-SNAPSHOT$/, '')
|
||
def parts = baseVer.tokenize('.')
|
||
while (parts.size() < 3) { parts.add('0') }
|
||
|
||
def newVer
|
||
if (params.SNAPSHOT) {
|
||
// SNAPSHOT:仅追加 -alpha.1 后缀,不修改 package.json,不 commit
|
||
newVer = baseVer + '-alpha.1'
|
||
echo "SNAPSHOT publish: ${pkg} → ${newVer} (tag: alpha)"
|
||
// 临时写入 package.json 供 npm publish 读取(不 commit)
|
||
bat """node -e "var fs=require('fs'),p='${dir}/package.json',j=JSON.parse(fs.readFileSync(p,'utf8'));j.version='${newVer}';fs.writeFileSync(p,JSON.stringify(j,null,2)+'\\n','utf8');" """
|
||
} else {
|
||
// Release:升级版本号,写回 package.json,稍后 commit
|
||
switch (params.VERSION_BUMP) {
|
||
case 'major': parts[0] = (parts[0].toInteger() + 1).toString(); parts[1] = '0'; parts[2] = '0'; break
|
||
case 'minor': parts[1] = (parts[1].toInteger() + 1).toString(); parts[2] = '0'; break
|
||
default: parts[2] = (parts[2].toInteger() + 1).toString(); break
|
||
}
|
||
newVer = parts.join('.')
|
||
echo "Release: ${pkg}: ${currentVer} → ${newVer}"
|
||
bat """node -e "var fs=require('fs'),p='${dir}/package.json',j=JSON.parse(fs.readFileSync(p,'utf8'));j.version='${newVer}';fs.writeFileSync(p,JSON.stringify(j,null,2)+'\\n','utf8');" """
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── 2. 发布信息 ─────────────────────────────────────────────────────────
|
||
stage('Publish Info') {
|
||
steps {
|
||
script {
|
||
def selected = env.SELECTED_MODULES.split(',')
|
||
def dirMap = [
|
||
common: 'packages/common',
|
||
im: 'packages/im',
|
||
push: 'packages/push',
|
||
update: 'packages/update',
|
||
xwebview: 'packages/xwebview',
|
||
license: 'packages/license',
|
||
log: 'packages/log',
|
||
]
|
||
echo "Git Commit : ${env.GIT_COMMIT_SHORT}"
|
||
echo "Modules to publish (${selected.size()}):"
|
||
for (mod in selected) {
|
||
def dir = dirMap[mod]
|
||
def ver = bat(
|
||
script: "@node -p \"require('./${dir}/package.json').version\"",
|
||
returnStdout: true
|
||
).trim()
|
||
echo " ${mod.padRight(10)} (${dir}) → v${ver}"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── 3. 安装依赖 ─────────────────────────────────────────────────────────
|
||
stage('Install Dependencies') {
|
||
steps {
|
||
withCredentials([usernamePassword(credentialsId: 'NEXUS_CREDS',
|
||
passwordVariable: 'NPM_PASS',
|
||
usernameVariable: 'NPM_USER')]) {
|
||
bat """
|
||
powershell -NonInteractive -Command "\$auth=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(\$env:NPM_USER+':'+\$env:NPM_PASS)); Set-Content .npmrc ('@xuqm:registry=https://nexus.xuqinmin.com/repository/npm-hosted/' + [char]10 + '//nexus.xuqinmin.com/repository/npm-hosted/:_auth=' + \$auth) -Encoding ASCII"
|
||
npm install --legacy-peer-deps
|
||
"""
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── 4. 类型检查 ─────────────────────────────────────────────────────────
|
||
stage('Type Check') {
|
||
steps {
|
||
script {
|
||
// 在每个选中的包目录运行类型检查(允许失败)
|
||
def selected = env.SELECTED_MODULES.split(',')
|
||
def dirMap = [
|
||
common: 'packages/common',
|
||
im: 'packages/im',
|
||
push: 'packages/push',
|
||
update: 'packages/update',
|
||
xwebview: 'packages/xwebview',
|
||
license: 'packages/license',
|
||
log: 'packages/log',
|
||
]
|
||
for (mod in selected) {
|
||
def dir = dirMap[mod]
|
||
if (!fileExists("${dir}/package.json")) {
|
||
echo "Skipping ${mod}: package.json not found (module may not be created yet)"
|
||
continue
|
||
}
|
||
echo "Type-checking ${mod} (${dir})"
|
||
bat "cd ${dir} && npm run typecheck || ver>nul"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── 5. 测试 ────────────────────────────────────────────────────────────
|
||
stage('Tests') {
|
||
steps {
|
||
bat 'npm test > test-results.txt 2>&1 || ver>nul'
|
||
}
|
||
post {
|
||
always {
|
||
archiveArtifacts artifacts: 'test-results.txt', allowEmptyArchive: true
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── 6. 发布到 Nexus ─────────────────────────────────────────────────────
|
||
stage('Publish to Nexus') {
|
||
steps {
|
||
withCredentials([usernamePassword(credentialsId: 'NEXUS_CREDS',
|
||
passwordVariable: 'NPM_PASS',
|
||
usernameVariable: 'NPM_USER')]) {
|
||
script {
|
||
// 写入带认证信息的 .npmrc
|
||
bat """
|
||
powershell -NonInteractive -Command "\$auth=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(\$env:NPM_USER+':'+\$env:NPM_PASS)); Set-Content .npmrc ('@xuqm:registry=https://nexus.xuqinmin.com/repository/npm-hosted/' + [char]10 + '//nexus.xuqinmin.com/repository/npm-hosted/:_auth=' + \$auth) -Encoding ASCII"
|
||
"""
|
||
|
||
def selected = env.SELECTED_MODULES.split(',')
|
||
def dirMap = [
|
||
common: 'packages/common',
|
||
im: 'packages/im',
|
||
push: 'packages/push',
|
||
update: 'packages/update',
|
||
xwebview: 'packages/xwebview',
|
||
license: 'packages/license',
|
||
log: 'packages/log',
|
||
]
|
||
|
||
for (mod in selected) {
|
||
def dir = dirMap[mod]
|
||
if (!fileExists("${dir}/package.json")) {
|
||
echo "Skipping ${mod}: package.json not found"
|
||
continue
|
||
}
|
||
echo "Publishing ${mod} from ${dir} (tag: ${env.NPM_TAG})"
|
||
bat """
|
||
copy .npmrc ${dir}\\.npmrc
|
||
cd ${dir} && npm publish --tag ${env.NPM_TAG} --registry %NEXUS_REGISTRY%
|
||
del ${dir}\\.npmrc 2>nul || exit 0
|
||
"""
|
||
}
|
||
}
|
||
}
|
||
}
|
||
post {
|
||
always {
|
||
bat 'del .npmrc 2>nul || exit 0'
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
post {
|
||
success {
|
||
script {
|
||
def selected = env.SELECTED_MODULES?.split(',') ?: []
|
||
echo "RN SDK 构建成功 — 已发布模块: ${selected.join(', ')} (Commit: ${env.GIT_COMMIT_SHORT})"
|
||
}
|
||
}
|
||
failure {
|
||
echo "RN SDK 构建失败,请检查日志"
|
||
}
|
||
}
|
||
}
|