name: ci
concurrency:
  group: "${{github.workflow}}-${{github.ref}}"
  cancel-in-progress: true
on:
  workflow_dispatch:
  schedule:
    - cron: "0 8 * * 3" # At 08:00 on Wednesday # https://crontab.guru/#0_8_*_*_3
  push:
    branches:
      - main
      - v*.*.x
    tags:
      - v*.*.*
  pull_request:
    types: [opened, synchronize]
    branches:
      - '*'

jobs:
  rubocop:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:mri-3.1
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - run: bundle install --local || bundle install
      - run: bundle exec rake rubocop

  gumbo:
    needs: ["rubocop"]
    strategy:
      fail-fast: false
      matrix:
        plat: ["ubuntu", "windows", "macos"]
    runs-on: ${{matrix.plat}}-latest
    steps:
      - name: configure git crlf
        if: matrix.plat == 'windows'
        run: |
          git config --system core.autocrlf false
          git config --system core.eol lf
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: MSP-Greg/setup-ruby-pkgs@v1
        with:
          ruby-version: "3.0" # OK for now, until ruby 3.1 CI is stable
          apt-get: "ragel"
          brew: "ragel"
          mingw: "ragel"
          bundler-cache: true
      - run: bundle exec rake gumbo:test

  basic:
    needs: ["rubocop"]
    strategy:
      fail-fast: false
      matrix:
        image: ["ubuntu", "ubuntu32"]
        sys: ["enable"]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:${{matrix.image}}
    steps:
      - uses: actions/checkout@v1 # v1 because of https://github.com/actions/checkout/issues/334
        with:
          submodules: true
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test

  linux:
    needs: ["basic"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["enable", "disable"]
        ruby: ["2.6", "2.7", "3.0", "3.1"]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:mri-${{matrix.ruby}}
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: actions/cache@v2
        if: matrix.sys == 'disable'
        with:
          path: ports
          key: ports-ubuntu-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test

  valgrind:
    needs: ["linux"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["enable", "disable"]
        ruby: ["2.6", "2.7", "3.0", "3.1"]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:mri-${{matrix.ruby}}
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: actions/cache@v2
        if: matrix.sys == 'disable'
        with:
          path: ports
          key: ports-ubuntu-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test:valgrind

  musl:
    needs: ["basic"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["enable"]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:alpine
    steps:
      - uses: actions/checkout@v1 # v1 because of https://github.com/actions/checkout/issues/334
        with:
          submodules: true
      # skip cache because of https://github.com/actions/cache/issues/675
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test

  musl-valgrind:
    needs: ["musl"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["disable"]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:alpine
    steps:
      - uses: actions/checkout@v1 # v1 because of https://github.com/actions/checkout/issues/334
        with:
          submodules: true
      # skip cache because of https://github.com/actions/cache/issues/675
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test:valgrind

  libxmlruby:
    needs: ["basic"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["enable"]
        ruby: ["3.1"]
    env:
      BUNDLE_GEMFILE: "Gemfile-libxml-ruby"
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:mri-${{matrix.ruby}}
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: actions/cache@v2
        if: matrix.sys == 'disable'
        with:
          path: ports
          key: ports-ubuntu-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test

  libxmlruby-valgrind:
    needs: ["libxmlruby"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["disable"]
        ruby: ["3.1"]
    env:
      BUNDLE_GEMFILE: "Gemfile-libxml-ruby"
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:mri-${{matrix.ruby}}
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: actions/cache@v2
        if: matrix.sys == 'disable'
        with:
          path: ports
          key: ports-ubuntu-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test:valgrind

  osx:
    needs: ["basic"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["enable", "disable"]
        ruby: ["2.6", "2.7", "3.0", "3.1"]
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{matrix.ruby}}
          bundler-cache: true
      - uses: actions/cache@v2
        if: matrix.sys == 'disable'
        with:
          path: ports
          key: ports-macos-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test

  windows:
    needs: ["basic"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["enable", "disable"]
        ruby: ["2.6", "2.7", "3.0", "3.1", "mingw"]
    runs-on: windows-2022
    steps:
      - name: configure git crlf
        run: |
          git config --system core.autocrlf false
          git config --system core.eol lf
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: MSP-Greg/setup-ruby-pkgs@v1
        with:
          ruby-version: "${{matrix.ruby}}"
          mingw: "libxml2 libxslt"
          bundler-cache: true
      - uses: actions/cache@v2
        if: matrix.sys == 'disable'
        with:
          path: ports
          key: ports-windows-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test

  jruby:
    needs: ["basic"]
    strategy:
      fail-fast: false
      matrix:
        ruby: ["jruby-9.3"]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{matrix.ruby}}
          bundler-cache: true
      - run: bundle exec rake compile
      - run: bundle exec rake test

  bsd:
    continue-on-error: true # we're seeing VMs hang and fail the whole workflow
    needs: ["basic"]
    strategy:
      fail-fast: false
      matrix:
        sys: ["enable", "disable"]
    runs-on: macos-12
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: vmactions/freebsd-vm@v0.2.0
        with:
          usesh: true
          prepare: pkg install -y ruby devel/ruby-gems pkgconf libxml2 libxslt
          run: |
            gem install bundler
            bundle install --local || bundle install
            bundle exec rake compile -- --${{matrix.sys}}-system-libraries
            bundle exec rake test

  memcheck:
    strategy:
      fail-fast: false
      matrix:
        sys: ["disable"]
        ruby: ["3.1"]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/sparklemotion/nokogiri-test:mri-${{matrix.ruby}}
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: actions/cache@v2
        if: matrix.sys == 'disable'
        with:
          path: ports
          key: ports-ubuntu-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
      - run: bundle install --local || bundle install
      - run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
      - run: bundle exec rake test:memcheck
