为什么 GitHub Actions 找不到 rubocop 和 httparty gems?

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

我正在尝试开发一个 Ruby 脚本,该脚本使用几个类来执行一些 API 调用,这些类在单独目录中的另一个 rb 文件中使用

httparty
gem,如下所示:

./script.rb
./lib
./lib/calls.rb

代码实际上更广泛,但为了简单起见,这里是一个极简的描述:

脚本.rb

#!/usr/bin/env ruby
require_relative 'lib/calls'

service = Service.new
response = service.get
puts response

lib/calls.rb

require 'httparty'

class Service
  include HTTParty
  base_uri 'https://api.service.com/v1/endpoint'

  def get
    self.class.get('/')
  end
end

效果很好!现在我尝试使用 GitHub Actions 运行它,但收到此错误:

<internal:/opt/hostedtoolcache/Ruby/3.2.2/x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require': cannot load such file -- httparty (LoadError)
    from <internal:/opt/hostedtoolcache/Ruby/3.2.2/x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from /home/runner/work/test/test/lib/calls.rb:1:in `<top (required)>'
    from ./script.rb:2:in `require_relative'
    from ./script.rb:2:in `<main>'

我在某处读过有关如果您使用带有大写字母的

require 'HTTParty
会发生此问题的内容,但我使用的是全小写字母,所以不是这样。

我认为可能与之相关的另一件有趣的事情是,如果我尝试直接在 GitHub Actions 的命令行中运行

rubocop
,我会得到以下结果:

Run rubocop --parallel
/home/runner/work/_temp/b235a1be-84fe-430b-91fb-4118542d46a3.sh: line 1: rubocop: command not found
Error: Process completed with exit code 127.

当然,

rubocop
httparty
都已安装,如果我使用
rubocop
bundle exec rubocop
就会运行,但在本地我可以只使用
rubocop
来运行它。

编辑:来自 GitHub Actions 的 yaml 文件并确认 gems 已安装:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.2.2
          bundler-cache: true
      - name: Script
        run: ./script.rb
Run bundler install
...
Using httparty 0.21.0
...
Using rubocop 1.50.2
...
```
ruby github-actions httparty rubocop
2个回答
0
投票

在 GitHub Actions 中运行命令之前使用

bundle exec
修复了该问题。

 - name: Script
        run: bundle exec script.rb

0
投票

解决办法是设置

bundler-cache: false


我今天也遇到了同样的问题,跟着Google来到这里。

不幸的是,阿尔瓦罗问题的解决方案对我不起作用。但幸运的是,我想我可能找到了原因:问题出在设置上

bundler-cache: true.

我认为错误发生的原因如下:当Action使用

ruby/setup-ruby@v1
时,它实际上在内部执行了
bundle install
命令,并且Action缓存了本次安装的结果。然后,Alvaro 和我在下面再次运行
bundle install
,但这一次该命令只读取缓存的结果,实际上并没有安装任何东西。结果运行ruby run script.rb时出现缺少gem的错误。

所以,解决办法就是设置

bundler-cache: false

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