name: release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write
  packages: write

jobs:
  release:
    runs-on: buildjet-32vcpu-ubuntu-2204
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          cache: false
          go-version: "1.20"
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v2
        with:
          buildkitd-flags: --debug
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Install Cosign
        uses: sigstore/cosign-installer@main
        with:
          cosign-release: "v1.4.1"
      - name: Setup Cosign
        run: |
          echo "${COSIGN_KEY}" > "$GITHUB_WORKSPACE/cosign.key"
        env:
          COSIGN_KEY: ${{ secrets.COSIGN_KEY }}
      - run: make validate-code
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v4
        with:
          distribution: goreleaser
          version: v1.15.2
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_PROJECT_TOKEN: ${{ secrets.GH_PROJECT_TOKEN }}
          COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
          AC_IDENTITY: ${{ secrets.AC_IDENTITY }}
          AC_USERNAME: ${{ secrets.AC_USERNAME }}
          AC_PASSWORD: ${{ secrets.AC_PASSWORD }}
          AC_P12: ${{ secrets.AC_P12 }}
          AC_P12_PASSWORD: ${{ secrets.AC_P12_PASSWORD }}
          AC_ISSUER_ID: ${{ secrets.AC_ISSUER_ID }}
          AC_KEY_ID: ${{ secrets.AC_KEY_ID }}
          AC_PRIVATE_KEY: ${{ secrets.AC_PRIVATE_KEY }}
          GORELEASER_CURRENT_TAG: ${{ github.ref_name }}
      - name: report failure to slack
        if: failure()
        id: slack-failure
        uses: slackapi/slack-github-action@v1.23.0
        with:
          channel-id: "${{ secrets.SLACK_BOT_FAILURE_CHANNEL }}"
          slack-message: "❌ Release failed: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
      - name: report success to slack
        id: slack-success
        uses: slackapi/slack-github-action@v1.23.0
        with:
          channel-id: "${{ secrets.USERS_SLACK_BOT_RELEASE_CHANNEL }}"
          slack-message: " ✅ Release ${{ github.ref_name }} is now available: ${{ github.server_url }}/${{ github.repository }}/releases/${{ github.ref_name }}"
        env:
          SLACK_BOT_TOKEN: "${{ secrets.USERS_SLACK_BOT_TOKEN }}"
