name: goreleaser

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+' # Only build tag with semantic versioning format
      - 'v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+'

permissions:
  contents: write

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - run: git fetch --force --tags
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.20'
          check-latest: true
          cache: true
      - name: Set release tag env variable
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      - name: Set the current version as env variable
        run: |
          make build
          echo "CURRENT_VERSION=$(./dblab version)" >> $GITHUB_ENV
      - name: Test the env variables
        run: |
          echo RELEASE_VERSION
          echo ${{ env.RELEASE_VERSION }}
          echo CURRENT_VERSION
          echo ${{ env.CURRENT_VERSION }}
      - name: Run GoReleaser
        if: env.RELEASE_VERSION == env.CURRENT_VERSION
        uses: goreleaser/goreleaser-action@v3
        with:
          version: latest
          args: release -f .goreleaser.yaml --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }}
