name: v3 build

on:
  push:
    tags:
      - "3.*.*"
  workflow_dispatch:

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      # pull code
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      # install latest node
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: latest

      # build sveltekit frontend
      - name: Build frontend
        run: |
          npm --prefix=./frontend i
          UPSNAP_VERSION=${GITHUB_REF##*/} npm --prefix=./frontend run build
          cp -r ./frontend/build/* ./backend/pb_public/

      # setup go
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: ">=1.20"

      # build
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v4
        with:
          distribution: goreleaser
          version: latest
          args: release --clean
          workdir: backend
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  docker:
    needs: goreleaser
    runs-on: ubuntu-latest
    steps:
      # pull code
      - name: Check Out Repo
        uses: actions/checkout@v3

      # image tags
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            ghcr.io/seriousm4x/UpSnap
          tags: |
            type=ref,event=tag
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}
            type=semver,pattern={{major}}.{{minor}}

      # qemu
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      # docker buildx
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v2

      # ghcr.io login
      - name: Log in to the Container registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      # build and push
      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v3
        with:
          context: .
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          builder: ${{ steps.buildx.outputs.name }}
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          build-args: |
            VERSION=${{github.ref_name}}
