name: build

on:
  push:
    branches:
      - 'main'
    tags:
      - 'v*'
  pull_request:

permissions:
  contents: write
  id-token: write
  packages: write

jobs:
  goreleaser-check-pkgs:
    runs-on: ubuntu-latest
    env:
      DOCKER_CLI_EXPERIMENTAL: "enabled"
    needs:
      - goreleaser
    if: github.ref == 'refs/heads/main'
    strategy:
      matrix:
        format: [ deb, rpm, apk ]
    steps:
      - uses: actions/checkout@b0e28b5ac45a892f91e7d036f8200cf5ed489415 # v3
        with:
          fetch-depth: 0
      - uses: arduino/setup-task@accf38bba955639d21816bb68775e5c48c482182 # v1
        with:
          version: 3.x
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/setup-qemu-action@27d0a4f181a40b142cce983c5393082c365d1480 # v1
      - uses: actions/cache@661fd3eb7f2f20d8c7c84bc2b0509efd7a826628 # v2
        with:
          path: |
            ./dist/*.deb
            ./dist/*.rpm
            ./dist/*.apk
          key: ${{ runner.os }}-go-${{ hashFiles('**/*.go') }}-${{ hashFiles('**/go.sum') }}
      - run: task goreleaser:test:${{ matrix.format }}
  goreleaser:
    runs-on: ubuntu-latest
    env:
      DOCKER_CLI_EXPERIMENTAL: "enabled"
    steps:
      - uses: actions/checkout@b0e28b5ac45a892f91e7d036f8200cf5ed489415 # v3
        with:
          fetch-depth: 0
      - uses: arduino/setup-task@accf38bba955639d21816bb68775e5c48c482182 # v1
        with:
          version: 3.x
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/setup-qemu-action@27d0a4f181a40b142cce983c5393082c365d1480 # v1
      - uses: docker/setup-buildx-action@94ab11c41e45d028884a99163086648e898eed25 # v1
      - name: setup-snapcraft
        # FIXME: the mkdirs are a hack for https://github.com/goreleaser/goreleaser/issues/1715
        run: |
          sudo apt-get update
          sudo apt-get -yq --no-install-suggests --no-install-recommends install snapcraft
          mkdir -p $HOME/.cache/snapcraft/download
          mkdir -p $HOME/.cache/snapcraft/stage-packages
      - uses: actions/setup-go@f6164bd8c8acb4a71fb2791a8b6c4024ff038dab # v2
        with:
          go-version: '1.18'
      - uses: actions/cache@661fd3eb7f2f20d8c7c84bc2b0509efd7a826628 # v2
        with:
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - uses: actions/cache@661fd3eb7f2f20d8c7c84bc2b0509efd7a826628 # v2
        with:
          path: |
            ./dist/*.deb
            ./dist/*.rpm
            ./dist/*.apk
          key: ${{ runner.os }}-go-${{ hashFiles('**/*.go') }}-${{ hashFiles('**/go.sum') }}
      - uses: sigstore/cosign-installer@ab3bb6a537fb08a1b78f7d1175fff799ca6850a3 # v2.1.0
      - uses: anchore/sbom-action/download-syft@ce4a7cf05d7b684693d7b6bba97bfbee56806edb # v0.7.0
      - name: setup-validate-krew-manifest
        # TODO: replace this once https://github.com/kubernetes-sigs/krew/pull/736 is merged
        run: go install github.com/caarlos0/krew/cmd/validate-krew-manifest@fork
      - run: task ci
      - uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2
        with:
          file: ./coverage.txt
      - run: ./goreleaser check
      - run: git diff
      - name: dockerhub-login
        if: startsWith(github.ref, 'refs/tags/v')
        uses: docker/login-action@dd4fa0671be5250ee6f50aedf4cb05514abda2c7 # v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: ghcr-login
        if: startsWith(github.ref, 'refs/tags/v')
        uses: docker/login-action@dd4fa0671be5250ee6f50aedf4cb05514abda2c7 # v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: snapcraft-login
        if: startsWith(github.ref, 'refs/tags/v')
        run: snapcraft login --with <(echo "${{ secrets.SNAPCRAFT_LOGIN }}")
      - name: goreleaser-release
        if: startsWith(github.ref, 'refs/tags/v') ||  github.ref == 'refs/heads/main'
        env:
          GITHUB_TOKEN: ${{ secrets.GH_PAT }}
          TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }}
          TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }}
          TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
          TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
          COSIGN_PWD: ${{ secrets.COSIGN_PWD }}
          FURY_TOKEN: ${{ secrets.FURY_TOKEN }}
          DISCORD_WEBHOOK_ID: ${{ secrets.DISCORD_WEBHOOK_ID }}
          DISCORD_WEBHOOK_TOKEN: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
          AUR_KEY: ${{ secrets.AUR_KEY }}
        run: task goreleaser
