name: release

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

env:
  RUST_BACKTRACE: 1

jobs:
  create-release:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get-release-version.outputs.version }}
      upload-url: ${{ steps.release.outputs.upload_url }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get release version from tag
        id: get-release-version
        run: |
          echo "::set-output name=version::${GITHUB_REF#refs/tags/}"
      - name: Create GitHub release
        id: release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.get-release-version.outputs.version }}
          release_name: ${{ steps.get-release-version.outputs.version }}
          body_path: RELEASE.md
  build-linux:
    name: build (linux)
    runs-on: ubuntu-latest
    needs: [create-release]
    steps:
    - uses: actions/checkout@v2
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        profile: minimal
        override: true
    - name: Build
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose --release
    - name: Upload release asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create-release.outputs.upload-url }}
        asset_path: target/release/bader
        asset_name: bader-${{ needs.create-release.outputs.version }}-x86_64-linux
        asset_content_type: application/octet-stream
    - name: Upload action assest
      uses: actions/upload-artifact@v2
      with:
        name: bader-${{ needs.create-release.outputs.version }}-x86_64-linux
        path: target/release/bader
  build-linux-musl:
    name: build (linux musl)
    runs-on: ubuntu-latest
    needs: [create-release]
    steps:
    - uses: actions/checkout@v2
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        target: x86_64-unknown-linux-musl
        override: true
    - name: Build
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose --release --target x86_64-unknown-linux-musl
    - name: Upload release asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create-release.outputs.upload-url }}
        asset_path: target/x86_64-unknown-linux-musl/release/bader
        asset_name: bader-${{ needs.create-release.outputs.version }}-x86_64-linux-musl
        asset_content_type: application/octet-stream
    - name: Upload action assest
      uses: actions/upload-artifact@v2
      with:
        name: bader-${{ needs.create-release.outputs.version }}-x86_64-linux-musl
        path: target/x86_64-unknown-linux-musl/release/bader
  build-windows:
    name: build (windows)
    runs-on: windows-latest
    needs: [create-release]
    steps:
    - uses: actions/checkout@v2
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        profile: minimal
        override: true
    - name: Build
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose --release
    - name: Upload release asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create-release.outputs.upload-url }}
        asset_path: target/release/bader.exe
        asset_name: bader-${{ needs.create-release.outputs.version }}-x86_64-windows.exe
        asset_content_type: application/octet-stream
    - name: Upload action assest
      uses: actions/upload-artifact@v2
      with:
        name: bader-${{ needs.create-release.outputs.version }}-x86_64-windows.exe
        path: target/release/bader.exe
  build-macos:
    name: build (macos)
    runs-on: macos-latest
    needs: [create-release]
    steps:
    - uses: actions/checkout@v2
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        profile: minimal
        override: true
    - name: Build
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose --release
    - name: Upload release asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create-release.outputs.upload-url }}
        asset_path: target/release/bader
        asset_name: bader-${{ needs.create-release.outputs.version }}-x86_64-macos
        asset_content_type: application/octet-stream
    - name: Upload action assest
      uses: actions/upload-artifact@v2
      with:
        name: bader-${{ needs.create-release.outputs.version }}-x86_64-macos
        path: target/release/bader
  publish:
    runs-on: ubuntu-latest
    needs: [build-linux, build-macos, build-windows]
    steps:
    - uses: actions/checkout@v2
    - name: Publish crate
      uses: actions-rs/cargo@v1
      with:
        command: publish
        args: --verbose --token ${{ secrets.CARGO_PUBLISH_TOKEN }}
