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

0sgqnhkj  于 2023-05-06  发布在  Ruby
关注(0)|答案(1)|浏览(120)

我正在尝试开发一个Ruby脚本,它使用几个类来执行一些API调用,这些类使用另一个rb文件中的httparty gem,该文件位于一个单独的目录中,如下所示:

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

代码实际上更广泛,但为了简单起见,这里是一个极简的描述:
script.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.

当然,rubocophttparty都已经安装,如果我使用bundle exec rubocop,则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
...
vawmfj5a

vawmfj5a1#

通过在GitHub Actions中运行命令之前使用bundle exec修复了它。

- name: Script
        run: bundle exec script.rb

相关问题