name: Release

on:
  push:
    tags:
      - v*

permissions:
  # Allow the workflow to write attestations.
  id-token: write
  attestations: write

jobs:

  release:
    name: Release version
    runs-on: ubuntu-latest
    env:
      GO_VERSION: stable
      CGO_ENABLED: 0

    steps:
      # temporary workaround for an error in free disk space action
      # https://github.com/jlumbroso/free-disk-space/issues/14
      - name: Update Package List and Remove Dotnet
        run: |
          sudo apt-get update
          sudo apt-get remove -y '^dotnet-.*'

      # https://github.com/marketplace/actions/free-disk-space-ubuntu
      - name: Free Disk Space
        uses: jlumbroso/free-disk-space@main
        with:
          # this might remove tools that are actually needed
          tool-cache: false

          # all of these default to true
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: false

      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: actions/setup-go@v6
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Docker Login
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
        run: echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin

      - name: Install snapcraft
        run: sudo snap install snapcraft --classic

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

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

      # https://goreleaser.com/ci/actions/
      - name: Run GoReleaser
        id: goreleaser
        uses: goreleaser/goreleaser-action@v6
        with:
          version: v2.13.0
          args: release -p 1 --clean --timeout=90m
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN_REPO }}
          SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
          AUR_KEY: ${{ secrets.AUR_KEY }}

      - uses: actions/attest-build-provenance@v3
        with:
          subject-checksums: ./dist/lego_${{ fromJSON(steps.goreleaser.outputs.metadata).version }}_checksums.txt
          github-token: ${{ secrets.GH_TOKEN_REPO }}
      - uses: actions/attest-build-provenance@v3
        with:
          subject-checksums: ./dist/digests.txt
          github-token: ${{ secrets.GH_TOKEN_REPO }}
