修改 CI/CD 流程
- 正式环境改为手动触发部署 - feature 分支 push → 测试环境 → 测试 → 合并 main - main 分支不再自动部署正式环境 - 需要手动在 Gitea Actions 中触发 prod 部署
This commit is contained in:
+22
-37
@@ -3,12 +3,22 @@ name: CI/CD Pipeline
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- 'feature/*'
|
||||
- 'develop'
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
# 手动触发正式环境部署
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
environment:
|
||||
description: 'Deploy to'
|
||||
required: true
|
||||
default: 'test'
|
||||
type: choice
|
||||
options:
|
||||
- test
|
||||
- prod
|
||||
|
||||
env:
|
||||
DEPLOY_HOST: micecola.top
|
||||
@@ -20,7 +30,7 @@ jobs:
|
||||
# ==================== 测试环境部署 ====================
|
||||
deploy-test:
|
||||
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:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
@@ -41,8 +51,9 @@ jobs:
|
||||
# 部署代码
|
||||
if [ -d .git ]; then
|
||||
git fetch origin
|
||||
git checkout ${{ github.ref_name }} 2>/dev/null || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
|
||||
git reset --hard origin/${{ github.ref_name }}
|
||||
BRANCH="${{ github.ref_name }}"
|
||||
git checkout $BRANCH 2>/dev/null || git checkout -b $BRANCH origin/$BRANCH
|
||||
git reset --hard origin/$BRANCH
|
||||
git clean -fd
|
||||
else
|
||||
git clone -b ${{ github.ref_name }} ssh://gitea-external/starmen1265/kb-web.git .
|
||||
@@ -61,7 +72,7 @@ jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
needs: deploy-test
|
||||
if: github.ref != 'refs/heads/main'
|
||||
if: github.event_name == 'push'
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
@@ -128,17 +139,16 @@ jobs:
|
||||
run: |
|
||||
git checkout 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
|
||||
|
||||
- name: Notify merge
|
||||
run: echo "✅ 已合并到 main"
|
||||
run: echo "✅ 已合并到 main,等待手动部署到正式环境"
|
||||
|
||||
# ==================== 正式环境部署 ====================
|
||||
# ==================== 正式环境部署(仅手动触发) ====================
|
||||
deploy-prod:
|
||||
runs-on: ubuntu-latest
|
||||
needs: auto-merge
|
||||
if: success()
|
||||
if: github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'prod'
|
||||
steps:
|
||||
- name: Backup and deploy
|
||||
uses: appleboy/ssh-action@v1
|
||||
@@ -171,30 +181,5 @@ jobs:
|
||||
chown -R www:www ${{ env.PROD_DIR }} 2>/dev/null || true
|
||||
echo "✅ 正式环境部署完成"
|
||||
|
||||
# ==================== main 分支直接部署 ====================
|
||||
direct-deploy:
|
||||
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 "✅ 正式环境已更新"
|
||||
- name: Notify
|
||||
run: echo "🚀 正式环境已更新: http://${{ env.DEPLOY_HOST }}"
|
||||
|
||||
Reference in New Issue
Block a user