英文:
Rails pipeline not picking up environment variables
问题
我在我的GitLab流水线中遇到了一个问题,应该运行我的Ruby on Rails测试。在我的项目中,我有以下的.gitlab-ci.yml
文件:
stages:
- test
tests:
stage: test
image: ruby:3.2.2
services:
- postgres:15.1
variables:
POSTGRES_USER: rails
POSTGRES_PASSWORD: rails
DB_USERNAME: rails
DB_PASSWORD: rails
DB_HOST: postgres
DB_PORT: 5432
RAILS_ENV: test
DISABLE_SPRING: 1
BUNDLE_PATH: vendor/bundle
script:
- gem install bundler -v 2.4.14
- bundle install
- bundle exec rails db:migrate
- bundle exec rails db:create db:schema:load --trace
- bundle exec rails test
only:
- tags
但当我运行流水线时,我得到了以下错误消息:
$ bundle exec rails db:migrate
rails aborted!
LoadError: Could not load the 'localhost' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.
在我的config/database.yml
文件中,我已经将数据库设置为:
default: &default
adapter: <%= ENV['DB_CONNECTION'] || 'localhost' %>
encoding: unicode
pool: 5
host: <%= ENV['DB_HOST'] || 'localhost' %>
port: <%= ENV['DB_PORT'] || 5432 %>
username: <%= ENV['DB_USERNAME'] || 'postgres' %>
password: <%= ENV['DB_PASSWORD'] || 'postgres' %>
development:
<<: *default
database: <%= ENV['DB_DATABASE'] || 'database' %>
test:
<<: *default
database: <%= ENV['DB_DATABASE'] || 'database-test' %>
production:
<<: *default
database: <%= ENV['DB_DATABASE'] || 'database' %>
当我在本地启动这个项目然后运行rails test
时,我的测试会成功执行。为什么流水线环境会忽略我尝试传递的环境变量呢?
我还尝试创建了一个.env.pipeline
文件,并添加了:
before_script:
- cp .env.pipeline .env
但结果是相同的错误... 我在这里漏掉了什么?
英文:
I am having a problem with my GitLab pipeline which should run my Ruby on Rails tests. In my project I have the following .gitlab-ci.yml
file:
stages:
- test
tests:
stage: test
image: ruby:3.2.2
services:
- postgres:15.1
variables:
POSTGRES_USER: rails
POSTGRES_PASSWORD: rails
DB_USERNAME: rails
DB_PASSWORD: rails
DB_HOST: postgres
DB_PORT: 5432
RAILS_ENV: test
DISABLE_SPRING: 1
BUNDLE_PATH: vendor/bundle
script:
- gem install bundler -v 2.4.14
- bundle install
- bundle exec rails db:migrate
- bundle exec rails db:create db:schema:load --trace
- bundle exec rails test
only:
- tags
But then when I run the pipeline I get this error message:
$ bundle exec rails db:migrate
rails aborted!
LoadError: Could not load the 'localhost' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.
In my config/database.yml I have set the database settings to this:
default: &default
adapter: <%= ENV['DB_CONNECTION'] || 'localhost' %>
encoding: unicode
pool: 5
host: <%= ENV['DB_HOST'] || 'localhost' %>
port: <%= ENV['DB_PORT'] || 5432 %>
username: <%= ENV['DB_USERNAME'] || 'postgres' %>
password: <%= ENV['DB_PASSWORD'] || 'postgres' %>
development:
<<: *default
database: <%= ENV['DB_DATABASE'] || 'database' %>
test:
<<: *default
database: <%= ENV['DB_DATABASE'] || 'database-test' %>
production:
<<: *default
database: <%= ENV['DB_DATABASE'] || 'database' %>
When I start this project locally and then run rails test
, my tests are executed successfully. Why is the pipeline environment ignoring my environment variables that I am trying to pass?
I also tried creating a .env.pipeline
and added:
before_script:
- cp .env.pipeline .env
But that results in the same error... What am I missing here?
答案1
得分: 0
问题出在adapter
设置上。
LoadError: 无法加载 'localhost' Active Record 适配器。确保在config/database.yml中拼写适配器正确,并且已将所需的适配器 gem 添加到您的 Gemfile 中。
config/database.yml:
default: &default
adapter: <%= ENV['DB_CONNECTION'] || 'localhost' %> ## 错误的
encoding: unicode
根据文档,将其更改为 postgresql
。
default: &default
adapter: postgresql
encoding: unicode
此外,您似乎有很多未正确使用/设置的 DB-*
变量,也需要修复。
英文:
The problem is with the adapter
settings.
LoadError: Could not load the 'localhost' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.
config/database.yml:
default: &default
adapter: <%= ENV['DB_CONNECTION'] || 'localhost' %> ## incorrect
encoding: unicode
Change this to postgresql
as per the documentation
default: &default
adapter: postgresql
encoding: unicode
Also, you seem to have a lot of DB-*
variables that are not properly used/set, fix that as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论