name: Pre-build Binary

on:
  push:
    branches: [ master, main ]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Build Frontend
        run: |
          cd web/default
          npm install --legacy-peer-deps
          DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION="$(cat ../../VERSION)" npm run build
          cd ../..
          mkdir -p web/build
          cp -r web/default/build/* web/build/

      - name: Build Backend
        run: |
          go mod download
          go build -trimpath -ldflags "-s -w -X 'github.com/songquanpeng/one-api/common.Version=$(cat VERSION)'" -o one-api

      - name: Upload Binary Artifact
        uses: actions/upload-artifact@v3
        with:
          name: one-api-linux-amd64
          path: one-api

      - name: Upload Frontend Artifact
        uses: actions/upload-artifact@v3
        with:
          name: frontend-build
          path: web/build/