name: deploy

on:
  push:
    tags:
      - 'v*.*.*'

jobs:

  windows-binaries:

    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v2

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

    - name: Build json-minimal-tests
      run: |
        cargo build --release

    - name: Create zip
      run: |
        cd target/release
        7z a ../../json-minimal-tests-windows-msvc.zip `
             "json-minimal-tests.exe"

    - name: Upload binaries
      uses: actions/upload-artifact@v2
      with:
        name: windows-msvc-binaries
        path: json-minimal-tests-windows-msvc.zip

  linux-binaries:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Install musl-tools
      run: |
        sudo apt-get install musl-tools

    - name: Install Rust stable and musl target
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        target: x86_64-unknown-linux-musl
        override: true

    - name: Build json-minimal-tests
      run: |
        cargo build --release --target x86_64-unknown-linux-musl

    - name: Create zip
      run: |
        cd target/x86_64-unknown-linux-musl/release
        strip json-minimal-tests
        tar -czvf $GITHUB_WORKSPACE/json-minimal-tests-linux.tar.gz \
                  json-minimal-tests

    - name: Upload binaries
      uses: actions/upload-artifact@v2
      with:
        name: linux-binaries
        path: json-minimal-tests-linux.tar.gz

  macos-binaries:

    runs-on: macos-latest

    steps:
    - uses: actions/checkout@v2

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

    - name: Build json-minimal-tests
      run: |
        cargo build --release

    - name: Create zip
      run: |
        cd target/release
        strip json-minimal-tests
        zip $GITHUB_WORKSPACE/json-minimal-tests-macos.zip \
            json-minimal-tests

    - name: Upload binaries
      uses: actions/upload-artifact@v2
      with:
        name: macos-binaries
        path: json-minimal-tests-macos.zip

  deploy:

    needs: [windows-binaries, linux-binaries, macos-binaries]

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Download zip files
      uses: actions/download-artifact@v2

    - name: Create Cargo.lock
      run: |
        cargo update

    - name: Create a release
      uses: softprops/action-gh-release@v1
      with:
        files: |
          Cargo.lock
          linux-binaries/json-minimal-tests-linux.tar.gz
          macos-binaries/json-minimal-tests-macos.zip
          windows-msvc-binaries/json-minimal-tests-windows-msvc.zip
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
