Gitlab CI 设置错误 - 找不到 JavaScript 运行时

Gitlab CI setup error - Could not find a JavaScript runtime

我正在尝试为 Rails 项目上的 Ruby 设置 Gitlab CI,但是 运行 出现了一个我不知道如何修复的错误。

该应用程序 运行 Ruby 2.3.1,Rails 5.0,并且正在使用 postgreSQL 数据库。

我当前的 .gitlab.ci.yml 文件如下所示:

# https://hub.docker.com/r/library/ruby/tags/
image: "ruby:2.3.0"

# Pick zero or more services to be used on all builds.
# Only needed when using a docker container to run your tests in.
# Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-service
services:
  - redis:latest
  - postgres:latest
  - node:latest

# This is a basic example for a gem or script which doesn't use
# services such as redis or postgres
before_script:
  - gem install bundler         # Bundler is not installed with the image
  - bundle install -j $(nproc)  # Install dependencies

rubocop:
  script:
  - rubocop

rspec:
  script:
  - rspec spec

rails:
  script:
  - rails db:migrate
  - rspec spec

所以它应该使用 NodeJS 作为运行时。但是,我收到以下错误:

$ rails db:migrate
rails aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'uglifier'.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:293:in `require'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:293:in `block in require'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:259:in `load_dependency'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:293:in `require'
/usr/local/bundle/gems/uglifier-3.0.0/lib/uglifier.rb:5:in `<top (required)>'

您需要有一个 javascript 运行时(例如 nodejs)。您可以使用以下命令安装它:

对于 Ubuntu 用户

sudo apt-get install nodejs

对于 CentOS/RedHat 用户

sudo yum install nodejs

如果您无法安装 nodejs,您可以安装然后使用 therubyracer gem。

来自存储库的描述:

Embed the V8 JavaScript interpreter into Ruby.

将此行添加到您的 Gemfile

gem 'therubyracer', platforms: :ruby

Gitlab-CI 可以用文件 .gitlab-ci.yml 配置,在这种情况下:

before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs

可用于在安装阶段安装 nodejs (source)。

一个完整的 before_script 与 jekyll 可以看起来像下面的代码(例如,这是 jekyll-minifier 所必需的):

image: ruby:latest

variables:
  JEKYLL_ENV: production
  LC_ALL: C.UTF-8

before_script:
  - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
  - ruby -v
  - which ruby
  - gem install bundler
  - bundle install

test:
  stage: test
  script:
  - bundle exec jekyll build -d test
  artifacts:
    paths:
    - test
  except:
  - master

pages:
  stage: deploy
  script:
  - bundle exec jekyll build -d public
  artifacts:
    paths:
    - public
  only:
  - master