name: Release
on:
  push:
    tags:
      - 'v*'

jobs:
  build-docker:
    runs-on: ubuntu-latest
    env:
      IMAGE_NAME: ppanel-server

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract version from git tag
        id: version
        run: echo "VERSION=$(git describe --tags --abbrev=0 | sed 's/^v//')" >> $GITHUB_ENV

      - name: Get short SHA
        id: sha
        run: echo "GIT_SHA=${GITHUB_SHA::8}" >> $GITHUB_ENV

      - name: Set BUILD_TIME env
        run: echo BUILD_TIME=$(date --iso-8601=seconds) >> ${GITHUB_ENV}


      - name: Build and push Docker image for main release
        if: "!contains(github.ref_name, 'beta')"
        uses: docker/build-push-action@v6
        with:
          context: .
          file: Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          build-args: |
            VERSION=${{ env.VERSION }}
          tags: |
            ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:latest
            ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}-${{ env.GIT_SHA }}

      - name: Build and push Docker image for beta release
        if: contains(github.ref_name, 'beta')
        uses: docker/build-push-action@v6
        with:
          context: .
          file: Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          build-args: |
            VERSION=${{ env.VERSION }}
          tags: |
            ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:beta
            ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}-${{ env.GIT_SHA }}

  release-notes:
    runs-on: ubuntu-latest
    needs: build-docker
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.21'

      - name: Install GoReleaser
        run: |
          go install github.com/goreleaser/goreleaser/v2@latest

      - name: Run GoReleaser
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        run: |
          goreleaser check
          goreleaser release --clean

  releases-matrix:
    name: Release ppanel-server binary
    runs-on: ubuntu-latest
    needs: release-notes  # wait for release-notes job to finish
    strategy:
      matrix:
        # build and publish in parallel: linux/386, linux/amd64, linux/arm64,
        # windows/386, windows/amd64, windows/arm64, darwin/amd64, darwin/arm64
        goos: [ linux, windows, darwin ]
        goarch: [ '386', amd64, arm64 ]
        exclude:
          - goarch: '386'
            goos: darwin


    steps:
      - uses: actions/checkout@v2
      - name: Extract version from git tag
        id: version
        run: echo "VERSION=$(git describe --tags --abbrev=0 | sed 's/^v//')" >> $GITHUB_ENV

      - name: Set BUILD_TIME env
        run: echo BUILD_TIME=$(date --iso-8601=seconds) >> ${GITHUB_ENV}

      - uses: actions/checkout@v4
      - uses: wangyoucao577/go-release-action@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          goos: ${{ matrix.goos }}
          goarch: ${{ matrix.goarch }}
          asset_name: "ppanel-server-${{ matrix.goos }}-${{ matrix.goarch }}"
          goversion: "https://dl.google.com/go/go1.23.3.linux-amd64.tar.gz"
          project_path: "."
          binary_name: "ppanel-server"
          extra_files: LICENSE etc
          ldflags: -X "github.com/perfect-panel/server/pkg/constant.Version=${{env.VERSION}}" -X "github.com/perfect-panel/server/pkg/constant.BuildTime=${{env.BUILD_TIME}}"
