Rails pipeline未获取到环境变量。

huangapple go评论54阅读模式
英文:

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 &#39;localhost&#39; Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you&#39;ve added the necessary adapter gem to your Gemfile.

In my config/database.yml I have set the database settings to this:

default: &amp;default
  adapter: &lt;%= ENV[&#39;DB_CONNECTION&#39;] || &#39;localhost&#39; %&gt;
  encoding: unicode
  pool: 5
  host: &lt;%= ENV[&#39;DB_HOST&#39;] || &#39;localhost&#39; %&gt;
  port: &lt;%= ENV[&#39;DB_PORT&#39;] || 5432 %&gt;
  username: &lt;%= ENV[&#39;DB_USERNAME&#39;] || &#39;postgres&#39; %&gt;
  password: &lt;%= ENV[&#39;DB_PASSWORD&#39;] || &#39;postgres&#39; %&gt;

development:
  &lt;&lt;: *default
  database: &lt;%= ENV[&#39;DB_DATABASE&#39;] || &#39;database&#39; %&gt;

test:
  &lt;&lt;: *default
  database: &lt;%= ENV[&#39;DB_DATABASE&#39;] || &#39;database-test&#39; %&gt;

production:
  &lt;&lt;: *default
  database: &lt;%= ENV[&#39;DB_DATABASE&#39;] || &#39;database&#39; %&gt;

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 &#39;localhost&#39; Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you&#39;ve added the necessary adapter gem to your Gemfile.

config/database.yml:

default: &amp;default
  adapter: &lt;%= ENV[&#39;DB_CONNECTION&#39;] || &#39;localhost&#39; %&gt; ## incorrect
  encoding: unicode

Change this to postgresql as per the documentation

default: &amp;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.

huangapple
  • 本文由 发表于 2023年6月22日 18:53:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定