2026-05-09 14:53:42 +08:00
pipeline {
agent any
parameters {
string(name: 'BRANCH', defaultValue: 'main', description: 'Git 分支名')
string(name: 'VERSION', defaultValue: '', description: '发布版本号(留空使用 package.json 中的 version) ')
booleanParam(name: 'RUN_TESTS', defaultValue: true, description: '是否运行测试')
booleanParam(name: 'PUBLISH', defaultValue: true, description: '是否发布到 Nexus npm 仓库')
2026-05-14 18:16:11 +08:00
string(name: 'IMAGE_TAG', defaultValue: 'latest', description: 'npm 版本 Tag')
2026-05-09 14:53:42 +08:00
}
environment {
NEXUS_REGISTRY = 'https://nexus.xuqinmin.com/repository/npm-hosted/'
2026-05-14 16:27:49 +08:00
GIT_URL = 'https://xuqinmin.com/xuqmGroup/XuqmGroup-RNSDK.git'
2026-05-09 14:53:42 +08:00
NODE_VERSION = '22'
}
options {
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '20'))
disableConcurrentBuilds()
}
stages {
stage('Checkout') {
steps {
checkout scm
script {
2026-05-14 18:16:11 +08:00
env.GIT_COMMIT_SHORT = bat(script: '@git rev-parse --short HEAD', returnStdout: true).trim()
2026-05-09 14:53:42 +08:00
if (params.VERSION?.trim()) {
2026-05-14 16:27:49 +08:00
bat "npm version ${params.VERSION} --no-git-tag-version"
2026-05-09 14:53:42 +08:00
}
2026-05-14 18:16:11 +08:00
env.PUBLISH_VERSION = bat(script: '@node -p "require(\'./package.json\').version"', returnStdout: true).trim()
2026-05-09 14:53:42 +08:00
}
}
}
stage('Build Info') {
steps {
script {
2026-05-14 16:27:49 +08:00
echo "构建分支: ${params.BRANCH}"
echo "发布版本: ${env.PUBLISH_VERSION}"
echo "Git Commit: ${env.GIT_COMMIT_SHORT}"
2026-05-14 18:16:11 +08:00
echo "npm Tag: ${params.IMAGE_TAG}"
2026-05-09 14:53:42 +08:00
}
}
}
stage('Install Dependencies') {
steps {
2026-05-14 16:27:49 +08:00
bat 'npm ci'
2026-05-09 14:53:42 +08:00
}
}
stage('Type Check') {
steps {
2026-05-14 16:27:49 +08:00
bat 'npm run typecheck:all'
2026-05-09 14:53:42 +08:00
}
}
stage('Tests') {
when { expression { return params.RUN_TESTS } }
steps {
2026-05-14 16:27:49 +08:00
bat 'npm test > test-results.txt 2>&1 || ver>nul'
2026-05-09 14:53:42 +08:00
}
post {
always {
archiveArtifacts artifacts: 'test-results.txt', allowEmptyArchive: true
}
}
}
stage('Publish to Nexus') {
when {
2026-05-14 18:16:11 +08:00
expression { return params.PUBLISH }
2026-05-09 14:53:42 +08:00
}
steps {
withCredentials([usernamePassword(credentialsId: 'nexus-npm-credentials', passwordVariable: 'NPM_PASS', usernameVariable: 'NPM_USER')]) {
2026-05-14 16:27:49 +08:00
bat """
2026-05-14 18:16:11 +08:00
@echo off
2026-05-14 16:27:49 +08:00
npm config set registry %NEXUS_REGISTRY%
2026-05-14 18:16:11 +08:00
for /f "delims=" %%i in ('powershell -noprofile -command "[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('%NPM_USER%:%NPM_PASS%'))"') do set B64_AUTH=%%i
echo //nexus.xuqinmin.com/repository/npm-hosted/:_auth=%B64_AUTH%> .npmrc
npm publish --workspaces --registry %NEXUS_REGISTRY% --tag %IMAGE_TAG%
2026-05-09 14:53:42 +08:00
"""
}
}
}
}
post {
success {
script {
2026-05-14 16:27:49 +08:00
echo "RN SDK v${env.PUBLISH_VERSION} 构建成功 (Commit: ${env.GIT_COMMIT_SHORT})"
2026-05-09 14:53:42 +08:00
if (params.PUBLISH) {
2026-05-14 18:16:11 +08:00
echo "已发布到 Nexus npm 仓库: ${NEXUS_REGISTRY},Tag: ${params.IMAGE_TAG}"
2026-05-09 14:53:42 +08:00
}
}
}
failure {
2026-05-14 18:16:11 +08:00
echo 'RN SDK 构建失败,请检查日志'
2026-05-09 14:53:42 +08:00
}
always {
2026-05-14 16:27:49 +08:00
bat 'npm config delete registry 2>nul || ver>nul'
2026-05-09 14:53:42 +08:00
}
}
}