From ba31ef9eda9455e3d889a3189cefb4350e1a46e3 Mon Sep 17 00:00:00 2001 From: FlandreDaisuki Date: Tue, 22 Oct 2024 11:37:57 +0800 Subject: [PATCH] ci: setup database --- .github/workflows/ci.yaml | 38 ++++++++++++++++++++++++++++++++++++++ config/database.ci.yml | 10 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 config/database.ci.yml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1be0d28..b99019f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -39,16 +39,54 @@ jobs: rspec_test: runs-on: ubuntu-latest + + # ref: https://docs.github.com/en/actions/use-cases-and-examples/using-containerized-services/creating-postgresql-service-containers + services: + # Label used to access the service container + postgres: + # Docker Hub image + image: postgres:14 + # Provide the password for postgres + env: + POSTGRES_PASSWORD: rails_github_actions_pa55w0rd + POSTGRES_USER: rails_github_actions + POSTGRES_DB: rails_github_actions_test + ports: + - 5432:5432 + # Set health checks to wait until postgres has started + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: - name: Checkout code uses: actions/checkout@v4 + - name: Install dependencies + run: | + sudo apt-get -yqq install libpq-dev + - name: Set up Ruby uses: ruby/setup-ruby@v1 with: ruby-version: .ruby-version bundler-cache: true + # ref: https://blog.niclin.tw/2019/10/31/building-a-rails-ci-piepline-and-run-rspec-on-github-actions/ + - name: Setup test database + env: + RAILS_ENV: test + PG_USERNAME: rails_github_actions + PG_PASSWORD: rails_github_actions_pa55w0rd + PG_HOST: localhost + PG_PORT: 5432 + PG_DATABASE: rails_github_actions_test + run: | + cp config/database.ci.yml config/database.yml + bundle exec rake db:create db:migrate + - name: Run rspec env: RAILS_ENV: test diff --git a/config/database.ci.yml b/config/database.ci.yml new file mode 100644 index 0000000..697cd7e --- /dev/null +++ b/config/database.ci.yml @@ -0,0 +1,10 @@ +# ref: https://blog.niclin.tw/2019/10/31/building-a-rails-ci-piepline-and-run-rspec-on-github-actions/ +test: + adapter: postgresql + encoding: unicode + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + username: <%= ENV['PG_USERNAME'] %> + password: <%= ENV['PG_PASSWORD'] %> + host: <%= ENV['PG_HOST'] %> + port: <%= ENV['PG_PORT'] %> + database: <%= ENV['PG_DATABASE'] %>