name: Release

on:
  push:
    tags:
      - v*

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Prepare Crate
        uses: actions-rs/cargo@v1
        with:
          command: package

      - name: Login to crates.io
        uses: actions-rs/cargo@v1
        with:
          command: login
          args: ${{ secrets.CARGO_TOKEN }}

      - name: Publish Crate
        uses: actions-rs/cargo@v1
        with:
          command: publish

  dockerize:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Set environment
        run: echo "docker_image_tag=${GITHUB_REF##*/v}" >> $GITHUB_ENV

      - uses: docker/login-action@v1.8.0
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_TOKEN }}

      - uses: docker/build-push-action@v2.3.0
        with:
          push: true
          tags: |
            ${{ secrets.DOCKER_HUB_USERNAME }}/mc-server-pinger:${{ env.docker_image_tag }}
            ${{ secrets.DOCKER_HUB_USERNAME }}/mc-server-pinger:latest

  release:
    strategy:
      matrix:
        target: [ x86_64-unknown-linux-gnu, x86_64-pc-windows-msvc, x86_64-apple-darwin ]
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            binary-name: mc-server-pinger
            artifact-name: mc-server-pinger-linux-x86_64
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            binary-name: mc-server-pinger.exe
            artifact-name: mc-server-pinger-windows-x86_64.exe
          - target: x86_64-apple-darwin
            os: macOS-latest
            binary-name: mc-server-pinger
            artifact-name: mc-server-pinger-darwin-x86_64
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2

      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          override: true

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --target ${{ matrix.target }}

      - name: Move binary
        run: mv target/${{ matrix.target }}/release/${{ matrix.binary-name }} ${{ matrix.artifact-name }}

      - name: Strip binary
        run: strip ${{ matrix.artifact-name }}

      - name: Generate checksum
        run: shasum -a 256 ${{ matrix.artifact-name }} > ${{ matrix.artifact-name }}.sha256

      - name: Upload release binary
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: mc-server-pinger-*
          file_glob: true
          tag: ${{ github.ref }}

