name: Ruby workflow

on:
  workflow_dispatch:
  push:
    branches:
      - trunk
    paths:
      - .bazel*
      - .github/actions/**
      - .github/workflows/ruby.yml
      - common/**
      - javascript/atoms/**
      - javascript/cdp-support/**
      - javascript/webdriver/**
      - rb/**
  pull_request:
    branches:
      - trunk
    paths:
      - .bazel*
      - .github/actions/**
      - .github/workflows/ruby.yml
      - common/**
      - javascript/atoms/**
      - javascript/cdp-support/**
      - javascript/webdriver/**
      - rb/**

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: ['selenium-devtools', 'selenium-webdriver']
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: 11
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.6
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cache/bazel-disk
            ~/.cache/bazel-repo
          key: ${{ runner.os }}-bazel-ruby-build-gem-${{ matrix.target }}-${{ hashFiles('**/BUILD.bazel') }}
          restore-keys: |
            ${{ runner.os }}-bazel-ruby-build-gem-${{ matrix.target }}-
      - uses: ./.github/actions/setup-bazelisk
      - uses: ./.github/actions/bazel
        with:
          command: build //rb:${{ matrix.target }}

  chrome-test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: ['chrome-test', 'remote-chrome-test']
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: 11
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.6
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cache/bazel-disk
            ~/.cache/bazel-repo
          key: ${{ runner.os }}-bazel-ruby-${{ matrix.target }}-${{ hashFiles('**/BUILD.bazel') }}
          restore-keys: |
            ${{ runner.os }}-bazel-ruby-${{ matrix.target }}-
      - uses: ./.github/actions/setup-bazelisk
      - uses: ./.github/actions/setup-chrome
      - run: Xvfb :99 &
      - uses: ./.github/actions/bazel
        with:
          command: test --test_output=all //rb:${{ matrix.target }}
        env:
          DISPLAY: :99

  firefox-test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: ['firefox-test', 'remote-firefox-test']
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: 11
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.6
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cache/bazel-disk
            ~/.cache/bazel-repo
          key: ${{ runner.os }}-bazel-ruby-${{ matrix.target }}-${{ hashFiles('**/BUILD.bazel') }}
          restore-keys: |
            ${{ runner.os }}-bazel-ruby-${{ matrix.target }}-
      - uses: ./.github/actions/setup-bazelisk
      - uses: ./.github/actions/setup-firefox
      - run: Xvfb :99 &
      - uses: ./.github/actions/bazel
        with:
          command: test --test_output=all //rb:${{ matrix.target }}
        env:
          DISPLAY: :99

  docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.6
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cache/bazel-disk
            ~/.cache/bazel-repo
          key: ${{ runner.os }}-bazel-ruby-docs-${{ hashFiles('**/BUILD.bazel') }}
          restore-keys: |
            ${{ runner.os }}-bazel-ruby-${{ matrix.target }}-
      - uses: ./.github/actions/setup-bazelisk
      - uses: ./.github/actions/bazel
        with:
          command: run //rb:docs

  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.6
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cache/bazel-disk
            ~/.cache/bazel-repo
          key: ${{ runner.os }}-bazel-ruby-lint-${{ hashFiles('**/BUILD.bazel') }}
          restore-keys: |
            ${{ runner.os }}-bazel-ruby-lint-
      - uses: ./.github/actions/setup-bazelisk
      - uses: ./.github/actions/bazel
        with:
          command: run //rb:lint

  unit-test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ruby: ['2.6', 'jruby-9.3.0.0']
    steps:
      - uses: actions/checkout@v2
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby }}
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cache/bazel-disk
            ~/.cache/bazel-repo
          key: ${{ runner.os }}-bazel-ruby-unit-test-${{ matrix.ruby }}-${{ hashFiles('**/BUILD.bazel') }}
          restore-keys: |
            ${{ runner.os }}-bazel-ruby-unit-test-${{ matrix.ruby }}-
      - uses: ./.github/actions/setup-bazelisk
      - uses: ./.github/actions/bazel
        with:
          command: test --test_output=all //rb:unit-test
