Files
YuCheng Hu 332f70b229
DOCS/OpenSearch/pipeline/pr-main This commit looks good
DOCS/OpenSearch/pipeline/head This commit looks good
Delete merged branch of the backport PRs / delete-branch (pull_request) Canceled after 0s
Jekyll Build Verification / check (pull_request) Canceled after 0s
Style check / style-job (pull_request) Canceled after 0s
Backport / Backport (pull_request_target) Canceled after 0s
把项目向 Docs-iSharkFly-Com 对齐。Jenkins 改用 corepack 加 pnpm 安装,并清掉旧的 node_modules 与软链接。
2026-12-04 07:42:42 -05:00

168 lines
7.4 KiB
Groovy
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
pipeline {
agent any
options {
ansiColor('xterm')
disableConcurrentBuilds()
// 文档约 1650 页(中文站点 + /en/ 英文站点各约 820 页),完整构建本地约需 8 分钟
timeout(time: 60, unit: 'MINUTES')
}
tools {
nodejs 'NodeJS 22.14.0'
}
environment {
PRJ_NAME = 'opensearch-isharkfly-com'
// 显式指定 wrangler 配置文件
WRANGLER_CONFIG = 'wrangler.jsonc'
// 发布到线上的分支
RELEASE_BRANCH = 'main'
// Worker Preview 的自定义预览域名
PREVIEW_DOMAIN = 'opensearch.sharkdat.com'
// Cloudflare API Token(Jenkins Secret text 凭据,需要 Workers Scripts:Edit 权限)
CLOUDFLARE_API_TOKEN = credentials('sharkdat-cloudflare-api-token')
// Cloudflare Account ID(Secret text 凭据)。设置后 wrangler 不再调用 /memberships 查询账号,
// 避免 token 缺少 Account 读取权限时报 "You may have incorrect permissions on your API token"
CLOUDFLARE_ACCOUNT_ID = credentials('sharkdat-cloudflare-account-id')
// 不让 wrangler 发送使用统计、不弹交互提示
WRANGLER_SEND_METRICS = 'false'
CI = 'true'
}
stages {
stage('Configure') {
steps {
sh 'git config --global url."https://github.com/".insteadOf git://github.com/'
sh '''
set -eu
corepack enable
node --version
pnpm --version
'''
}
}
stage('Install') {
steps {
// 只安装文档工具链(VitePress + Vue + Wrangler,版本见 package.json / pnpm-lock.yaml),不需要 Ruby / Jekyll
sh '''
set -eu
# 清理旧的 npm 安装方式留下的文件:.vitepress/node_modules 和根目录指向它的软链接
# (Jenkins 工作区不会自动删除 git 忽略的文件)
rm -rf .vitepress/node_modules
if [ -L node_modules ]; then rm -f node_modules; fi
pnpm install --frozen-lockfile
'''
}
}
stage('Build') {
steps {
echo 'Build VitePress docs ...'
// docs:build = generate.mjs + sync-zh.mjs + vitepress build,
// 默认堆内存(及 8 GB)会内存溢出,脚本中已设置 --max-old-space-size=16384
sh 'pnpm run docs:build'
}
}
stage('Deploy - CF') {
steps {
script {
withEnv([
'LANG=C.UTF-8',
'LC_ALL=C.UTF-8',
'NO_COLOR=1',
'TERM=dumb'
]) {
// 部署消息:显示在 Cloudflare 控制台 → Worker → Deployments
def shortSha = env.GIT_COMMIT ? env.GIT_COMMIT.take(7) : 'unknown'
def deployMsg = "Jenkins ${env.JOB_NAME} #${env.BUILD_NUMBER} (${shortSha})"
// Tag 构建:部署到 production 环境(wrangler.jsonc 中的 env.production)
if (env.TAG_NAME?.trim()) {
sh """
pnpm exec wrangler deploy --config ${env.WRANGLER_CONFIG} --env production \
--message '${deployMsg}' \
--tag '${env.TAG_NAME}'
"""
return
}
// main 分支:部署默认 Worker
if (env.BRANCH_NAME == env.RELEASE_BRANCH) {
sh """
pnpm exec wrangler deploy --config ${env.WRANGLER_CONFIG} --env="" \
--message '${deployMsg}' \
--tag '${shortSha}'
"""
return
}
// PR 或其他分支:创建/更新 Worker Preview,不影响线上流量
// 访问地址:https://<previewName>.opensearch.sharkdat.com
// https://<previewName>-opensearch-isharkfly-com.huyuchengus.workers.dev
def branchArg = env.CHANGE_ID
? "pr-${env.CHANGE_ID}"
: env.BRANCH_NAME
// 转成合法的 DNS 标签:小写、只含 a-z0-9-,并限制长度
// (<deployment-id>-<previewName> 也要放进一个 63 字符的子域名标签里)
def previewName = branchArg
.toLowerCase()
.replaceAll(/[^a-z0-9-]+/, '-')
.replaceAll(/^-+|-+$/, '')
previewName = previewName.take(40).replaceAll(/-+$/, '')
// 打印当前 token 所属账号,便于排查 "Worker does not yet exist"(通常是账号不对)
sh 'pnpm exec wrangler whoami'
// Preview 挂在已有 Worker 下;首次构建时先 deploy 一次创建 Worker
def workerExists = sh(
script: "pnpm exec wrangler deployments list --config ${env.WRANGLER_CONFIG} --env=\"\" > /dev/null 2>&1",
returnStatus: true
) == 0
if (!workerExists) {
echo "Worker ${env.PRJ_NAME} 尚不存在,先执行首次 deploy 创建 Worker ..."
sh """
pnpm exec wrangler deploy --config ${env.WRANGLER_CONFIG} --env="" \
--message 'Initial deploy: ${deployMsg}'
"""
}
// 用新版 Previews(需要 wrangler >= 4.135.0),才会出现在自定义预览域名下。
// Jenkins 检出的是 detached HEAD,无法自动识别分支,所以必须显式传 --name。
sh """
pnpm exec wrangler preview --config ${env.WRANGLER_CONFIG} --env="" \
--name '${previewName}' \
--message '${deployMsg}' \
--tag '${shortSha}'
"""
echo "Preview URL: https://${previewName}.${env.PREVIEW_DOMAIN}"
}
}
}
}
}
post {
always {
script {
env.BUILD_STARTED_AT = new Date(currentBuild.startTimeInMillis)
.format('yyyy-MM-dd HH:mm:ss z', TimeZone.getTimeZone('America/New_York'))
env.BUILD_DURATION_TEXT = currentBuild.durationString
emailext (
mimeType: 'text/html; charset=UTF-8',
subject: "Build ${currentBuild.currentResult}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: '${DEFAULT_CONTENT}',
from: '[email protected]',
to: '[email protected], [email protected]'
)
}
}
}
}