name: main

on:
  push:
    branches:
      - main

env:
  CARGO_TERM_VERBOSE: true
  RUST_BACKTRACE: 1

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
    steps:
      - uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

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

      - name: Run cargo build
        run: cargo build --release --workspace

  publish:
    name: Publish
    runs-on: ubuntu-latest
    needs: build
    if: "startsWith(github.event.head_commit.message, 'chore(release):')"
    steps:
      - uses: actions/checkout@v2
      # checkout with fetch-depth: '0' to be sure to retrieve all commits to look for the semver commit message
        with:
          fetch-depth: '0'

      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Cargo Login
        run: cargo login ${{ secrets.CRATES_IO_TOKEN }}

      - name: Cargo Publish
        run: cargo publish --allow-dirty
