Github工作流程用于Ruby on Rails – 错误#127。rubocop:找不到命令

huangapple go评论61阅读模式
英文:

Github workflow for Ruby on Rails - Error #127. rubocop: command not found

问题

我正在了解工作流程/操作/持续集成,并试图理解GitHub Actions的工作原理。

我在GitHub Actions上有这个工作流程(我选择了GitHub上可用的一个)用于Ruby on Rails。

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@v3
      # Add or replace dependency steps here
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
        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/rails test:system

  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - 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: Lint Ruby files
        run: rubocop --parallel

在lint Ruby文件步骤中,我收到以下错误。

Run rubocop --parallel
  rubocop --parallel
  shell: /usr/bin/bash -e {0}
/home/runner/work/_temp/ba9b7df9-2b25-4f55-acc3-8349cc07d754.sh: line 1: rubocop: command not found
Error: Process completed with exit code 127.

我的Gemfile已经更新了rubocop相关的gem。我也已经运行了bundle来安装它们。

gem 'rubocop', require: false
gem 'rubocop-rails', require: false

我尝试将测试/检查作业中的rubocop --parallel一行更改为bundle exec rubocop --parallel,但收到了与另一个gem rubocop-discourse 相关的错误。

当我在本地使用相同的命令时,一切正常。

经过一些测试,从lint步骤中删除了bundler-cache: true这一行后,问题解决了。我不知道为什么,但我仍然想要了解为什么。此外,我在安装步骤中只保留了rubocoprubocop-rails

lint:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v3
    - uses: ruby/setup-ruby@v1
      with:
        ruby-version: 3.1.2
    - run: gem install rubocop rubocop-rails
    - name: Rubocop Lint
      run: rubocop --parallel
英文:

I'm reading about workflow/actions/CI and trying to understand how github actions work.

I have this workflow on github actions (I chose one available on Github) for Ruby on Rails.

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@v3
      # Add or replace dependency steps here
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
        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/rails test:system

  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - 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: Lint Ruby files
      	run: rubocop --parallel

And I'm receiving this error during the lint ruby files step.

Run rubocop --parallel
  rubocop --parallel
  shell: /usr/bin/bash -e {0}
/home/runner/work/_temp/ba9b7df9-2b25-4f55-acc3-8349cc07d754.sh: line 1: rubocop: command not found
Error: Process completed with exit code 127.

My gemfile it's updated with rubocop gems. Also already run a bundle to install.

gem 'rubocop', require: false
gem 'rubocop-rails', require: false

I did try changing in the test/lint job the line rubocop --parallel to bundle exec rubocop --parallel, but received another error related to another gem rubocop-discourse.

Everything is normal locally when I use the same commands.

After some testing, removing the line bundler-cache: true from the lint steps made it work. I don't know why, and I still want to understand why. Also, I kept only the rubocop and rubocop-rails in the install.

lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.1.2
      - run: gem install rubocop rubocop-rails
      - name: Rubocop Lint
        run: rubocop --parallel

答案1

得分: 1

在GitHub runner中,该工作流如下:

name: "rubocop lint"
on: workflow_dispatch
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
        with:
          ruby-version: 3.1.2
          bundler-cache: true
      - run: echo $PATH | tr ':' '\n'
      - run: bundle info rubocop
      - run: gem which rubocop
      - name: Lint Ruby files
        run: rubocop --parallel

与Gemfile一起,如下:

source "https://rubygems.org"
gem 'rubocop', require: false
gem 'rubocop-rails', require: false

将显示gem二进制文件添加到路径/opt/hostedtoolcache/Ruby/3.1.2/x64/bin,但是操作中运行的bundle install会将gems安装到供应商路径:

/home/runner/work/<repo>/<repo>/vendor/bundle/ruby/3.1.0/gems/rubocop-1.52.1

通常可以配置此路径,但在操作中将其硬编码为$pwd/vendor/bundle在此处,因此使用缓存意味着您必须使用bundle exec

Bundler知道这些gems及其二进制文件的安装位置,但由于它们安装在供应商路径中,它们不在系统范围的gem二进制文件路径/opt/hostedtoolcache/Ruby/3.1.2/x64/bin中。

删除bundler-cache: true并添加以下步骤- run: gem install rubocop rubocop-rails意味着gems将安装到系统路径,即它们的二进制文件可在ruby bin路径中使用。

就为什么其中一个可行而另一个不可行的原因而言,就是这样。gem install正在安装到gem主目录,而操作正在将安装位置设置为供应商文件夹,而这不在路径中。

如果bundle exec rubocop出现不同的错误,那可能与项目的其他组件相关——bundle exec将是通过bundler安装的可执行脚本的规范调用方式。

英文:

In the GitHub runner, the workflow;

name: &quot;rubocop lint&quot;
on: workflow_dispatch
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
        with:
          ruby-version: 3.1.2
          bundler-cache: true
      - run: echo $PATH | tr &#39;:&#39; &#39;\n&#39;
      - run: bundle info rubocop
      - run: gem which rubocop
      - name: Lint Ruby files
        run: rubocop --parallel

with the Gemfile

source &quot;https://rubygems.org&quot;
gem &#39;rubocop&#39;, require: false
gem &#39;rubocop-rails&#39;, require: false

will show that gem binaries are added to the path at /opt/hostedtoolcache/Ruby/3.1.2/x64/bin, but that the bundle install run inside the action installed the gems to the vendor path;

/home/runner/work/&lt;repo&gt;/&lt;repo&gt;/vendor/bundle/ruby/3.1.0/gems/rubocop-1.52.1

This path can usually be configured, but it's being hard set in the action, here, to $pwd/vendor/bundle. So using the cache means you'll have to use bundle exec.

Bundler knows where these gems and their binaries are installed at, but because they are being installed in the vendor path, they are not in the system wide gem binaries /opt/hostedtoolcache/Ruby/3.1.2/x64/bin

Removing bundler-cache: true and adding the step - run: gem install rubocop rubocop-rails means the gems will be installed at the system path, i.e. their binaries are available at the ruby bin path.

In terms of understanding why one works and the other doesn't, that's why. gem install is installing to the gem home, and the action is setting the installation location to the vendor folder, which is not in the path.

If bundle exec rubocop is giving a different error, that'd be related to some other component of your project -- bundle exec would be the canonical way of invoking the executable script installed by bundler

huangapple
  • 本文由 发表于 2023年6月16日 08:20:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定