ci: 发布阶段增加权限自愈与自动备份
This commit is contained in:
1 parent
f52b91e06c
commit
9fc30aaadf
2 files changed
+49
-12
No files matched your search
Vendored
+15
-4
@@ -4,7 +4,7 @@
|
||||
//
|
||||
// 前置条件(见 README「自动部署(Jenkins)」):
|
||||
// 1. Jenkins 用户能用 docker(通常在 docker 组里)。
|
||||
// 2. jenkins 用户对 DEPLOY_DIR 有写权限。
|
||||
// 2. 服务器预置 /usr/local/bin/index-deploy-prepare 及其 sudo 免密规则(权限自愈)。
|
||||
// 3. Job 的 SCM 指向本仓库,并开启 Gitea push 触发 + 配置 Gitea webhook。
|
||||
// 说明:构建跑在 node:22-alpine 容器里,宿主机 Jenkins 不需要安装 Node。
|
||||
// =============================================================================
|
||||
@@ -55,10 +55,21 @@ pipeline {
|
||||
steps {
|
||||
sh '''
|
||||
set -euo pipefail
|
||||
mkdir -p "$DEPLOY_DIR"
|
||||
# 清空目标目录内容(等价于 rsync --delete),保持站点干净
|
||||
|
||||
# 1) 权限自愈:目录属主若被 1Panel 改回 root 等情况,自动修正为 jenkins
|
||||
# (服务器已预置 sudo 规则,仅允许免密执行这一个脚本)
|
||||
sudo -n /usr/local/bin/index-deploy-prepare
|
||||
|
||||
# 2) 备份当前线上版本(保留最近 10 份,便于回滚)
|
||||
BK_DIR=/var/backups/index-site
|
||||
mkdir -p "$BK_DIR"
|
||||
if [ -n "$(ls -A "$DEPLOY_DIR" 2>/dev/null)" ]; then
|
||||
tar czf "$BK_DIR/index-$(date +%Y%m%d-%H%M%S)-build${BUILD_NUMBER}.tar.gz" -C "$DEPLOY_DIR" .
|
||||
ls -1t "$BK_DIR"/index-*.tar.gz | tail -n +11 | xargs -r rm -f
|
||||
fi
|
||||
|
||||
# 3) 清空 + 发布(等价于 rsync --delete)
|
||||
find "$DEPLOY_DIR" -mindepth 1 -delete
|
||||
# 复制构建产物(-a 保留属性);先移除 extra/ 带来的占位文件
|
||||
find .deploy -name .gitkeep -delete
|
||||
cp -a .deploy/. "$DEPLOY_DIR"/
|
||||
echo "✅ Deployed to $DEPLOY_DIR"
|
||||
|
||||
@@ -248,16 +248,39 @@ Node,也不依赖 rsync**,只要能跑 Docker 即可。整条流水线定义
|
||||
sudo usermod -aG docker jenkins # 之后重启 jenkins 使组生效
|
||||
```
|
||||
|
||||
2. 让 Jenkins 能写站点目录(1Panel 的站点目录通常不属于 `jenkins` 用户):
|
||||
2. 预置「权限自愈」脚本与免密 sudo 规则(`Deploy` 阶段会 `sudo` 调它,把被 1Panel
|
||||
改回 root 的站点目录重新修正为 `jenkins`):
|
||||
|
||||
```bash
|
||||
# 方式 A(推荐,权限最小):把 jenkins 加入站点目录所属组
|
||||
ls -ld /opt/1panel/www/sites/xiaozhou233.cn/index # 查看所属组
|
||||
sudo usermod -aG <该组> jenkins
|
||||
# 重新登录/重启 jenkins 使组生效
|
||||
sudo tee /usr/local/bin/index-deploy-prepare >/dev/null <<'EOF'
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
DIR=/opt/1panel/www/sites/xiaozhou233.cn/index
|
||||
mkdir -p "$DIR"
|
||||
chown -R jenkins:jenkins "$DIR"
|
||||
chmod u+rwx "$DIR"
|
||||
EOF
|
||||
sudo chmod 755 /usr/local/bin/index-deploy-prepare
|
||||
|
||||
# 方式 B(简单):直接把目录属主改成 jenkins
|
||||
sudo chown -R jenkins:jenkins /opt/1panel/www/sites/xiaozhou233.cn/index
|
||||
# 仅允许 jenkins 免密执行这一个脚本
|
||||
echo 'jenkins ALL=(root) NOPASSWD: /usr/local/bin/index-deploy-prepare' \
|
||||
| sudo tee /etc/sudoers.d/index-deploy >/dev/null
|
||||
sudo chmod 440 /etc/sudoers.d/index-deploy
|
||||
sudo visudo -cf /etc/sudoers.d/index-deploy # 看到 parsed OK 即通过
|
||||
```
|
||||
|
||||
3. 建备份目录(`Deploy` 阶段每次发布前会在此留一份上一版,保留最近 10 份,便于回滚):
|
||||
|
||||
```bash
|
||||
sudo mkdir -p /var/backups/index-site
|
||||
sudo chown jenkins:jenkins /var/backups/index-site
|
||||
```
|
||||
|
||||
4. (可选)安装 `rsync`:流水线发布用 `cp`/`find`,并不依赖它;装了只是方便手工
|
||||
同步 / 回滚。
|
||||
|
||||
```bash
|
||||
sudo apt-get install -y rsync
|
||||
```
|
||||
|
||||
### 二、Jenkins 配置
|
||||
@@ -280,7 +303,10 @@ Node,也不依赖 rsync**,只要能跑 Docker 即可。整条流水线定义
|
||||
### 四、常见问题
|
||||
|
||||
- **docker: permission denied**:Jenkins 用户不在 docker 组,见「一、服务器准备」第 1 步。
|
||||
- **cp/find: Permission denied**:见「一、服务器准备」第 2 步处理目录权限。
|
||||
- **cp/find: Permission denied**:`Deploy` 会先跑权限自愈脚本;若仍失败,确认「一、服务器准备」第 2 步的 sudo 规则已生效。
|
||||
- **回滚到上一版**:每次发布前会把上一版备份到 `/var/backups/index-site/`(最近 10 份),
|
||||
在站点目录解压覆盖即可,例如
|
||||
`tar xzf /var/backups/index-site/index-<时间>.tar.gz -C /opt/1panel/www/sites/xiaozhou233.cn/index`。
|
||||
- **想回滚**:`git revert <坏提交>` 后 push,Jenkins 重新构建;或改回上一个提交。
|
||||
- **发布时会先清空站点目录**:`Deploy` 阶段用 `find … -delete` 清空再覆盖(与
|
||||
`rsync --delete` 等效)。若 1Panel 在该目录放了额外文件(如 `.user.ini`),复制后补回,
|
||||
|
||||
Reference in new issue
Block a user