star-cloud/.gitea/workflows/deploy-prod.yaml
sky121113 1401912059 [FIX] 重構正式環境部署腳本,改用 SSH+tar 方式同步程式碼
1. 棄用 rsync 直接同步方案,改用 tar 打包傳送再遠端解壓,與 demo 部署邏輯一致。
2. 將 docker compose 換成 docker-compose,修正 GCP 主機僅有舊版 v1 CLI 的相容問題。
3. 拆分 Step 步驟結構,對齊 demo workflow 的 Step 0~5 風格。
4. docker exec 加入 -u 1001:1001 確保產出檔案不會被 root 佔用。
2026-05-15 11:12:05 +08:00

79 lines
2.8 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: star-cloud-deploy-production
on:
push:
branches:
- main
jobs:
deploy-production:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.PROD_SSH_PRIVATE_KEY }}
known_hosts: ${{ secrets.PROD_KNOWN_HOSTS }}
- name: Step 0 - Prepare Remote Environment
run: |
# 確保遠端工作目錄存在
ssh twsystem1003@34.84.169.132 "mkdir -p ~/star-cloud/docker/emqx"
# 從遠端載入持久化的 .env若無則複製範本
ssh twsystem1003@34.84.169.132 "test -f ~/star-cloud/.env || cp ~/star-cloud/.env.example ~/star-cloud/.env"
- name: Step 1 - Sync Code to Server
run: |
echo ">>> 打包並傳送程式碼至正式主機..."
tar --exclude='.git' --exclude='node_modules' --exclude='vendor' \
--exclude='storage' --exclude='bootstrap/cache' \
-cf /tmp/deploy.tar . || [ $? -le 1 ]
# 傳送並在遠端解壓
cat /tmp/deploy.tar | ssh twsystem1003@34.84.169.132 \
"tar -xf - -C ~/star-cloud/"
rm -f /tmp/deploy.tar
- name: Step 2 - Build & Deploy Containers
run: |
ssh twsystem1003@34.84.169.132 "cd ~/star-cloud && \
WWWGROUP=1001 WWWUSER=1001 docker-compose up -d --build"
- name: Step 3 - Composer & NPM & Initialization
run: |
ssh twsystem1003@34.84.169.132 "
# 清除 node_modules 確保乾淨安裝
docker exec star-cloud-laravel sh -c \"
chown -R 1001:1001 /var/www/html &&
mkdir -p /.npm && chown -R 1001:1001 /.npm &&
rm -rf /var/www/html/node_modules
\"
docker exec -u 1001:1001 -w /var/www/html star-cloud-laravel sh -c \"
composer install --no-dev --optimize-autoloader --no-interaction &&
npm install &&
npm run build &&
php artisan migrate --force &&
php artisan storage:link &&
php artisan optimize:clear &&
php artisan optimize &&
php artisan view:cache &&
php artisan queue:restart &&
php artisan db:seed --class=RoleSeeder --force &&
php artisan db:seed --class=AdminUserSeeder --force
\"
docker exec star-cloud-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
"
- name: Step 4 - Restart Long-running Workers
run: |
ssh twsystem1003@34.84.169.132 "docker restart star-cloud-mqtt-worker"
- name: Step 5 - Cleanup
run: |
ssh twsystem1003@34.84.169.132 "docker image prune -f"