name: Build

on:
  push:
    branches:
      - '**'

jobs:
  test_and_build:
    strategy:
      fail-fast: false
      matrix:
        version:
          - stable
          - nightly
    name: Test and Build - ${{ matrix.version }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - run: git submodule update --init --recursive
      - name: Install ${{ matrix.version }}
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.version }}-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: ${{ matrix.version }}-sccache-0213-cache-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ matrix.version }}-sccache-0213-cache-
      - name: Show sccache stats
        run: sccache --show-stats
      - name: Cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --tests --workspace --verbose
      - name: Cargo test
        uses: actions-rs/cargo@v1
        timeout-minutes: 20
        with:
          command: test
          args: --workspace --verbose --no-fail-fast -- --nocapture
      - name: Cargo build
        uses: actions-rs/cargo@v1
        timeout-minutes: 5
        with:
          command: build
          args: --workspace --verbose
  lints:
    name: Clippy and Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - run: git submodule update --init --recursive
      - name: Install toolchain and components
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable-x86_64-unknown-linux-gnu
          components: clippy, rustfmt
          override: true
      - name: Rustfmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --color=always --check
      - name: Cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --workspace --verbose -- -D warnings
