改改改

This commit is contained in:
2026-01-09 17:52:16 +08:00
parent 72f3644236
commit f71063450b
9 changed files with 537 additions and 383 deletions

View File

@@ -243,40 +243,55 @@ jenkins执行人: ${env.ACTUAL_USER}
when { expression { return params.ROLLBACK_VERSION == false } }
steps {
script {
withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
withCredentials([usernamePassword(
credentialsId: 'fly_gitlab_auth',
usernameVariable: 'GIT_USER',
passwordVariable: 'GIT_PASS'
)]) {
sh """
cd ${WORKSPACE}
git config user.name "jenkins"
git config user.email "jenkins@local"
set -e
cd ${WORKSPACE}
git config user.name "jenkins"
git config user.email "jenkins@local"
git checkout ${params.BRANCH_NAME}
echo "machine 172.24.16.20 login ${GIT_USER} password ${GIT_PASS}" > ~/.netrc
chmod 600 ~/.netrc
MAX_RETRY=3
i=1
while [ \$i -le \$MAX_RETRY ]; do
echo "🔁 尝试第 \$i 次提交..."
git fetch http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
# 检查工作树是否有变化
if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then
echo "检测到更改,正在提交..."
git add ${Deployment_yaml}
git commit -m "更新镜像 \${CHANGE_MSG}"
git commit -m "更新镜像 \${CHANGE_MSG}" || true
else
echo "${Deployment_yaml} 没有变化, 无需commit"
echo "文件无变化,跳过 commit"
fi
# 检查是否需要推送(是否有新的提交)
LOCAL=\$(git rev-parse @)
REMOTE=\$(git rev-parse @{u} 2>/dev/null || true)
BASE=\$(git merge-base @ @{u} 2>/dev/null || true)
if [ "\$LOCAL" = "\$REMOTE" ]; then
echo "已与远程系统同步更新"
elif [ "\$LOCAL" = "\$BASE" ]; then
echo "需要从远程获取数据"
git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
elif [ "\$REMOTE" = "\$BASE" ]; then
echo "将更改推送到远程服务器..."
# 生成临时 .netrc 文件
echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc
chmod 600 ~/.netrc
git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}
else
echo "与远程模式不同,跳过推送操作"
if git rebase FETCH_HEAD; then
if git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME}; then
echo "✅ 推送成功"
exit 0
fi
fi
echo "⚠️ 推送失败,回滚并重试..."
git rebase --abort || true
git reset --hard FETCH_HEAD
i=\$((i+1))
sleep 2
done
echo "❌ 多次尝试失败,可能存在并发冲突"
exit 1
"""
}
}