name: Run benchmark and upload results

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

jobs:
  bench:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v1

      - name: Get version
        id: version
        shell: bash
        run: echo ::set-output name=VERSION::"$([[ "$GITHUB_REF" == refs/tags/v* ]] && echo ${GITHUB_REF#refs/tags/v} || echo '0.0.0')"

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          default: true

      - name: Run prebuild steps
        shell: bash
        run: bash ./prebuild.sh

      - name: Set up Node.js for benching
        uses: actions/setup-node@master
        with:
          node-version: 16.x

      - name: Set up Node.js module
        working-directory: ./nodejs
        run: npm install

      - name: Build bench
        working-directory: ./bench
        run: |
          ./build

      - name: Set up Backblaze B2 CLI
        uses: wilsonzlin/setup-b2@v3

      - name: Run bench and upload results
        working-directory: ./bench
        run: |
          b2 authorize-account ${{ secrets.CICD_CLI_B2_KEY_ID }} ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }}
          ./run
          node graph.js
          b2 sync ./graphs/ b2://${{ secrets.CICD_CLI_B2_BUCKET_NAME }}/minify-html/bench/${{ steps.version.outputs.VERSION }}/js/
          MHB_HTML_ONLY=1 ./run
          node graph.js
          b2 sync ./graphs/ b2://${{ secrets.CICD_CLI_B2_BUCKET_NAME }}/minify-html/bench/${{ steps.version.outputs.VERSION }}/core/
