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@v3
        with:
          fetch-depth: 0
      - uses: arduino/setup-task@v1
        with:
          version: 3.x
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/setup-qemu-action@v1
      - uses: actions/cache@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@v3
        with:
          fetch-depth: 0
      - uses: arduino/setup-task@v1
        with:
          version: 3.x
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/setup-qemu-action@v1
      - uses: docker/setup-buildx-action@v1
      - name: setup-buildpacks
        run: |
          sudo add-apt-repository ppa:cncf-buildpacks/pack-cli
          sudo apt-get update
          sudo apt-get --no-install-suggests --no-install-recommends install pack-cli
      - 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@v2
        with:
          go-version: '1.17'
      - uses: actions/cache@v2
        with:
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - uses: actions/cache@v2
        with:
          path: |
            ./dist/*.deb
            ./dist/*.rpm
            ./dist/*.apk
          key: ${{ runner.os }}-go-${{ hashFiles('**/*.go') }}-${{ hashFiles('**/go.sum') }}
      - uses: sigstore/cosign-installer@v2.0.1
      - uses: anchore/sbom-action/download-syft@v0.6.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@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@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: ghcr-login
        if: startsWith(github.ref, 'refs/tags/v')
        uses: docker/login-action@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
