I have defined a secondary external database in my Rails application for read-only purposes. Thus for testing, I setup a local database and plan to mock data within my test examples. Connecting to the database and running tests locally work great. However, when running the CI tests, the secondary database fails to setup due to the following error:
I believe this to be a configuration setup issue within the ci.yml
file, and am not sure how to configure this properly.
# ci.yml
name: Continuous Integration
on:
pull_request:
branches: [ main ]
jobs:
test:
name: Testing
runs-on: ubuntu-latest
services:
postgres:
image: postgres:12
ports:
- "5432:5432"
env:
POSTGRES_USER: rails
POSTGRES_PASSWORD: password
env:
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Chromedriver
uses: nanasess/setup-chromedriver@v1
# with:
# Optional: do not specify to match Chrome's version
# chromedriver-version: '88.0.4324.96'
# Add or replace dependency steps here
- name: Install Ruby and gems
uses: ruby/setup-ruby@1a68550f2e3309e13c8ccb91ac6b8786f59ee147
with:
bundler-cache: true
# Add or replace database setup steps here
- name: Set up primary database
env:
POSTGRES_DB: calendarize_test
DATABASE_URL: "postgres://rails:password@localhost:5432/calendarize_test"
run: bin/rails db:create:primary db:migrate:primary
- name: Set up warehouse database
env:
POSTGRES_DB: warehouse_test
DATABASE_URL: "postgres://rails:password@localhost:5432/warehouse_test"
run: bin/rails db:create:warehouse db:migrate:warehouse
# Add or replace test runners here
- name: Start Chromedriver
run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub --disable-dev-shm-usage &
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
- name: Run tests
run: bundle exec rspec --color
# database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
calendarize: &calendarize
<<: *default
host: localhost
username: <%= ENV["CALENDARIZE_DATABASE_USERNAME"] %>
password: <%= ENV["CALENDARIZE_DATABASE_PASSWORD"] %>
test:
primary:
<<: *calendarize
database: calendarize_test
warehouse:
<<: *calendarize
database: warehouse_test
migrations_paths: db/warehouse_migrate
development:
primary:
<<: *calendarize
database: calendarize_development
warehouse:
<<: *calendarize
database: warehouse_development
migrations_paths: db/warehouse_migrate
production:
primary:
<<: *calendarize
database: <%= ENV["CALENDARIZE_DATABASE_NAME"] %>
warehouse:
<<: *default
url: <%= ENV["WAREHOUSE_DATABASE_URL"] %>
database_tasks: false
The problem was solved by matching the username/password fields between both ci.yml
and database.yml
.
# database.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
test:
primary:
<<: *default
database: calendarize_test
username: postgres
password: postgres
warehouse:
<<: *default
database: warehouse_test
username: postgres
password: postgres
migrations_paths: db/warehouse_migrate
development:
primary:
<<: *default
database: calendarize_development
warehouse:
<<: *default
database: warehouse_development
migrations_paths: db/warehouse_migrate
production:
primary:
<<: *default
database: <%= ENV["CALENDARIZE_DATABASE_NAME"] %>
username: <%= ENV["CALENDARIZE_DATABASE_USERNAME"] %>
password: <%= ENV["CALENDARIZE_DATABASE_PASSWORD"] %>
warehouse:
<<: *default
url: <%= ENV["WAREHOUSE_DATABASE_URL"] %>
database_tasks: false
# ci.yml
name: Continuous Integration
on:
pull_request:
branches: [ main ]
jobs:
test:
name: Testing
runs-on: ubuntu-latest
services:
postgres:
image: postgres
ports:
- "5432:5432"
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Chromedriver
uses: nanasess/setup-chromedriver@v1
# with:
# Optional: do not specify to match Chrome's version
# chromedriver-version: '88.0.4324.96'
# Add or replace dependency steps here
- name: Install Ruby and gems
uses: ruby/setup-ruby@1a68550f2e3309e13c8ccb91ac6b8786f59ee147
with:
bundler-cache: true
# Add or replace database setup steps here
- name: Set up primary database
env:
POSTGRES_DB: calendarize_test
DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/calendarize_test"
run: bin/rails db:create:primary db:migrate:primary
- name: Set up warehouse database
env:
POSTGRES_DB: warehouse_test
DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/warehouse_test"
run: bin/rails db:create:warehouse db:migrate:warehouse
# Add or replace test runners here
- name: Start Chromedriver
run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub --disable-dev-shm-usage &
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
- name: Run tests
env:
PG_USER: postgres
PG_PASSWORD: postgres
run: bundle exec rspec --color