name: build

on:
  push:
    branches:
      - 'master'
    tags:
      - 'v*'
    paths-ignore:
      - '**.md'
      - '.github/workflows/docs.yml'
      - 'docs/**'
      - 'mkdocs.yml'
  pull_request:
    branches:
      - 'master'
    paths-ignore:
      - '**.md'
      - '.github/workflows/docs.yml'
      - 'docs/**'
      - 'mkdocs.yml'

jobs:
  go:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Prepare
        id: prep
        run: |
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            echo ::set-output name=tag_name::${GITHUB_REF#refs/tags/}
          fi
      -
        name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.13
      -
        name: Cache Go modules
        uses: actions/cache@v2
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      -
        name: GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --skip-publish --rm-dist
      -
        name: GitHub Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          draft: true
          files: |
            dist/checksums.txt
            dist/*.tar.gz
            dist/*.zip
          name: ${{ steps.prep.outputs.tag_name }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  docker:
    runs-on: ubuntu-latest
    env:
      DOCKERHUB_SLUG: crazymax/diun
      GHCR_SLUG: ghcr.io/crazy-max/diun
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Docker meta
        id: docker_meta
        uses: crazy-max/ghaction-docker-meta@v1
        with:
          images: |
            ${{ env.DOCKERHUB_SLUG }}
            ${{ env.GHCR_SLUG }}
          tag-edge: true
          tag-match: v(.*)
          tag-match-group: 1
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
        with:
          buildkitd-flags: "--debug"
      -
        name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-
      -
        name: Login to DockerHub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      -
        name: Login to GHCR
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ secrets.GHCR_USERNAME }}
          password: ${{ secrets.GHCR_TOKEN }}
      -
        name: Build
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/386,linux/ppc64le,linux/s390x
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.docker_meta.outputs.tags }}
          build-args: |
            VERSION=${{ steps.docker_meta.outputs.version }}
          labels: |
            ${{ steps.docker_meta.outputs.labels }}
            org.opencontainers.image.title=Diun
            org.opencontainers.image.description=Docker image update notifier
            org.opencontainers.image.vendor=CrazyMax
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,mode=max,dest=/tmp/.buildx-cache
      -
        name: Check manifest
        if: github.event_name != 'pull_request'
        run: |
          docker buildx imagetools inspect ${{ env.DOCKERHUB_SLUG }}:${{ steps.docker_meta.outputs.version }}
          docker buildx imagetools inspect ${{ env.GHCR_SLUG }}:${{ steps.docker_meta.outputs.version }}
      -
        name: Inspect image
        if: github.event_name != 'pull_request'
        run: |
          docker pull ${{ env.DOCKERHUB_SLUG }}:${{ steps.docker_meta.outputs.version }}
          docker image inspect ${{ env.DOCKERHUB_SLUG }}:${{ steps.docker_meta.outputs.version }}
          docker pull ${{ env.GHCR_SLUG }}:${{ steps.docker_meta.outputs.version }}
          docker image inspect ${{ env.GHCR_SLUG }}:${{ steps.docker_meta.outputs.version }}
