修改 CI/CD 流程

- 正式环境改为手动触发部署
- feature 分支 push → 测试环境 → 测试 → 合并 main
- main 分支不再自动部署正式环境
- 需要手动在 Gitea Actions 中触发 prod 部署
This commit is contained in:
2026-02-15 19:05:51 +08:00
parent df5373adbc
commit a1598ce243
+22 -37
View File
@@ -3,12 +3,22 @@ name: CI/CD Pipeline
on: on:
push: push:
branches: branches:
- main
- 'feature/*' - 'feature/*'
- 'develop' - 'develop'
pull_request: pull_request:
branches: branches:
- main - main
# 手动触发正式环境部署
workflow_dispatch:
inputs:
environment:
description: 'Deploy to'
required: true
default: 'test'
type: choice
options:
- test
- prod
env: env:
DEPLOY_HOST: micecola.top DEPLOY_HOST: micecola.top
@@ -20,7 +30,7 @@ jobs:
# ==================== 测试环境部署 ==================== # ==================== 测试环境部署 ====================
deploy-test: deploy-test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.ref != 'refs/heads/main' if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'test')
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -41,8 +51,9 @@ jobs:
# 部署代码 # 部署代码
if [ -d .git ]; then if [ -d .git ]; then
git fetch origin git fetch origin
git checkout ${{ github.ref_name }} 2>/dev/null || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }} BRANCH="${{ github.ref_name }}"
git reset --hard origin/${{ github.ref_name }} git checkout $BRANCH 2>/dev/null || git checkout -b $BRANCH origin/$BRANCH
git reset --hard origin/$BRANCH
git clean -fd git clean -fd
else else
git clone -b ${{ github.ref_name }} ssh://gitea-external/starmen1265/kb-web.git . git clone -b ${{ github.ref_name }} ssh://gitea-external/starmen1265/kb-web.git .
@@ -61,7 +72,7 @@ jobs:
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: deploy-test needs: deploy-test
if: github.ref != 'refs/heads/main' if: github.event_name == 'push'
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -128,17 +139,16 @@ jobs:
run: | run: |
git checkout main git checkout main
git pull origin main git pull origin main
git merge --no-ff ${{ github.ref_name }} -m "Merge ${{ github.ref_name }} into main [skip ci]" git merge --no-ff ${{ github.ref_name }} -m "Merge ${{ github.ref_name }} into main"
git push origin main git push origin main
- name: Notify merge - name: Notify merge
run: echo "✅ 已合并到 main" run: echo "✅ 已合并到 main,等待手动部署到正式环境"
# ==================== 正式环境部署 ==================== # ==================== 正式环境部署(仅手动触发) ====================
deploy-prod: deploy-prod:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: auto-merge if: github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'prod'
if: success()
steps: steps:
- name: Backup and deploy - name: Backup and deploy
uses: appleboy/ssh-action@v1 uses: appleboy/ssh-action@v1
@@ -171,30 +181,5 @@ jobs:
chown -R www:www ${{ env.PROD_DIR }} 2>/dev/null || true chown -R www:www ${{ env.PROD_DIR }} 2>/dev/null || true
echo "✅ 正式环境部署完成" echo "✅ 正式环境部署完成"
# ==================== main 分支直接部署 ==================== - name: Notify
direct-deploy: run: echo "🚀 正式环境已更新: http://${{ env.DEPLOY_HOST }}"
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Deploy to production
uses: appleboy/ssh-action@v1
with:
host: ${{ env.DEPLOY_HOST }}
username: ${{ env.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_KEY }}
script: |
cd ${{ env.PROD_DIR }}
# 备份配置
[ -f config.php ] && cp config.php /tmp/config.php.bak
# 部署
git fetch origin
git reset --hard origin/main
git clean -fd
# 恢复配置
[ -f /tmp/config.php.bak ] && cp /tmp/config.php.bak config.php && chown www:www config.php
chown -R www:www ${{ env.PROD_DIR }} 2>/dev/null || true
echo "✅ 正式环境已更新"