Github工作流程设置-ruby动作,中间如何进行自定义gem安装

问题描述 投票:0回答:1

我有一个 Github 工作流程文件,如下所示:

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

当我运行它时,我在“安装 Ruby 和 Gems”部分中收到此错误。它非常长、令人困惑,而且充满了我认为不相关的内容。这似乎与 nokogiri 有关(我从来没有在没有进行过激烈的混乱的情况下进行过 nokogiri 工作):

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.

这非常令人困惑,因为在工作流程的早期,我专门放置了

sudo gem install nokogiri -v 1.16.4
并且有效,表示 gem 已安装:

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

我无法查看它告诉我的文件(因为它全部包含在这个工作流程中),所以我不太确定从哪里开始解决方案。

但是,即使我确实知道需要使用哪些选项来让 nokogiri 运行,我似乎也无法以可识别的方式将它们放入工作流程文件中。 ruby 设置部分是一个整体;我不知道如何在其中运行自定义内容。

ruby-on-rails github-actions nokogiri
1个回答
0
投票

错误消息表明 nokogiri gem 由于缺少依赖项(特别是 xslt 库)而无法安装。这是 nokogiri 正常运行所必需的。 要解决此问题,您需要在 GitHub Actions 环境中安装 xslt 库。您可以通过在工作流程文件中添加新步骤来完成此操作,特别是在“安装 Ruby 和 gems”步骤之前。

这是工作流程文件的更新版本,其中包含附加步骤:

   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 ...

此附加步骤将安装 libxslt-dev 软件包,该软件包为 nokogiri 正常运行提供必要的 xslt 库。 至于自定义 ruby/setup-ruby 步骤,您可以使用 with 关键字传递其他配置选项。例如,您可以指定其他 gem 安装选项,如下所示:

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

这会将 --use-system-libraries 选项传递给 gem install 命令,这可能有助于解决 nokogiri 安装的任何问题。 我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.