---
name: "Release a tag"
on:
  push:
    tags:
      - v*
jobs:
  release:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GOLANGCI_LINT_TOKEN }}
    steps:
      - uses: actions/checkout@v2
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16
      - name: Unshallow
        run: git fetch --prune --unshallow

      - name: Create release
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GOLANGCI_LINT_TOKEN }}

  docker-release:
    needs: release
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
          - Dockerfile: build/Dockerfile
          - Dockerfile: build/Dockerfile.alpine
    steps:
      - uses: actions/checkout@v2

      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16

      - name: Unshallow
        run: git fetch --prune --unshallow

      - name: Prepare
        id: prepare
        run: |
          TAG=${GITHUB_REF#refs/tags/}
          MAJOR=${TAG%.*}
          SHORT_COMMIT=${GITHUB_SHA::8}
          DATE=$(date '+%Y-%m-%dT%H:%M:%SZ')
          echo ::set-output name=tag_name::${TAG}
          echo ::set-output name=major_tag::${MAJOR}
          echo ::set-output name=short_commit::${SHORT_COMMIT}
          echo ::set-output name=date::${DATE}
          if [[ ${{ matrix.target.Dockerfile }} == *"alpine"* ]]; then
            echo ::set-output name=full_tag_name::${TAG}-alpine
            echo ::set-output name=full_major_tag::${MAJOR}-alpine
            echo ::set-output name=latest_tag::latest-alpine
          else
            echo ::set-output name=full_tag_name::${TAG}
            echo ::set-output name=full_major_tag::${MAJOR}
            echo ::set-output name=latest_tag::latest
          fi

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

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

      - name: Login do docker.io
        run: docker login -u golangci -p ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }}

      - name: Build and publish ${{ matrix.target.Dockerfile }}
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ${{ matrix.target.Dockerfile }}
          platforms: linux/amd64,linux/arm64
          push: true
          build-args: |
            VERSION=${{ steps.prepare.outputs.tag_name }}
            SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }}
            DATE=${{ steps.prepare.outputs.date }}
          tags: |
            golangci/golangci-lint:${{ steps.prepare.outputs.full_tag_name }}
            golangci/golangci-lint:${{ steps.prepare.outputs.full_major_tag }}
            golangci/golangci-lint:${{ steps.prepare.outputs.latest_tag }}
