name: Build and publish Ruby gem

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

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-18.04, macos-11.0]
        ruby: [2.5.7, 2.6.5, 2.7.0]
        include:
          - os: ubuntu-18.04
            NAME: linux
            FILE: 'libminify_html_ruby_lib.so'
          - os: macos-11.0
            NAME: macos
            FILE: 'libminify_html_ruby_lib.dylib'
    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: Get native module file name
        id: native_file
        shell: bash
        env:
          RUBY_VERSION: ${{ matrix.ruby }}
        run: echo ::set-output name=NAME::${{ matrix.NAME }}-ruby${RUBY_VERSION%.*}

      # Install Ruby manually as actions/setup-ruby@v1 does not compile with `--enable-shared`.
      - name: Install rbenv dependencies (macOS)
        if: runner.os == 'macOS'
        run: brew install openssl libyaml libffi

      - name: Install rbenv dependencies (Linux)
        if: runner.os == 'Linux'
        run: sudo apt install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev

      - name: Install rbenv
        # rbenv-installer now uses Linux brew to install Ruby, which we do not want.
        run: |
          git clone https://github.com/rbenv/rbenv.git ~/.rbenv
          mkdir -p "$HOME/.rbenv/plugins"
          git clone https://github.com/rbenv/ruby-build.git "$HOME/.rbenv/plugins/ruby-build"

      - name: Set up Ruby
        run: |
          export PATH="$HOME/.rbenv/bin:$PATH"
          eval "$(rbenv init -)"
          rbenv install ${{ matrix.ruby }}
          rbenv global ${{ matrix.ruby }}

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

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: '^1.14.0'

      - name: Set up GCC (Windows)
        if: runner.os == 'Windows'
        run: .\.github\workflows\gcc.ps1

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

      - name: Build Ruby native library
        run: |
          export PATH="$HOME/.rbenv/bin:$PATH"
          eval "$(rbenv init -)"
          cargo build -vvv --release
        working-directory: ./ruby

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

      - name: Upload to B2
        run: |
          b2 authorize-account ${{ secrets.CICD_CLI_B2_KEY_ID }} ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }}
          b2 upload-file ${{ secrets.CICD_CLI_B2_BUCKET_NAME }} ./ruby/target/release/${{ matrix.FILE }} minify-html/bin/ruby/${{ steps.version.outputs.VERSION }}/${{ steps.native_file.outputs.NAME }}

  package:
    runs-on: ubuntu-18.04
    needs: build
    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 Ruby
        uses: actions/setup-ruby@v1
        with:
          ruby-version: '2.5'
      - name: Set up Backblaze B2 CLI
        uses: wilsonzlin/setup-b2@v3
      - name: Pack and publish gem
        working-directory: ./ruby
        run: |
          cp ../README.md .
          b2 authorize-account ${{ secrets.CICD_CLI_B2_KEY_ID }} ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }}
          b2 sync b2://${{ secrets.CICD_CLI_B2_BUCKET_NAME }}/minify-html/bin/ruby/${{ steps.version.outputs.VERSION }}/ ./lib/.
          gem build minify_html.gemspec
          mkdir -p "$HOME/.gem"
          cat << 'EOF' > "$HOME/.gem/credentials"
          ---
          :rubygems_api_key: ${{ secrets.RUBYGEMS_API_KEY }}
          EOF
          chmod 0600 "$HOME/.gem/credentials"
          if [[ "${{ steps.version.outputs.VERSION }}" != "0.0.0" ]]; then
            gem push minify_html-${{ steps.version.outputs.VERSION }}.gem
          fi
