name: Scheduled Release

on:
  push:
    branches:
      - master
    tags:
      - v*

permissions:
  contents: write

jobs:
  goreleaser:
    name: Release motr, sonrd, and DWN
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          repository: onsonr/sonr
          fetch-depth: 0
          fetch-tags: true

      - name: Get latest tag
        run: |
          # Fetch all tags
          git fetch --tags
          # Get the latest tag
          LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1`)
          echo "Latest Tag: $LATEST_TAG"
          # Checkout the latest tag
          git checkout $LATEST_TAG

      - uses: actions/setup-go@v5
        with:
          go-version: "1.22"
          check-latest: true

      - name: Release
        uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser-pro
          version: latest
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
