ruby-on-railsgithub-actionsnokogiri

GitHub Workflow setup-ruby action. How to do custom gem install in the middle


I have a GitHub Workflow file that looks like this:

name: "Ruby on Rails CI"
on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:11-alpine
        ports:
          - "5432:5432"
        env:
          POSTGRES_DB: rails_test
          POSTGRES_USER: rails
          POSTGRES_PASSWORD: password
    env:
      RAILS_ENV: test
      DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test"
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: install Nokogiri alone
        run: sudo gem install nokogiri -v 1.16.4
      # Add or replace dependency steps here
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
      # Add or replace database setup steps here
      - name: Set up database schema
        run: bin/rails db:schema:load
      # Add or replace test runners here
      - name: Run tests
        run: bin/rake

  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
        with:
          bundler-cache: true
      # Add or replace any other lints here
      - name: Security audit dependencies
        run: bin/bundler-audit --update
      - name: Security audit application code
        run: bin/brakeman -q -w2
      - name: Lint Ruby files
        run: bin/rubocop --parallel

When I run it, I get this error during the "Install Ruby and Gems" section. It's extremely long, confusing, and full of stuff I don't think is relevant. It seems to have to do with nokogiri (I've never had nokogiri work without some intense messing about):

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/runner/work/admin-tool/admin-tool/vendor/bundle/ruby/3.0.0/gems/nokogiri-1.16.4/ext/nokogiri
/opt/hostedtoolcache/Ruby/3.0.5/x64/bin/ruby -I
/opt/hostedtoolcache/Ruby/3.0.5/x64/lib/ruby/3.0.0 -r
./siteconf20240508-1911-jrafce.rb extconf.rb --use-system-libraries
...
checking for xsltParseStylesheetDoc() in -lxslt... no
checking for xsltParseStylesheetDoc() in -llibxslt... no
checking for libxslt using `pkg_config`... no
Please install either the `pkg-config` utility or the `pkg-config` rubygem.
checking for xsltParseStylesheetDoc() in -lxslt... no
checking for xsltParseStylesheetDoc() in -llibxslt... no
-----
extconf.rb:295:in `ensure_package_configuration'
extconf.rb:711:in `<main>'
xslt is missing. Please locate mkmf.log to investigate how it is failing.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.
...
To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/home/runner/work/admin-tool/admin-tool/vendor/bundle/ruby/3.0.0/extensions/x86_64-linux/3.0.0/nokogiri-1.16.4/mkmf.log

extconf failed, exit code 1
...
An error occurred while installing nokogiri (1.16.4), and Bundler cannot
continue.

This is extremely confusing, because earlier in the workflow, I specifically put sudo gem install nokogiri -v 1.16.4 and that worked, saying the gem had been installed:

Successfully installed nokogiri-1.16.4-x86_64-linux
Parsing documentation for nokogiri-1.16.4-x86_64-linux
Installing ri documentation for nokogiri-1.16.4-x86_64-linux
Done installing documentation for nokogiri after 2 seconds
1 gem installed

I cannot look at the files it tells me to (because it's all wrapped up in this workflow), so I'm not really sure where to start with a solution.

However, even if I did know what options were needed to use to get nokogiri going, I cannot seem to put them in the workflow file in a way that will be recognized. The Ruby setup section is a monolith; I don't know how to run custom stuff inside of it.


Solution

  • The error message indicates that the nokogiri gem is failing to install due to a missing dependency, specifically the xslt library. This is required for nokogiri to function correctly. To resolve this issue, you need to install the xslt library on your GitHub Actions environment. You can do this by adding a new step in your workflow file, specifically before the Install Ruby and gems step.

    Here's an updated version of your workflow file with the additional step:

       name: "Ruby on Rails CI"
        on:
          push:
            branches: [ "master" ]
          pull_request:
            branches: [ "master" ]
        jobs:
          test:
            runs-on: ubuntu-latest
            services:
              postgres:
                image: postgres:11-alpine
                ports:
                  - "5432:5432"
                env:
                  POSTGRES_DB: rails_test
                  POSTGRES_USER: rails
                  POSTGRES_PASSWORD: password
            env:
              RAILS_ENV: test
              DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test"
            steps:
              - name: Checkout code
                uses: actions/checkout@v4
              - name: Install xslt library
                run: sudo apt-get update && sudo apt-get install -y libxslt-dev
              - name: Install Nokogiri alone
                run: sudo gem install nokogiri -v 1.16.4
              # Add or replace dependency steps here
              - name: Install Ruby and gems
                uses: ruby/setup-ruby@v1
                with:
                  bundler-cache: true
              # ... rest of your workflow file ...
    

    This additional step installs the libxslt-dev package, which provides the necessary xslt library for nokogiri to function correctly. As for customizing the ruby/setup-ruby step, you can pass additional configuration options using the with keyword. For example, you can specify additional gem installation options like this:

     - name: Install Ruby and gems
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
          gem-install-options: "-- --use-system-libraries"
    

    This passes the --use-system-libraries option to the gem install command, which may help resolve any issues with nokogiri installation. I hope this helps!