Merge pull request #58 from DamnDamnDamnM3/feature/export-reports #13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build & Deploy to VPS | |
| on: | |
| push: | |
| branches: [main] | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # ── 1. 取得原始碼 ──────────────────────────────────────────────── | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # ── 2. 建置後端 JAR ────────────────────────────────────────────── | |
| # 前端改由 VPS 直接跑 Vite dev server,不再打包進 JAR | |
| - name: Setup Java 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| cache: 'maven' | |
| - name: Build backend JAR (skip tests) | |
| working-directory: backend | |
| run: mvn package -DskipTests --batch-mode --no-transfer-progress | |
| - name: Rename JAR to fixed filename | |
| run: mv backend/target/vehicle-management-*.jar backend/target/app.jar | |
| # ── 3. 上傳 JAR 到 VPS ─────────────────────────────────────────── | |
| - name: Upload JAR to VPS | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.HOST_ADDRESS }} | |
| port: ${{ secrets.HOST_PORT }} | |
| username: ${{ secrets.ACCOUNT_NAME }} | |
| password: ${{ secrets.ACCOUNT_PASSWORD }} | |
| source: backend/target/app.jar | |
| target: ~/deploy/ | |
| strip_components: 2 # backend/target/app.jar → ~/deploy/app.jar | |
| # ── 4. SSH:更新前端原始碼並重啟兩個服務 ───────────────────────── | |
| - name: Deploy & restart via PM2 | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.HOST_ADDRESS }} | |
| port: ${{ secrets.HOST_PORT }} | |
| username: ${{ secrets.ACCOUNT_NAME }} | |
| password: ${{ secrets.ACCOUNT_PASSWORD }} | |
| script: | | |
| set -e | |
| # ── npm global 路徑(無 sudo) ──────────────────────────── | |
| export NPM_CONFIG_PREFIX="$HOME/.npm-global" | |
| export PATH="$HOME/.npm-global/bin:$PATH" | |
| if ! grep -q 'npm-global' ~/.profile 2>/dev/null; then | |
| echo 'export NPM_CONFIG_PREFIX="$HOME/.npm-global"' >> ~/.profile | |
| echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.profile | |
| fi | |
| # ── 確保 PM2 已安裝 ────────────────────────────────────── | |
| if ! command -v pm2 &> /dev/null; then | |
| npm install -g pm2 | |
| fi | |
| # ── 更新前端原始碼(git pull 或首次 clone)─────────────── | |
| REPO_DIR="$HOME/repo" | |
| if [ -d "$REPO_DIR/.git" ]; then | |
| git -C "$REPO_DIR" pull origin main | |
| else | |
| git clone https://github.com/DamnDamnDamnM3/114-2_FCU_Framework-Design-Final.git "$REPO_DIR" | |
| fi | |
| # 安裝前端相依套件 | |
| cd "$REPO_DIR/frontend" | |
| npm install | |
| # ── 移動新 JAR ─────────────────────────────────────────── | |
| mkdir -p ~/app | |
| mv ~/deploy/app.jar ~/app/app.jar | |
| # ── 找出 java 完整路徑(PM2 不讀 ~/.profile)──────────── | |
| JAVA_BIN=$(which java 2>/dev/null \ | |
| || find /usr /opt -name java -type f 2>/dev/null | grep '/bin/java' | head -1) | |
| if [ -z "$JAVA_BIN" ]; then | |
| echo "ERROR: java not found on VPS" && exit 1 | |
| fi | |
| echo "Java: $JAVA_BIN ($($JAVA_BIN -version 2>&1 | head -1))" | |
| # ── 重啟後端(port 8080,dev 模式 H2 DB)──────────────── | |
| pm2 delete vehicle-backend 2>/dev/null || true | |
| pm2 start "$JAVA_BIN -jar $HOME/app/app.jar --spring.profiles.active=dev" \ | |
| --name vehicle-backend \ | |
| --log $HOME/app/backend.log \ | |
| --time | |
| # ── 重啟前端(port 5173,Vite dev server)──────────────── | |
| pm2 delete vehicle-frontend 2>/dev/null || true | |
| pm2 start "npm run dev" \ | |
| --name vehicle-frontend \ | |
| --cwd "$REPO_DIR/frontend" \ | |
| --log $HOME/app/frontend.log \ | |
| --time | |
| pm2 save | |
| pm2 list |