feat(jenkins): support sudo deploy hooks

This commit is contained in:
2026-04-23 04:47:29 +08:00
parent 25e35afe09
commit c79d1b7ded
4 changed files with 71 additions and 15 deletions

View File

@@ -13,6 +13,7 @@ pipeline {
booleanParam(name: 'RUN_NPM_CI', defaultValue: false, description: '构建前是否执行 npm ci')
string(name: 'DEPLOY_JOB_NAME', defaultValue: 'Genarrative-Deploy', description: '部署流水线作业名')
string(name: 'DEPLOY_DIRECTORY', defaultValue: '/var/lib/jenkins/deploy/Genarrative', description: '固定部署目录')
booleanParam(name: 'RUN_DEPLOY_HOOKS_WITH_SUDO', defaultValue: true, description: 'start.sh / stop.sh 是否通过 sudo -n 执行')
}
stages {
@@ -65,6 +66,7 @@ pipeline {
string(name: 'SOURCE_WORKSPACE_ROOT', value: env.WORKSPACE_ROOT),
string(name: 'BUILD_VERSION', value: env.EFFECTIVE_BUILD_VERSION),
string(name: 'DEPLOY_DIRECTORY', value: params.DEPLOY_DIRECTORY),
booleanParam(name: 'RUN_DEPLOY_HOOKS_WITH_SUDO', value: params.RUN_DEPLOY_HOOKS_WITH_SUDO),
string(name: 'EXPECTED_UPSTREAM_JOB', value: env.JOB_NAME),
]
}

View File

@@ -11,6 +11,7 @@ pipeline {
string(name: 'SOURCE_WORKSPACE_ROOT', defaultValue: '', description: '上游源码根目录')
string(name: 'BUILD_VERSION', defaultValue: '', description: '待部署版本号')
string(name: 'DEPLOY_DIRECTORY', defaultValue: '/var/lib/jenkins/deploy/Genarrative', description: '固定部署目录')
booleanParam(name: 'RUN_DEPLOY_HOOKS_WITH_SUDO', defaultValue: true, description: 'start.sh / stop.sh 是否通过 sudo -n 执行')
string(name: 'EXPECTED_UPSTREAM_JOB', defaultValue: '', description: '允许触发本作业的上游作业名')
}
@@ -83,10 +84,15 @@ pipeline {
set -euo pipefail
test -d "build/${params.BUILD_VERSION}"
chmod +x scripts/jenkins-deploy-release.sh
# 只部署上游已构建好的版本目录,避免部署阶段再次构建产生漂移。
./scripts/jenkins-deploy-release.sh \
--source-dir "build/${params.BUILD_VERSION}" \
deploy_args=(
--source-dir "build/${params.BUILD_VERSION}"
--deploy-dir "${params.DEPLOY_DIRECTORY}"
)
if [[ "${params.RUN_DEPLOY_HOOKS_WITH_SUDO}" == "true" ]]; then
deploy_args+=(--hook-with-sudo)
fi
# 只部署上游已构建好的版本目录,避免部署阶段再次构建产生漂移。
./scripts/jenkins-deploy-release.sh "\${deploy_args[@]}"
'
"""
}