blackd-client 0.0.6

Blazing fast Python code formatting using Black
name: Rust

on:
  workflow_dispatch:
  push:
    branches: [main]
    tags: ["v*.*.*"]
  pull_request:
    branches: [main]
  release:
    types:
      - created

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2.3.5
        with:
          fetch-depth: 1

      - name: Build
        run: cargo build --verbose

      - name: Check format
        run: cargo fmt --all -- --check

      - name: Clippy
        run: cargo clippy -- -D warnings

      - name: Run tests
        run: cargo test --verbose

  release:
    name: Release ${{ matrix.name }}
    needs: [build]
    if: github.event_name == 'pull_request' || github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        name: [linux, macos]

        include:
          - name: Linux
            os: ubuntu-latest
            sccache-path: /home/runner/.cache/sccache
            artifact_name: target/release/blackd-client_linux
            asset_name: blackd-client_linux
          - name: macOS
            os: macos-latest
            sccache-path: /Users/runner/Library/Caches/Mozilla.sccache
            artifact_name: target/release/blackd-client_macos
            asset_name: blackd-client_macos

    env:
      RUST_BACKTRACE: full
      RUSTC_WRAPPER: sccache
      RUSTV: ${{ matrix.rust }}
      SCCACHE_CACHE_SIZE: 2G
      SCCACHE_DIR: ${{ matrix.sccache-path }}
      # SCCACHE_RECACHE: 1 # Uncomment this to clear cache, then comment it back out

    steps:
      - uses: actions/checkout@v2.3.5
        with:
          fetch-depth: 1

      - name: Install sccache (linux)
        if: matrix.os == 'ubuntu-latest'
        env:
          LINK: https://github.com/mozilla/sccache/releases/download
          SCCACHE_VERSION: 0.2.13
        run: |
          SCCACHE_FILE=sccache-$SCCACHE_VERSION-x86_64-unknown-linux-musl
          mkdir -p $HOME/.local/bin
          curl -L "$LINK/$SCCACHE_VERSION/$SCCACHE_FILE.tar.gz" | tar xz
          mv -f $SCCACHE_FILE/sccache $HOME/.local/bin/sccache
          echo "$HOME/.local/bin" >> $GITHUB_PATH

      - name: Install sccache (macos)
        if: matrix.os == 'macos-latest'
        run: |
          brew update
          brew install sccache

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

      - name: Cache cargo registry
        uses: actions/cache@v2.1.6
        continue-on-error: false
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Save sccache
        uses: actions/cache@v2.1.6
        continue-on-error: false
        with:
          path: ${{ matrix.sccache-path }}
          key: ${{ runner.os }}-sccache-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-sccache-

      - name: Start sccache server
        run: sccache --start-server

      - name: Build
        run: |
          cargo build --release --locked
          strip target/release/blackd-client
          mv target/release/blackd-client ${{ matrix.artifact_name }}

      - name: Print sccache stats
        run: sccache --show-stats

      - name: Stop sccache server
        run: sccache --stop-server || true

      - name: Upload binaries to release
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ matrix.artifact_name }}

      - name: Set outputs
        id: vars
        run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

      - name: Check outputs
        run: echo ${{ steps.vars.outputs.sha_short }}

      - name: Create tag
        uses: actions/github-script@v5
        if: startsWith(github.ref , 'refs/tags/') != true
        continue-on-error: true
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.git.createRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: "refs/tags/${{ steps.vars.outputs.sha_short }}",
              sha: context.sha
            })

      - name: Release artifacts commit
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref , 'refs/tags/') != true
        with:
          files: ${{ matrix.artifact_name }}
          draft: true
          tag_name: ${{ steps.vars.outputs.sha_short }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Release artifacts tag
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: ${{ matrix.artifact_name }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish:
    name: Publish
    needs: [build]
    if: github.event_name == 'release' && github.event.action == 'created'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2.3.5
        with:
          fetch-depth: 1

      - name: Publish crate
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_PUBLISH_TOKEN }}