ruby-on-rails Rails管道不拾取环境变量

nuypyhwy  于 2023-06-25  发布在  Ruby
关注(0)|答案(1)|浏览(131)

我的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

但这会导致同样的错误...我错过了什么?

jecbmhm3

jecbmhm31#

问题出在adapter设置上。

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

根据文档将其更改为postgresql

default: &default
  adapter: postgresql 
  encoding: unicode

此外,你似乎有很多DB-*变量没有正确使用/设置,修复它。

相关问题