如何修改 Netlify 的 Ruby 版本(自定义变量)

近期因为在 macOS 上升级了 Ruby 版本,发现本地项目不在 Gemfile 中指定版本也可以正常构建。然后保留的了 .ruby-version 文件,并且将版本改成了macOS对应版本(避免以后每次升级都去修改文件)。不料 Netlify 却无法正确部署。提示 ruby 未找到/opt/buildhome/.rvm/gems/ruby-3.3.5/bin/bundle: 6: exec: /opt/buildhome/.rvm/gems/ruby-3.3.5/bin/ruby: not found

为了将修改降到最小,尝试修改 Netlify 变量,修改后问题得到解决。

不想改动项目文件的原因是,本地构建正常、Gitlab构建正常,怕改动后其他地方又出问题,因为会同时在 Windows 和 macOS 中使用,怕下次某端升级 ruby 或者 ruby 环境发生改变又要修改项目文件。

添加环境变量

Netlify 添加环境变量

方式一:直达链接(将项目名称改成你的项目的名称):https://app.netlify.com/sites/项目名称/configuration/env#RUBY_VERSION,然后接着图中第三步继续。

方式二:按照图中步骤操作

  1. 点击左侧菜单中「Site configuration」选项
  2. 选择子菜单「Environment variables」选项
  3. 点击右上角「Add a variable」选项
  4. 选择「Add a single variable」选项

设置 Ruby 版本

Netlify 设置 Ruby 版本

打开「Add a variable」界面后,按照图中步骤操作

  1. 「key」输入框输入: RUBY_VERSION
  2. 「Scopes」选择:All scopes
  3. 「Values」输入框输入:3.3.5
    1. 这个值可以修改,根据自己需要调整

重新 Deploys

Netlify 重新 Deploys

完成添加Ruby版本环境变量后,按照图中步骤操作重新部署

  1. 左侧导航点击「Deploys」选项
  2. 点击右上角「Trigger deploy」选项
  3. 选择「Clear cache and deploy site」选项

直达链接(将项目名称改成你的项目的名称):https://app.netlify.com/sites/项目名称/deploys

遇到的相关报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
12:33:09 PM: build-image version: ecdc8b770f4a0193fd3f258c1bc6029e681813a4 (focal)
12:33:09 PM: buildbot version: 7f71d4a04b466d01951f8f6e47bdfedeb2f4f92f
12:33:09 PM: Fetching cached dependencies
12:33:10 PM: Starting to download cache of 171.0MB
12:33:11 PM: Finished downloading cache in 1.357s
12:33:11 PM: Starting to extract cache
12:33:12 PM: Finished extracting cache in 1.235s
12:33:12 PM: Finished fetching cache in 2.64s
12:33:12 PM: Starting to prepare the repo for build
12:33:13 PM: Preparing Git Reference refs/heads/main
12:33:15 PM: Custom build path detected. Proceeding with the specified path: ''
12:33:16 PM: Starting to install dependencies
12:33:16 PM: Python version set to 3.8
12:33:16 PM: Required ruby-3.3.5 is not installed.
12:33:16 PM: To install do: 'rvm install "ruby-3.3.5"'
12:33:16 PM: Attempting Ruby version 3.3.5, read from .ruby-version file
12:33:16 PM: Started restoring cached Ruby version
12:33:16 PM: Finished restoring cached Ruby version
12:33:17 PM: Using Ruby version 3.3.5
12:33:17 PM: Using Bundler version 2.5.21 from Gemfile.lock
12:33:18 PM: Failed during stage 'Install dependencies': dependency_installation script returned non-zero exit code: 1
12:33:18 PM: Successfully installed bundler-2.5.21
12:33:18 PM: 1 gem installed
12:33:18 PM: Started restoring cached ruby gems
12:33:18 PM: Finished restoring cached ruby gems
12:33:18 PM: Installing gem bundle
12:33:18 PM: /opt/buildhome/.rvm/gems/ruby-3.3.5/bin/bundle: 6: exec: /opt/buildhome/.rvm/gems/ruby-3.3.5/bin/ruby: not found
12:33:18 PM: Error during gem install
12:33:18 PM: Failing build: Failed to install dependencies

相关链接

构建环境变量: https://docs.netlify.com/configure-builds/environment-variables/#netlify-configuration-variables

管理构建依赖项: https://docs.netlify.com/configure-builds/manage-dependencies/#ruby-dependencies

Relay Tips: 一极乐https://yijile.com/zh/how-modify-ruby-version-of-netlify/