Since multiple databases is still a relatively fresh topic on Rails I wasn't able to find good resources on how to integrate them with my github actions. As long as I had single database in my app those configs we correct:
name: CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: root
POSTGRES_DB: db_test
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout#v1
- name: Set up Ruby 2.6
uses: actions/setup-ruby#v1
with:
ruby-version: 2.6.x
- name: Build and test with Rake
env:
PGHOST: 127.0.0.1
PGUSER: postgres
PGPASSWORD: root
RAILS_ENV: test
run: |
sudo apt-get -yqq install libpq-dev
gem install bundler
bundle install --jobs 4 --retry 3
bundle exec rake db:test:prepare
bundle exec rails test
However after implementing a second db I got this error:
TypeError: no implicit conversion of nil into String
/opt/hostedtoolcache/Ruby/2.6.6/x64/bin/bundle:23:in load' /opt/hostedtoolcache/Ruby/2.6.6/x64/bin/bundle:23:in ' Tasks:
TOP => db:test:load => db:test:purge (See full trace by running task
with --trace) Error: Process completed with exit code 1.
I'm almost 100% positive that this error is related to the lack of credentials for the second db in my github action file. How can I fix it and include both my dbs there?
Related
I'm trying to configure a ruby.yml with github-actions but I'm getting this error at Setup database
PG::ConnectionBad: could not translate host name "db" to address: Temporary failure in name resolution
my yml file looks like this
name: CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
steps:
- uses: actions/checkout#v2
- name: Setup Ruby
uses: ruby/setup-ruby#v1
with:
bundler-cache: true
- name: Install Dependencies
run: |
sudo apt install -yqq libpq-dev
gem install bundler
- name: Install Gems
run: |
bundle install
- name: Setup database
env:
PG_DATABASE: postgres
PG_HOST: localhost
PG_USER: postgres
PG_PASSWORD: password
RAILS_ENV: test
WITH_COVERAGE: true
DISABLE_SPRING: 1
run: |
bundle exec rails db:setup
- name: Run Tests
env:
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
run: |
bundle exec rake test
I tried following this tutorial https://dev.to/buildwithallan/how-to-set-up-a-ci-workflow-using-github-actions-4818 but nothing it still doesn’t work. I'm not sure how I can fix this, would appreciate some help
double check this segment:
env:
PG_DATABASE: postgres
PG_HOST: localhost
PG_USER: postgres
PG_PASSWORD: password
RAILS_ENV: test
WITH_COVERAGE: true
DISABLE_SPRING: 1
check db in your config file. seems github action complaints it can not connect to db
I'm trying to use parallel_tests in my github action to run my test suite but I was not able to find a proper solution.
The official docs has one but it is for gitlab:
https://github.com/grosser/parallel_tests/wiki/Distributed-Parallel-Tests-on-CI-systems
Any help would be appreciated thanks!
Here's a sample workflow you can drop into .github/workflows/tests.yml:
name: Rails Tests
on: push
env:
PGHOST: localhost
PGUSER: postgres
RAILS_ENV: test
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
# Set N number of parallel jobs you want to run
# Remember to update ci_node_index below to 0..N-1
ci_node_total: [6]
# set N-1 indexes for parallel jobs
# When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc
ci_node_index: [0, 1, 2, 3, 4, 5]
services:
postgres:
image: postgres:11.5
ports: ["5432:5432"]
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
redis:
image: redis:5
ports: ["6379:6379"]
steps:
- uses: actions/checkout#v1
- uses: ruby/setup-ruby#v1
with:
bundler-cache: true
- name: Set node version (from .tool-versions)
run: echo "NODE_VERSION=$(cat .tool-versions | grep nodejs | sed 's/^nodejs //')" >> $GITHUB_ENV
- uses: actions/setup-node#v2
with:
node-version: ${{ env.NODE_VERSION }}
- uses: bahmutov/npm-install#v1
- name: Install PostgreSQL client
run: |
sudo apt-get -yqq install libpq-dev postgresql-client
- name: Test Prep
env:
CI_NODE_INDEX: ${{ matrix.ci_node_index }}
run: |
bundle exec rake parallel:create["1"] parallel:load_schema["1"]
- name: Run tests
env:
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
CI_NODE_INDEX: ${{ matrix.ci_node_index }}
run : |
bundle exec parallel_test spec/ -n $CI_NODE_TOTAL --only-group $CI_NODE_INDEX --type rspec
This was what I used to get it solved. Note: There are some other omitted parts such as the setup for ruby, postgresql, sqlite, etc.
name: "Lint and Test"
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
services:
redis:
image: redis
ports: ["6379:6379"]
postgres:
ports: ["5432:5432"]
steps:
//omitted setups
- name: Setup Parallel Database
env:
RAILS_ENV: test
PGHOST: localhost
PGUSER: postgres
run: |
cp config/database.yml.example config/database.yml
bundle exec rake parallel:create
bundle exec rake parallel:rake[db:schema:load] || true
- name: Build and test with rspec
env:
RAILS_ENV: test
PGHOST: localhost
PGUSER: postgres
APP_REDIS_URL: redis://localhost:6379
MINIMUM_COVERAGE: 80
run: |
bundle exec parallel_rspec --verbose spec
I'm having this problem when I'm trying to run rspec on Github actions. This is my github action workflow.
name: Ruby
on:
push:
branches: [ master]
pull_request:
branches: [ master]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout#v2
- name: Setup System
run: |
sudo apt-get install sqlite3 libsqlite3-dev
- name: Set up Ruby 2.7.2
uses: actions/setup-ruby#v1
with:
ruby-version: 2.7.2
- name: Cache Ruby Gems
uses: actions/cache#v2
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Bundle Install
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3 --without=production
- name: Setup DB
env:
RAILS_ENV: test
DISABLE_SPRING: 1
run: |
bin/rails db:setup db:migrate db:seed
- name: Run rspec
env:
RAILS_ENV: test
DISABLE_SPRING: 1
run: bundle exec rspec
My code is calling insert_all.
I was able to track the error down to https://github.com/rails/rails/blob/main/activerecord/lib/active_record/insert_all.rb#L97
From what I can see, it has nothing to do with the data being insert but the connection itself
For some reason the (sqlite3) connection doesn't support skipping duplicates
I do not get this error on my local macbook air
tl;dr - I was using an old version of SQLite3
Okay, tracing the rail's code, I find that for the sqlite3 adapter it calls
https://github.com/rails/rails/blob/da418dc2508eb8af94ba88b70034021cd17b1abd/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L152
which calls
https://github.com/rails/rails/blob/da418dc2508eb8af94ba88b70034021cd17b1abd/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L149
which specifies that sqlite3 must be version 3.24.0 or great.
At the same time, Github's ubuntu-latest currently (02-2021) uses Ubuntu 18 as "latest".
And the sqlite3 package on Ubuntu installs sqlite 3.22 by default. source: https://packages.ubuntu.com/search?keywords=sqlite3
So after changing to runs-on: ubuntu-20.04 I no longer get this problem!
This is my very first question.
I'm trying to integrate github actions to my rails project.
I have my ruby.yml like this:
name: Ruby
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up Ruby
uses: ruby/setup-ruby#ec106b438a1ff6ff112340de34ddc62c540232e0
with:
ruby-version: 2.6.5
- name: Install dependencies
run: bundle install
- name: Run tests
run: bundle exec rake
The bundle step takes a lot of time, i don't know if i can just load the Gemfile.lock instead of running bundle everytime. But it runs ok.
The problem is the test step.
So here is the error:
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
/home/runner/work/project/project/test/test_helper.rb:10:in `<main>'
/home/runner/work/project/project/test/application_system_test_case.rb:1:in `<main>'
/home/runner/work/project/project/test/system/create_practice_test.rb:1:in `<main>'
/home/runner/work/GPLM/GPLM/bin/rails:9:in `<top (required)>'
/home/runner/work/GPLM/GPLM/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => test:system
(See full trace by running task with --trace)
Coverage report generated for Minitest to /home/runner/work/project/project/coverage. 31 / 3352 LOC (0.92%) covered.
SimpleCov failed with exit 1
##[error]Process completed with exit code 1.
Thank you everybody.
You need to add Postgres service to your GitHub Action config. For example:
services:
postgres:
image: postgres:11
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
ports:
- 5432:5432
And before bundle install you need to install dependent library:
- name: Install library for postgres
run: sudo apt-get install libpq-dev
Don't forget about setup database:
- name: Setup Database
run: |
cp config/YOUR_GITHUB_ACTIONS_DATABASE.yml config/database.yml
bundle exec rails db:setup
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
RAILS_ENV: test
Here, of course, you have to tailor the commands to your individual case.
The easiest way is to have a separate database configuration file specifically for GitHub Actions. But you can also have a special environment for CI.
I'm in a problem right now and don't know how to solve this in Github Actions context.
Side context: I'm following a tutorial on setting up Rails-Postgres with GH actions for CI.
Normally when I run into this problem in dev environment, I'd delete a postmaster.pid file. But here, because I'm in a test environment in GH actions, I'm not sure how to solve it. I tried already doing how I would do it in dev env, it unfortunately did not work.
The solutions I've tried:
rm /usr/local/var/postgres/postmaster.pid
Changing the ports within postgres entry in the workflow
My GH Actions Workflow:
name: Test
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:12.1
ports:
- 6543:6543
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout repository
uses: actions/checkout#v2
- name: Install Ruby version specified in `.ruby-version`
uses: eregon/use-ruby-action#master
- name: Install required apt packages
run: |
sudo apt-get -y install libpq-dev
- name: Setup cache key and directory for gems cache
uses: actions/cache#v1
with:
path: vendor/bundle
key: ${{ runner.os }}-gem-use-ruby-${{ hashFiles('**/Gemfile.lock') }}
- name: Read Node.js version to install from `.nvmrc`
run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
id: nvm
- name: Install required Node.js version
uses: actions/setup-node#v1
with:
node-version: "${{ steps.nvm.outputs.NVMRC }}"
- name: Get Yarn cache directory path
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Setup cache key and directory for node_modules cache
uses: actions/cache#v1
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- name: Bundle install
run: |
gem install bundler -v2.1.4
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- name: Yarn install
run: yarn --frozen-lockfile
- name: Run RSpec // ERRORS HERE
run: |
RUBYOPT='-W:no-deprecated -W:no-experimental' bundle exec rails db:prepare
RUBYOPT='-W:no-deprecated -W:no-experimental' bundle exec rspec
It breaks on the Run RSpec task.
Error message:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Thanks for your help everyone😁
Found a fix!
I needed to add the host, username, and password entry in config/database.yml for the test DB
Because you specified the port for 6543, so PostgreSQL cannot find its default port 5432.
To fix this, specify the port under
services:
postgres:
ports:
- 5432/tcp