name: Create release with node binaries

on:
  workflow_dispatch:
    inputs:
      name:
        description: 'Release name (v9.3.0 - Antioch)'
        required: true
      tag:
        description: 'Tag (v9.3.0)'
        required: true

env:
  REPOSITORY: joystream/node

jobs:
  build-mac-binary:
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - id: compute_shasum
        name: Compute runtime code shasum
        run: |
          export RUNTIME_CODE_SHASUM=`scripts/runtime-code-shasum.sh`
          echo "::set-output name=shasum::${RUNTIME_CODE_SHASUM}"

      - name: Run Setup
        run: |
          ./setup.sh

      - name: Build binaries
        run: |
          yarn cargo-build

      - name: Tar the binary
        run: |
          tar czvf joystream-node-macos.tar.gz -C ./target/release joystream-node

      - name: Temporarily save node binary
        uses: actions/upload-artifact@v2
        with:
          name: joystream-node-macos-${{ steps.compute_shasum.outputs.shasum }}
          path: joystream-node-macos.tar.gz
          retention-days: 1

  build-rpi-binary:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - id: compute_shasum
        name: Compute runtime code shasum
        run: |
          export RUNTIME_CODE_SHASUM=`scripts/runtime-code-shasum.sh`
          echo "::set-output name=shasum::${RUNTIME_CODE_SHASUM}"

      - name: Run Setup
        run: |
          ./setup.sh

      - name: Build binaries
        run: |
          export WORKSPACE_ROOT=`cargo metadata --offline --no-deps --format-version 1 | jq .workspace_root -r`
          sudo chmod a+w $WORKSPACE_ROOT
          ./scripts/raspberry-cross-build.sh

      - name: Tar the binary
        run: |
          tar czvf joystream-node-rpi.tar.gz -C ./target/arm-unknown-linux-gnueabihf/release joystream-node

      - name: Temporarily save node binary
        uses: actions/upload-artifact@v2
        with:
          name: joystream-node-rpi-${{ steps.compute_shasum.outputs.shasum }}
          path: joystream-node-rpi.tar.gz
          retention-days: 1

  create-release:
    runs-on: ubuntu-latest
    needs: [build-mac-binary, build-rpi-binary]
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - id: compute_shasum
        name: Compute runtime code shasum
        run: |
          export RUNTIME_CODE_SHASUM=`scripts/runtime-code-shasum.sh`
          echo "::set-output name=shasum::${RUNTIME_CODE_SHASUM}"

      - id: extract_binaries
        name: Copy binaries & wasm file from docker images
        run: |
          IMAGE=${{ env.REPOSITORY }}:${{ steps.compute_shasum.outputs.shasum }}

          docker run -d --entrypoint tail --name temp-container-joystream-node $IMAGE-amd64 -f /dev/null

          RESULT=$(docker exec temp-container-joystream-node b2sum -l 256 runtime.compact.wasm | awk '{print $1}')
          VERSION_AND_COMMIT=$(docker exec temp-container-joystream-node /joystream/node --version | awk '{print $2}' | cut -d- -f -2)
          echo "::set-output name=blob_hash::${RESULT}"
          echo "::set-output name=version_and_commit::${VERSION_AND_COMMIT}"

          docker cp temp-container-joystream-node:/joystream/runtime.compact.wasm ./joystream_runtime_${{ github.event.inputs.tag }}.wasm
          docker cp temp-container-joystream-node:/joystream/node ./joystream-node
          tar -czvf joystream-node-$VERSION_AND_COMMIT-x86_64-linux-gnu.tar.gz joystream-node

          docker rm --force temp-container-joystream-node

          docker cp $(docker create --rm $IMAGE-arm64):/joystream/node ./joystream-node
          tar -czvf joystream-node-$VERSION_AND_COMMIT-arm64-linux-gnu.tar.gz joystream-node

          docker cp $(docker create --rm $IMAGE-arm):/joystream/node ./joystream-node
          tar -czvf joystream-node-$VERSION_AND_COMMIT-armv7-linux-gnu.tar.gz joystream-node

      - name: Retrieve saved MacOS binary
        uses: actions/download-artifact@v2
        with:
          name: joystream-node-macos-${{ steps.compute_shasum.outputs.shasum }}

      - name: Retrieve saved RPi binary
        uses: actions/download-artifact@v2
        with:
          name: joystream-node-rpi-${{ steps.compute_shasum.outputs.shasum }}

      - name: Rename MacOS and RPi tar
        run: |
          mv joystream-node-macos.tar.gz joystream-node-${{ steps.extract_binaries.outputs.version_and_commit }}-x86_64-macos.tar.gz
          mv joystream-node-rpi.tar.gz joystream-node-${{ steps.extract_binaries.outputs.version_and_commit }}-rpi.tar.gz

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            *.tar.gz
            *.wasm
          tag_name: ${{ github.event.inputs.tag }}
          name: ${{ github.event.inputs.name }}
          draft: true
          body: 'Verify wasm hash:
            ```
            $ b2sum -l 256 joystream_runtime_${{ github.event.inputs.tag }}.wasm
            ```

            This should be the output

            ```
            ${{ steps.extract_binaries.outputs.blob_hash }}
            ```
            '
