Last active
January 7, 2025 06:25
-
-
Save alexford/66a6cf5ed0b870af948594c1b9395f19 to your computer and use it in GitHub Desktop.
Running Rails tests with RSpec on GitHub actions with Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI | |
on: [push] | |
jobs: | |
rspec: | |
runs-on: ubuntu-latest | |
container: | |
image: ruby:2.6.5 | |
services: | |
postgres: | |
image: postgres:10.8 | |
env: | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: "" | |
POSTGRES_DB: postgres | |
ports: | |
- 5432:5432 | |
# needed because the postgres container does not provide a healthcheck | |
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 | |
steps: | |
- uses: actions/checkout@v1 | |
- uses: actions/setup-node@v1 | |
with: | |
node-version: '10.x' | |
registry-url: 'https://registry.npmjs.org' | |
- name: Gem cache | |
id: cache-bundle | |
uses: actions/cache@v1 | |
with: | |
path: vendor/bundle | |
key: bundle-${{ hashFiles('**/Gemfile.lock') }} | |
- name: Bundle install | |
env: | |
RAILS_ENV: test | |
run: | | |
gem install bundler | |
bundle install --jobs 4 --retry 3 --path vendor/bundle | |
- name: Install yarn | |
run: npm install -g yarn | |
# NOTE: Yarn cache doesn't seem to be working quite yet | |
- name: Get yarn cache dir | |
id: yarn-cache-dir | |
run: echo "::set-output name=dir::$(yarn cache dir)" | |
- name: Yarn cache | |
id: cache-yarn | |
uses: actions/cache@v1 | |
with: | |
path: ${{ steps.yarn-cache-dir.outputs.dir }} | |
key: yarn-${{ hashFiles('**/yarn.lock') }} | |
- name: Yarn install | |
run: yarn install | |
- name: Setup DB, Run tests | |
env: | |
PGHOST: postgres | |
PGUSER: postgres | |
PGPORT: ${{ job.services.postgres.ports[5432] }} | |
RAILS_ENV: test | |
run: | | |
bin/rails db:create db:schema:load | |
bundle exec rspec |
Thanks!
# needed because the postgres container does not provide a healthcheck options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
This is one of the things that was missing for me, thank you! 🎉
The other was this:
# Allows connections without a password. Fine since this is just for unit tests.
POSTGRES_HOST_AUTH_METHOD: trust
Which I added right below the empty-string password
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
they nowadays have bundler cache for you 💪