name: Release

on:
  push:
    branches:
      - 'master'
    tags:
      - 'v*.*.*'

jobs:
  build_and_publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - run: git submodule update --init --recursive
      - name: Install stable
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable-x86_64-unknown-linux-gnu
          override: true
      - name: Install sccache
        env:
          SCCACHE_LINK: https://github.com/mozilla/sccache/releases/download
          SCCACHE_VERSION: 0.2.13
        run: |
          curl -L "$SCCACHE_LINK/$SCCACHE_VERSION/sccache-$SCCACHE_VERSION-x86_64-unknown-linux-musl.tar.gz" | tar xz
          echo "$PWD/sccache-$SCCACHE_VERSION-x86_64-unknown-linux-musl/" >> $GITHUB_PATH
          echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV
      - name: cache sccache files
        uses: actions/cache@v2.1.4
        with:
          path: ~/.cache/sccache
          key: release-stable-sccache-0213-cache-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            release-stable-sccache-0213-cache-
      - name: show sccache stats
        run: sccache --show-stats
      - name: checks
        uses: actions-rs/cargo@v1
        if: github.ref == 'refs/heads/master'
        with:
          command: check
          args: --tests --workspace --release --verbose
      - name: tests
        if: github.ref == 'refs/heads/master'
        uses: actions-rs/cargo@v1
        timeout-minutes: 20
        with:
          command: test
          args: --workspace --release --verbose --no-fail-fast -- --nocapture
      - name: builds
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --workspace --release --verbose

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

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GHCR_TOKEN }}
      - name: Login to Quay.io
        uses: docker/login-action@v1
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_USER }}
          password: ${{ secrets.QUAY_TOKEN }}

      - name: Get the version
        id: get_version
        run: |	
          v="${GITHUB_REF##*/}"
          if [[ "$v" == "master" ]]; then
            echo ::set-output name=VERSION::latest
          else
            echo ::set-output name=VERSION::${v}
          fi

      - name: mmdb-server push to GitHub Container Registry
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          context: .
          file: docker/server/Dockerfile
          tags: ghcr.io/${{ github.repository_owner }}/mmdb-server:${{ steps.get_version.outputs.VERSION }}
          push: true
      - name: mmdb-reload push to GitHub Container Registry
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          context: .
          file: docker/reload/Dockerfile
          tags: ghcr.io/${{ github.repository_owner }}/mmdb-reload:${{ steps.get_version.outputs.VERSION }}
          push: true

      - name: mmdb-server push to Docker Hub
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          context: .
          file: docker/server/Dockerfile
          tags: ${{ secrets.DOCKERHUB_USER }}/mmdb-server:${{ steps.get_version.outputs.VERSION }}
          push: true
      - name: mmdb-reload push to Docker Hub
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          context: .
          file: docker/reload/Dockerfile
          tags: ${{ secrets.DOCKERHUB_USER }}/mmdb-reload:${{ steps.get_version.outputs.VERSION }}
          push: true

      - name: mmdb-server push to Quay.io
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          context: .
          file: docker/server/Dockerfile
          tags: quay.io/${{ secrets.QUAY_USER }}/mmdb-server:${{ steps.get_version.outputs.VERSION }}
          push: true
      - name: mmdb-reload push to Quay.io
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          context: .
          file: docker/reload/Dockerfile
          tags: quay.io/${{ secrets.QUAY_USER }}/mmdb-reload:${{ steps.get_version.outputs.VERSION }}
          push: true
