I'm trying to develop a Ruby script that does some API calls using a couple of Classes that use the httparty gem in another rb file in a separate directory like this:
./script.rb
./lib
./lib/calls.rb
The code is actually more extensive but for the sake of simplicity here is a minimalist description:
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
That works great! Now I'm trying to run it using GitHub Actions and I get this error:
<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>'
I've read somewhere about this issue happening if you use require 'HTTParty with caps but I'm using it with all lowercase, so that's not it.
Another interesting thing that I believe might be related to is that if I try to run rubocop directly in the command line in GitHub Actions I get this:
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.
Both rubocop and httparty have been installed, of course, and rubocop runs if I use bundle exec rubocop, but locally I can run it using just rubocop.
EDIT: yaml file from GitHub Actions and confirmation that the gems were installed:
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
...
```
Fixed it by using
bundle execbefore running a command in GitHub Actions.