Files
star-erp/.gitea/workflows/deploy.yaml
sky121113 7c1ee40882
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 54s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
feat: 新增部署維護模式步驟,減少 500 錯誤發生
2026-01-12 10:15:07 +08:00

215 lines
7.6 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Koori-ERP-Deploy-System
on:
push:
branches:
- demo
- main
jobs:
# --- 1. Demo 環境部署 (103 本機) ---
deploy-demo:
if: github.ref == 'refs/heads/demo'
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
github-server-url: http://192.168.0.103:3000
repository: ${{ github.repository }}
- name: Step 1 - Push Code to Demo
run: |
apt-get update && apt-get install -y rsync openssh-client
mkdir -p ~/.ssh
echo "${{ secrets.DEMO_SSH_KEY }}" > ~/.ssh/id_rsa_demo
chmod 600 ~/.ssh/id_rsa_demo
rsync -avz --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='vendor' \
--exclude='storage' \
--exclude='.env' \
-e "ssh -i ~/.ssh/id_rsa_demo -o StrictHostKeyChecking=no" \
./ amba@192.168.0.103:/home/amba/koori-erp/
rm ~/.ssh/id_rsa_demo
# 1.5 啟用維護模式(讓使用者看到維護畫面而非 500
- name: Step 1.5 - Enable Maintenance Mode
uses: appleboy/ssh-action@master
with:
host: 192.168.0.103
port: 22
username: amba
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
cd /home/amba/koori-erp
if [ -f artisan ]; then
docker exec koori-erp-laravel php artisan down --refresh=15 --retry=60 || true
fi
# 2. 啟動或重建容器502 最容易發生在這裡的瞬間)
- name: Step 2 - Container Up & Health Check
uses: appleboy/ssh-action@master
with:
host: 192.168.0.103
port: 22
username: amba
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
cd /home/amba/koori-erp
chown -R 1000:1000 .
WWWGROUP=1000 WWWUSER=1000 docker compose up -d --build --wait
echo "容器狀態:" && docker ps --filter "name=koori-erp-laravel"
- name: Step 3 - Composer & NPM Build
run: |
docker exec -u 1000:1000 -w /var/www/html koori-erp-laravel sh -c "
# 1. 後端依賴 (Demo 環境建議加上 --no-interaction 避免卡住)
composer install --no-dev --optimize-autoloader --no-interaction &&
# 2. 前端編譯
npm install &&
npm run build &&
# 3. Laravel 初始化與優化
php artisan migrate --force &&
php artisan optimize:clear &&
php artisan optimize &&
php artisan view:cache
"
docker exec koori-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
# 4. 關閉維護模式(部署完成,恢復服務)
- name: Step 4 - Disable Maintenance Mode
uses: appleboy/ssh-action@master
with:
host: 192.168.0.103
port: 22
username: amba
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
docker exec koori-erp-laravel php artisan up
# --- 2. 正式環境部署 (erp.koori.tw:2224) ---
deploy-production:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
github-server-url: http://192.168.0.103:3000
repository: ${{ github.repository }}
- name: Step 1 - Push Code to Production
run: |
apt-get update && apt-get install -y rsync openssh-client
mkdir -p ~/.ssh
echo "${{ secrets.PROD_SSH_KEY }}" > ~/.ssh/id_rsa_prod
chmod 600 ~/.ssh/id_rsa_prod
rsync -avz --delete \
--exclude='.git' \
--exclude='.env' \
--exclude='node_modules' \
--exclude='vendor' \
-e "ssh -p 2224 -i ~/.ssh/id_rsa_prod -o StrictHostKeyChecking=no" \
./ root@erp.koori.tw:/var/www/koori-erp-prod/
rm ~/.ssh/id_rsa_prod
# 1.5 啟用維護模式(讓使用者看到維護畫面而非 500
- name: Step 1.5 - Enable Maintenance Mode
uses: appleboy/ssh-action@master
with:
host: erp.koori.tw
port: 2224
username: root
key: ${{ secrets.PROD_SSH_KEY }}
script: |
cd /var/www/koori-erp-prod
if [ -f artisan ]; then
docker exec koori-erp-laravel php artisan down --refresh=15 --retry=60 || true
fi
# 2. 啟動或重建容器502 最容易發生在這裡的瞬間)
- name: Step 2 - Container Up & Health Check
uses: appleboy/ssh-action@master
with:
host: erp.koori.tw
port: 2224
username: root
key: ${{ secrets.PROD_SSH_KEY }}
script: |
cd /var/www/koori-erp-prod
chown -R 1000:1000 .
WWWGROUP=1000 WWWUSER=1000 docker compose up -d --build --wait
echo "容器狀態:" && docker ps --filter "name=koori-erp-laravel"
docker exec -u 1000:1000 -w /var/www/html koori-erp-laravel sh -c "
composer install --no-dev --optimize-autoloader &&
npm install &&
npm run build
php artisan migrate --force &&
php artisan optimize:clear &&
php artisan optimize &&
php artisan view:cache
"
docker exec koori-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
# 3. 關閉維護模式(部署完成,恢復服務)
- name: Step 3 - Disable Maintenance Mode
uses: appleboy/ssh-action@master
with:
host: erp.koori.tw
port: 2224
username: root
key: ${{ secrets.PROD_SSH_KEY }}
script: |
docker exec koori-erp-laravel php artisan up
# 3. 處理後端與前端依賴(這時網站可能因為沒 vendor 呈現 500/502
# - name: Step 3 - Composer & NPM Build
# uses: appleboy/ssh-action@master
# with:
# host: erp.koori.tw
# port: 2224
# username: root
# key: ${{ secrets.PROD_SSH_KEY }}
# script: |
# docker exec -u 1000:1000 -w /var/www/html koori-erp-laravel sh -c "
# composer install --no-dev --optimize-autoloader &&
# npm install &&
# npm run build
# "
# # 4. 處理資料庫與 Laravel 快取
# - name: Step 4 - Database & Optimization
# uses: appleboy/ssh-action@master
# with:
# host: erp.koori.tw
# port: 2224
# username: root
# key: ${{ secrets.PROD_SSH_KEY }}
# script: |
# docker exec -u 1000:1000 -w /var/www/html koori-erp-laravel sh -c "
# php artisan migrate --force &&
# php artisan optimize:clear &&
# php artisan optimize &&
# php artisan view:cache
# "
# # 5. 最後權限修正與重啟(一發入魂,解決 502
# - name: Step 5 - Final Permission & Service Restart
# uses: appleboy/ssh-action@master
# with:
# host: erp.koori.tw
# port: 2224
# username: root
# key: ${{ secrets.PROD_SSH_KEY }}
# script: |
# docker exec koori-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
# echo "正在進行最後重啟以確保服務生效..."
# # docker restart koori-erp-laravel
# echo "部署完成!"