name: Build MEGREZ
on:
  push:
    tags:
      - v*

jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    if: github.repository == 'XShengTech/MEGREZ'
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: 'stable'
          check-latest: true

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20.17.0

      - name: Build Frontend
        run: |
          cd frontend
          npm install
          npm run build
          cd ..

      - name: Build binary
        run: |
          BRANCH=$(git rev-parse --abbrev-ref HEAD)
          VERSION=$(git describe --tags | cut -d'-' -f1)
          COMMIT=$(git rev-parse --short HEAD)
          GO_VERSION=$(go env GOVERSION)
          BUILD_TIME=$(date +%FT%T%z)
          GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X main.BRANCH=${BRANCH} -X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.GoVersion=${GO_VERSION} -X main.BuildTime=${BUILD_TIME}" -trimpath -o megrez . && tar -zcvf megrez-linux-amd64.tar.gz megrez
          GOOS=linux GOARCH=arm64 go build -ldflags="-s -w -X main.BRANCH=${BRANCH} -X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.GoVersion=${GO_VERSION} -X main.BuildTime=${BUILD_TIME}" -trimpath -o megrez . && tar -zcvf megrez-linux-arm64.tar.gz megrez
          GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="-s -w -X main.BRANCH=${BRANCH} -X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.GoVersion=${GO_VERSION} -X main.BuildTime=${BUILD_TIME}" -trimpath -o megrez . && tar -zcvf megrez-linux-armv7.tar.gz megrez
          GOOS=freebsd GOARCH=amd64 go build -ldflags="-s -w -X main.BRANCH=${BRANCH} -X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.GoVersion=${GO_VERSION} -X main.BuildTime=${BUILD_TIME}" -trimpath -o megrez . && tar -zcvf megrez-freebsd-amd64.tar.gz megrez
          GOOS=freebsd GOARCH=arm64 go build -ldflags="-s -w -X main.BRANCH=${BRANCH} -X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.GoVersion=${GO_VERSION} -X main.BuildTime=${BUILD_TIME}" -trimpath -o megrez . && tar -zcvf megrez-freebsd-arm64.tar.gz megrez
          GOOS=freebsd GOARCH=arm GOARM=7 go build -ldflags="-s -w -X main.BRANCH=${BRANCH} -X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.GoVersion=${GO_VERSION} -X main.BuildTime=${BUILD_TIME}" -trimpath -o megrez . && tar -zcvf megrez-freebsd-armv7.tar.gz megrez
      
      - name: Create Release and Upload Release Asset
        uses: softprops/action-gh-release@v1
        with:
          files: |
            megrez-linux-amd64.tar.gz
            megrez-linux-arm64.tar.gz
            megrez-linux-armv7.tar.gz
            megrez-freebsd-amd64.tar.gz
            megrez-freebsd-arm64.tar.gz
            megrez-freebsd-armv7.tar.gz

      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: routers/index/web
          retention-days: 3

  docker-build:
    name: docker-build
    needs: [build]
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - uses: actions/checkout@v3

      - name: Setup timezone
        uses: zcong1993/setup-timezone@master
        with:
          timezone: Asia/Shanghai

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      
      - uses: actions/download-artifact@v4
        with:
          name: dist
          path: routers/index/web

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          context: .
          file: ./Dockerfile  
          push: true
          tags: |
            docker.io/xshengtech/megrez:${{ github.ref_name }}
            docker.io/xshengtech/megrez:latest

      - name: Delete artifact
        uses: geekyeggo/delete-artifact@v5
        with:
          name: |
            dist
