You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需执行编辑凭证命令,如何生成Rails环境密钥?

自动生成Rails production环境Credentials文件的方案

用无交互编辑器跳过编辑环节

直接给EDITOR指定一个会立即退出的命令,这样执行:edit命令时不会等待用户操作,自动生成所需文件:

EDITOR="true" rails credentials:edit --environment production

这个命令的核心是让Rails认为你完成了编辑操作——true命令执行后立刻返回成功状态,Rails就会自动生成production.key和production.yml.enc,其中加密文件里是默认的空配置结构。

手动生成密钥与加密文件(不依赖edit命令)

如果不想碰:edit命令,也可以分两步手动完成:

  1. 生成符合Rails要求的32位十六进制密钥,写入production.key:
# 在Rails模板里可以用rails runner执行
key = SecureRandom.hex(32)
File.write(Rails.root.join("config/credentials/production.key"), key)
  1. 创建空的配置内容并加密成production.yml.enc:
echo "" | rails credentials:encrypt --key config/credentials/production.key --output config/credentials/production.yml.enc

执行前记得先创建config/credentials目录:mkdir -p config/credentials。

集成到Rails应用模板里

把上述命令加到模板的after_bundle回调中即可实现自动化:

after_bundle do
  # 方式1:用无交互编辑器快速生成
  run "EDITOR='true' rails credentials:edit --environment production"

  # 方式2:手动生成(需先创建目录)
  # run "mkdir -p config/credentials"
  # run "rails runner \"key = SecureRandom.hex(32); File.write(Rails.root.join('config/credentials/production.key'), key)\""
  # run "echo '' | rails credentials:encrypt --key config/credentials/production.key --output config/credentials/production.yml.enc"
end

内容的提问来源于stack exchange,提问作者CWarrington

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:52:11