on:
  push:
    # # Sequence of patterns matched against refs/tags
    tags:
      - "v*" # Push events to matching v*, i.e. v1.0, v20.15.10

name: Mean Bean Deploy
env:
  BIN: mcfly

jobs:
  # This job downloads and stores `cross` as an artifact, so that it can be
  # redownloaded across all of the jobs. Currently this copied pasted between
  # `mean_bean_ci.yml` and `mean_bean_deploy.yml`. Make sure to update both places when making
  # changes.
  install-cross:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
        with:
          fetch-depth: 50
      - uses: XAMPPRocky/get-github-release@v1
        id: cross
        with:
          owner: rust-embedded
          repo: cross
          matches: ${{ matrix.platform }}
          token: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions/upload-artifact@v1
        with:
          name: cross-${{ matrix.platform }}
          path: ${{ steps.cross.outputs.install_path }}
    strategy:
      matrix:
        platform: [linux-musl]

  macos:
    runs-on: macos-latest
    strategy:
      matrix:
        target:
          # macOS
          - x86_64-apple-darwin
          ## Disabling creating M1 builds, will need to wait for github to release M1 VMs
          # - aarch64-apple-darwin
    steps:
      - name: Get tag
        id: tag
        uses: dawidd6/action-get-tag@v1

      - name: Setup | Checkout
        uses: actions/checkout@v2

      # Cache files between builds
      - name: Setup | Cache Cargo
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{ matrix.target }}

      - name: Setup | Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          profile: minimal
          target: ${{ matrix.target }}

      - name: Build | Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --target ${{ matrix.target }}

      - run: tar -czvf ${{ env.BIN }}.tar.gz --directory=target/${{ matrix.target }}/release ${{ env.BIN }}
      - uses: XAMPPRocky/create-release@v1.0.2
        id: create_release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: false
          prerelease: false

      - uses: actions/upload-release-asset@v1
        id: upload-release-asset
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ${{ env.BIN }}.tar.gz
          asset_name: ${{ env.BIN }}-${{steps.tag.outputs.tag}}-${{ matrix.target }}.tar.gz
          asset_content_type: application/gzip

  linux:
    runs-on: ubuntu-latest
    needs: install-cross
    strategy:
      matrix:
        target:
          - aarch64-unknown-linux-gnu
          - aarch64-unknown-linux-musl
          - arm-unknown-linux-gnueabi
          - arm-unknown-linux-gnueabihf
          - armv7-unknown-linux-gnueabihf
          - i686-unknown-linux-musl
          - x86_64-unknown-linux-musl
    steps:
      - name: Get tag
        id: tag
        uses: dawidd6/action-get-tag@v1

      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v1
        with:
          name: cross-linux-musl
          path: /tmp/
      - run: chmod +x /tmp/cross

      - run: ci/set_rust_version.bash stable ${{ matrix.target }}
      - run: ci/build.bash /tmp/cross ${{ matrix.target }} RELEASE
      - run: tar -czvf ${{ env.BIN }}.tar.gz --directory=target/${{ matrix.target }}/release ${{ env.BIN }}
      - uses: XAMPPRocky/create-release@v1.0.2
        id: create_release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: true
          prerelease: false
      - name: Upload Release Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ${{ env.BIN }}.tar.gz
          asset_name: ${{ env.BIN }}-${{steps.tag.outputs.tag}}-${{ matrix.target }}.tar.gz
          asset_content_type: application/gzip
