Skip to content

Commit

Permalink
Merge pull request #1 from Nasdaq/add-ci-tests
Browse files Browse the repository at this point in the history
Add CI pipeline
  • Loading branch information
mateuscruz authored Nov 30, 2024
2 parents 2c5c7f6 + d016b73 commit d7ffe30
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 9 deletions.
Empty file added .github/.keep
Empty file.
46 changes: 46 additions & 0 deletions .github/workflows/prebuild_postgres_primary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Build Postgres Primary Image

on:
pull_request:
paths:
- 'postgres_primary.dockerfile'

jobs:
export_variables:
runs-on: ubuntu-latest

outputs:
primary_image: ${{ steps.compute_container_registry_name.outputs.CR_NAME }}/postgres_primary:${{ steps.calculate_primary_sha.outputs.PRIMARY_SHA }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Compute container registry name
id: compute_container_registry_name
run: echo "CR_NAME=$(echo ghcr.io/${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for postgres_primary.dockerfile
id: calculate_primary_sha
run: echo "PRIMARY_SHA=$(sha256sum postgres_primary.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT

prebuild_primary:
needs: export_variables
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and tag primary Docker image
run: docker build -f postgres_primary.dockerfile -t ${{ needs.export_variables.outputs.primary_image }} .

- name: Push primary Docker image
run: docker push ${{ needs.export_variables.outputs.primary_image }}
46 changes: 46 additions & 0 deletions .github/workflows/prebuild_postgres_replica.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Build Postgres Replica Image

on:
pull_request:
paths:
- 'postgres_replica.dockerfile'

jobs:
export_variables:
runs-on: ubuntu-latest

outputs:
replica_image: ${{ steps.compute_container_registry_name.outputs.CR_NAME }}/postgres_replica:${{ steps.calculate_replica_sha.outputs.REPLICA_SHA }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Compute container registry name
id: compute_container_registry_name
run: echo "CR_NAME=$(echo ghcr.io/${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for postgres_replica.dockerfile
id: calculate_replica_sha
run: echo "REPLICA_SHA=$(sha256sum postgres_replica.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT

prebuild_replica:
needs: export_variables
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and tag replica Docker image
run: docker build -f postgres_replica.dockerfile -t ${{ needs.export_variables.outputs.replica_image }} .

- name: Push replica Docker image
run: docker push ${{ needs.export_variables.outputs.replica_image }}
176 changes: 176 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
name: Run Test Suite

on:
pull_request:
branches:
- main

jobs:
export_variables:
runs-on: ubuntu-latest

outputs:
primary_image: ${{ steps.compute_container_registry_name.outputs.CR_NAME }}/postgres_primary:${{ steps.calculate_primary_sha.outputs.PRIMARY_SHA }}
replica_image: ${{ steps.compute_container_registry_name.outputs.CR_NAME }}/postgres_replica:${{ steps.calculate_replica_sha.outputs.REPLICA_SHA }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Compute container registry name
id: compute_container_registry_name
run: echo "CR_NAME=$(echo ghcr.io/${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for postgres_primary.dockerfile
id: calculate_primary_sha
run: echo "PRIMARY_SHA=$(sha256sum postgres_primary.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT

- name: Calculate SHA256 for postgres_replica.dockerfile
id: calculate_replica_sha
run: echo "REPLICA_SHA=$(sha256sum postgres_replica.dockerfile | awk '{ print substr($1, 1, 12) }')" >> $GITHUB_OUTPUT

rubocop:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1.6

- name: Install dependencies
run: |
gem install bundler
bundle install
- name: Run RuboCop
run: bundle exec rubocop

rspec:
needs: [export_variables]
runs-on: ubuntu-latest

strategy:
matrix:
ruby:
- 3.2.6
- 3.3.6
rails:
- 7.0.0
- 7.1.0
- 8.0.0
include:
- ruby: 3.1.6
rails: 7.0.0
- ruby: 3.1.6
rails: 7.1.0

name: Ruby ${{ matrix.ruby }} / ActiveRecord ${{ matrix.rails }}
services:
postgres_primary:
image: ${{ needs.export_variables.outputs.primary_image }}
ports:
- 5432:5432
env:
POSTGRES_DB: postgres_primary_test
POSTGRES_USER: postgres_primary_test
POSTGRES_PASSWORD: postgres_primary_test
POSTGRES_HOST_AUTH_METHOD: "scram-sha-256\nhost replication all 0.0.0.0/0 md5"
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256"
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
postgres_replica:
image: ${{ needs.export_variables.outputs.replica_image }}
ports:
- 5433:5432
env:
PGUSER: replicator
PGPASSWORD: replicator
PGPORT: 5432
PRIMARY_DATABASE_HOST: postgres_primary
PRIMARY_DATABASE_PORT: 5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

- name: Check if primary is available
run: pg_isready -h localhost -U postgres_primary_test --dbname=postgres -p 5432

- name: Check if replica is available
# run: pg_isready -h localhost -U replicator --dbname=postgres -p 5433
run: pg_isready -h localhost -U postgres_primary_test --dbname=postgres -p 5433

- name: Install dependencies
env:
RAILS_VERSION: ${{ matrix.rails }}
run: |
gem install bundler
bundle install
- name: Run RSpec tests
env:
PG_PRIMARY_USER: postgres_primary_test
PG_PRIMARY_PASSWORD: postgres_primary_test
PG_PRIMARY_HOST: localhost
PG_PRIMARY_PORT: 5432
PG_REPLICA_USER: postgres_primary_test
PG_REPLICA_PASSWORD: postgres_primary_test
PG_REPLICA_HOST: localhost
PG_REPLICA_PORT: 5433

run: RUBY_VERSION="${{ matrix.ruby }}" RAILS_VERSION="${{ matrix.rails }}" bundle exec rspec --format progress

- name: Upload Coverage Report
uses: actions/upload-artifact@v4
with:
name: ${{ env.RUN_UNIQUE_ID }}_artifact_${{ matrix.ruby }}_${{ matrix.rails }}
path: coverage/
include-hidden-files: true
if-no-files-found: error

coverage_report:
needs: [rspec]
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.5

- name: Install Dependencies
run: |
gem install bundler
bundle install
- name: Download Partial Coverage Resultsets
uses: actions/download-artifact@v4
with:
path: coverage/

- name: Collate Partial Coverage Resultsets
run: bundle exec rake coverage:report

- uses: joshmfrankel/simplecov-check-action@main
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
16 changes: 9 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ gem "pg", "~> 1.5"

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"

gem "rubocop-rspec", "~> 3.1.0"
group :test do
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
gem "rubocop-rspec", "~> 3.1.0"
gem "simplecov"
gem "simplecov-cobertura"
end

if ENV["RAILS_VERSION"]
gem "activerecord", ENV["RAILS_VERSION"]
gem "activesupport", ENV["RAILS_VERSION"]
gem "activerecord", "~> #{ENV["RAILS_VERSION"]}"
gem "activesupport", "~> #{ENV["RAILS_VERSION"]}"
end

# Specify your gem's dependencies in active_record_proxy_adapters.gemspec
Expand Down
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ require "rubocop/rake_task"
RuboCop::RakeTask.new

task default: %i[spec rubocop]

namespace :coverage do
desc "Collates all result sets generated by the different test runners"
task :report do
require "simplecov"

SimpleCov.collate Dir["coverage/**/.resultset.json"] do
add_group "PostgreSQL" do |src_file|
[/postgresql/, /postgre_sql/].any? { |pattern| pattern.match?(src_file.filename) }
end
end
end
end
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ services:
app:
build:
args:
- RUBY_VERSION=${RUBY_VERSION:-3.2.3}
- RAILS_VERSION=${RAILS_VERSION:-~> 6.1.0}
- RUBY_VERSION=${RUBY_VERSION:-3.3.6}
- RAILS_VERSION=${RAILS_VERSION:-8.0.0}
container_name: app
image: active_record_proxy_adapters-app:${ENV_TAG:-latest}
tty: true
Expand Down
27 changes: 27 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# frozen_string_literal: true

require "simplecov"
require "simplecov_json_formatter"
require "active_support/core_ext/object/blank"

simple_cov_formatters = [SimpleCov::Formatter::JSONFormatter]
simple_cov_formatters << SimpleCov::Formatter::HTMLFormatter unless ENV["CI"]

SimpleCov.start do
self.formatters = simple_cov_formatters
add_filter "/spec/"
add_group "PostgreSQL" do |src_file|
[/postgresql/, /postgre_sql/].any? { |pattern| pattern.match?(src_file.filename) }
end

sanitize = ->(filename) { filename.tr(".", "_").tr("~>", "").strip }
ruby_version = sanitize.call(ENV.fetch("RUBY_VERSION", ""))
ar_version = sanitize.call(ENV.fetch("RAILS_VERSION", ""))
coverage_path = [
"ruby",
ruby_version,
"ar",
ar_version
].reject(&:blank?).join("-")

coverage_dir "coverage/#{coverage_path}"
command_name "Ruby-#{ruby_version}-AR-#{ar_version}"
end
require "active_record_proxy_adapters"
require "active_record_proxy_adapters/connection_handling"

Expand Down

0 comments on commit d7ffe30

Please sign in to comment.