From 06b748e1af0c78fc2acc83b7d8676fbbff71148e Mon Sep 17 00:00:00 2001 From: kdletters Date: Fri, 1 May 2026 01:24:55 +0800 Subject: [PATCH] fix jenkins commit hash env fallback --- jenkins/Jenkinsfile.build-and-deploy | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/jenkins/Jenkinsfile.build-and-deploy b/jenkins/Jenkinsfile.build-and-deploy index 02ebd0fc..b89e6587 100644 --- a/jenkins/Jenkinsfile.build-and-deploy +++ b/jenkins/Jenkinsfile.build-and-deploy @@ -42,7 +42,7 @@ pipeline { if (commitHash && !(commitHash ==~ /^[0-9a-fA-F]{7,40}$/)) { error('COMMIT_HASH 只能填写 7 到 40 位十六进制 Git commit hash,当前值: ' + commitHash) } - env.REQUESTED_COMMIT_HASH = commitHash ?: '' + env.COMMIT_HASH = commitHash ?: '' def database = params.DATABASE?.trim() if (!database) { error('DATABASE 不能为空。') @@ -108,14 +108,15 @@ pipeline { sh ''' bash -lc ' set -euo pipefail - if [[ -n "${REQUESTED_COMMIT_HASH}" ]]; then + requested_commit="${COMMIT_HASH:-}" + if [[ -n "${requested_commit}" ]]; then # Jenkins 先按 SCM 配置完成 checkout;如指定 commit,再拉取远端引用并切到该提交构建。 git fetch --tags --prune origin "+refs/heads/*:refs/remotes/origin/*" || git fetch --all --tags --prune if [[ "$(git rev-parse --is-shallow-repository 2>/dev/null || echo false)" == "true" ]]; then git fetch --unshallow --tags || true fi - git cat-file -e "${REQUESTED_COMMIT_HASH}^{commit}" - resolved_commit="$(git rev-parse "${REQUESTED_COMMIT_HASH}^{commit}")" + git cat-file -e "${requested_commit}^{commit}" + resolved_commit="$(git rev-parse "${requested_commit}^{commit}")" git checkout --detach "${resolved_commit}" echo "[build-and-deploy] 使用指定 commit 构建: ${resolved_commit}" else