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'

env:
  DOCKERHUB_SLUG: crazymax/diun
  GHCR_SLUG: ghcr.io/crazy-max/diun

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Test
        uses: docker/bake-action@v1
        with:
          targets: test
      -
        name: Upload coverage
        uses: codecov/codecov-action@v1
        with:
          file: ./coverage.txt

  build:
    runs-on: ubuntu-latest
    needs: [ test ]
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      -
        name: Docker meta
        id: meta
        uses: crazy-max/ghaction-docker-meta@v2
        with:
          images: |
            ${{ env.DOCKERHUB_SLUG }}
            ${{ env.GHCR_SLUG }}
          tags: |
            type=semver,pattern={{version}}
            type=ref,event=pr
            type=edge
          labels: |
            org.opencontainers.image.title=Diun
            org.opencontainers.image.description=Docker image update notifier
            org.opencontainers.image.vendor=CrazyMax
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        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: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Build artifacts
        uses: docker/bake-action@v1
        with:
          targets: artifact-all
      -
        name: Move artifacts
        run: |
          mv ./dist/**/* ./dist/
      -
        name: Upload artifacts
        uses: actions/upload-artifact@v2
        with:
          name: diun
          path: ./dist/*
          if-no-files-found: error
      -
        name: Build
        uses: docker/bake-action@v1
        with:
          files: |
            ./docker-bake.hcl
            ${{ steps.meta.outputs.bake-file }}
          targets: image-all
          push: ${{ github.event_name != 'pull_request' }}
      -
        name: GitHub Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          draft: true
          files: |
            dist/*.tar.gz
            dist/*.zip
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Check manifest
        if: github.event_name != 'pull_request'
        run: |
          docker buildx imagetools inspect ${{ env.DOCKERHUB_SLUG }}:${{ steps.meta.outputs.version }}
          docker buildx imagetools inspect ${{ env.GHCR_SLUG }}:${{ steps.meta.outputs.version }}
      -
        name: Inspect image
        if: github.event_name != 'pull_request'
        run: |
          docker pull ${{ env.DOCKERHUB_SLUG }}:${{ steps.meta.outputs.version }}
          docker image inspect ${{ env.DOCKERHUB_SLUG }}:${{ steps.meta.outputs.version }}
          docker pull ${{ env.GHCR_SLUG }}:${{ steps.meta.outputs.version }}
          docker image inspect ${{ env.GHCR_SLUG }}:${{ steps.meta.outputs.version }}
