Skip to content

Commit

Permalink
Adds GH Actions and fixes tests (Closes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
zedtux committed Feb 19, 2023
1 parent 940d252 commit 4b837c2
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 63 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
push:
branches:
- master
pull_request:

jobs:
build:
strategy:
fail-fast: false
matrix:
include:
- ruby: 2.2
- ruby: 2.3
- ruby: 2.6
- ruby: 2.7
- ruby: 3
runs-on: ubuntu-latest
name: RSpec suite with Ruby ${{ matrix.ruby }}
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
FORCE_COLOR: 1
EARTHLY_RUBY_VERSION: ${{ matrix.ruby }}
steps:
- uses: actions/checkout@v3
- name: Put back the git branch into git (Earthly uses it for tagging)
run: |
branch=""
if [ -n "$GITHUB_HEAD_REF" ]; then
branch="$GITHUB_HEAD_REF"
else
branch="${GITHUB_REF##*/}"
fi
git checkout -b "$branch" || true
- name: Docker Login
run: docker login --username "$DOCKERHUB_USERNAME" --password "$DOCKERHUB_TOKEN"
- name: Download latest earthly
run: "sudo /bin/sh -c 'wget https://github.com/earthly/earthly/releases/download/v0.6.30/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly'"
- name: Earthly version
run: earthly --version
- name: Run build
run: earthly --ci --allow-privileged +rspec --EARTHLY_RUBY_VERSION=$EARTHLY_RUBY_VERSION
69 changes: 32 additions & 37 deletions Earthfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
FROM ruby:2.7-slim
WORKDIR /adapter
# This allows one to change the running Ruby version with:
#
# `earthly --build-arg EARTHLY_RUBY_VERSION=3 --allow-privileged +rspec`
ARG EARTHLY_RUBY_VERSION=2.7

FROM ruby:$EARTHLY_RUBY_VERSION
WORKDIR /gem

deps:
COPY Gemfile* /adapter/
COPY *.gemspec /adapter
COPY lib/database_cleaner/nobrainer/version.rb /adapter/lib/database_cleaner/nobrainer/
COPY Gemfile /gem
COPY *.gemspec /gem
COPY lib/database_cleaner/nobrainer/version.rb /gem/lib/database_cleaner/nobrainer/

RUN apt update \
&& apt install --yes \
Expand All @@ -14,6 +19,8 @@ deps:
&& bundle install --jobs $(nproc)

SAVE ARTIFACT /usr/local/bundle bundler
SAVE ARTIFACT /gem/Gemfile Gemfile
SAVE ARTIFACT /gem/Gemfile.lock Gemfile.lock

dev:
RUN apt update \
Expand All @@ -22,57 +29,45 @@ dev:
git

COPY +deps/bundler /usr/local/bundle
COPY +deps/Gemfile /gem/Gemfile
COPY +deps/Gemfile.lock /gem/Gemfile.lock

COPY *.gemspec /adapter
COPY LICENSE.txt /adapter
COPY README.md /adapter
COPY bin/ /adapter/bin/
COPY gemfiles/ /adapter/gemfiles/
COPY .rspec /adapter
COPY Appraisals /adapter
COPY Rakefile /adapter
COPY *.gemspec /gem
COPY LICENSE.txt /gem
COPY README.md /gem
COPY bin/ /gem/bin/
COPY .rspec /gem
COPY Appraisals /gem
COPY Rakefile /gem

COPY Gemfile* /adapter
COPY lib/database_cleaner/nobrainer/version.rb /adapter/lib/database_cleaner/nobrainer/
COPY Gemfile* /gem
COPY lib/database_cleaner/nobrainer/version.rb /gem/lib/database_cleaner/nobrainer/

RUN bundle install --jobs $(nproc)

COPY lib/ /adapter/lib/
COPY spec/ /adapter/spec/
COPY lib/ /gem/lib/
COPY spec/ /gem/spec/

ENTRYPOINT ["bundle", "exec"]
CMD ["rake"]

SAVE IMAGE zedtux/database_cleaner-nobrainer:latest

rspec:
FROM docker:19.03.13-dind

RUN apk --update --no-cache add docker-compose

COPY docker-compose.yml ./

RUN pwd

WITH DOCKER
DOCKER PULL rethinkdb:2.4-buster-slim
FROM earthly/dind:alpine

DOCKER LOAD +dev zedtux/database_cleaner-nobrainer:latest
COPY docker-compose*.yml ./

RUN pwd \
&& docker-compose up -d \
&& docker run --network=host \
zedtux/database_cleaner-nobrainer:latest \
&& docker-compose down
WITH DOCKER --load zedtux/database_cleaner-nobrainer:latest=+dev \
--pull rethinkdb:2.4
RUN docker-compose -f docker-compose-earthly.yml run --rm gem
END

publish:
FROM +dev

ARG RUBYGEMS_ORG_API_KEY

RUN rm -f /adapter/.rspec \
&& rm -rf /adapter/spec \
RUN rm -f /gem/.rspec \
&& rm -rf /gem/spec \
&& mkdir /root/.gem \
&& echo "---" > /root/.gem/credentials \
&& echo ":rubygems_api_key: $RUBYGEMS_ORG_API_KEY" >> /root/.gem/credentials \
Expand Down
2 changes: 1 addition & 1 deletion database_cleaner-nobrainer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_dependency 'database_cleaner-core', '~>2.0.0.beta2'
spec.add_dependency 'nobrainer', '~> 0.30'
spec.add_dependency 'nobrainer'

spec.add_development_dependency 'appraisal', '~> 2'
spec.add_development_dependency 'bundler', '~> 2'
Expand Down
19 changes: 19 additions & 0 deletions docker-compose-earthly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3'

services:
rethinkdb:
extends:
file: ./docker-compose.yml
service: rethinkdb

gem:
build: .
image: zedtux/database_cleaner-nobrainer:latest
depends_on:
- rethinkdb
environment:
- RUBY_ENV=test
# The nobrainer Rspec config expects the DB_HOST variable
# (see spec/spec_helper.rb), that's why we aren't setting the RDB_HOST env
# variable here.
- DB_HOST=rethinkdb
18 changes: 11 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ version: '3'

services:
rethinkdb:
image: rethinkdb:2.4-buster-slim
ports:
- 28015:28015
- 8080:8080
adapter:
image: rethinkdb:2.4

gem:
build: .
image: zedtux/database_cleaner-nobrainer:latest
depends_on:
- rethinkdb
environment:
# The nobrainer Rspec config expects the DB_HOST variable
# (see spec/spec_helper.rb), that's why we aren't setting the RDB_HOST env
# variable here.
- DB_HOST=rethinkdb
# Uncomment this line when you need to see NoBrainer logs while running
# the test suite.
# - DEBUG=1
- RUBY_ENV=test
- RETHINKDB_HOST=rethinkdb
volumes:
- .:/adapter
- .:/gem
2 changes: 0 additions & 2 deletions gemfiles/.bundle/config

This file was deleted.

14 changes: 0 additions & 14 deletions gemfiles/nobrainer0.gemfile

This file was deleted.

5 changes: 4 additions & 1 deletion lib/database_cleaner/nobrainer/deletion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ module DatabaseCleaner
module Nobrainer
class Deletion < Strategy
def initialize(args = {})
raise ArgumentError if (args.keys - %i[only except]).size.positive?

@only = args[:only] || []
@except = args[:except] || []
end

def clean
collections_to_delete.each do |table_name|
res = collections_to_delete
res.each do |table_name|
NoBrainer.run do |r|
if db == :default
r.table(table_name).delete
Expand Down
13 changes: 12 additions & 1 deletion spec/database_cleaner/nobrainer/deletion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
describe '#db=' do
def with_other_database(name)
initial_db = NoBrainer.current_db.split('_')

database_host = ENV['DB_HOST'] || 'localhost'

NoBrainer.configure do |config|
config.reset!
config.rethinkdb_url = "rethinkdb://#{database_host}/#{name}_test"

# app_name is the name of your application in lowercase.
# When using Rails, the application name is automatically inferred.
config.app_name = name
Expand All @@ -53,11 +59,16 @@ def with_other_database(name)
# variables RUBY_ENV, RAILS_ENV, RACK_ENV, or :production.
config.environment = 'test'
end

ret = yield

# Calls once again the `NoBrainer.configure` with the previous `app_name`
# which will force Nobrainer to manage to re-connect to the server with
# the previous database name.
NoBrainer.configure do |config|
config.reset!
config.rethinkdb_url = "rethinkdb://#{database_host}/#{initial_db.first}_#{initial_db.last}"

# app_name is the name of your application in lowercase.
# When using Rails, the application name is automatically inferred.
config.app_name = initial_db.first
Expand All @@ -66,6 +77,7 @@ def with_other_database(name)
# variables RUBY_ENV, RAILS_ENV, RACK_ENV, or :production.
config.environment = initial_db.last
end

ret
end

Expand All @@ -88,4 +100,3 @@ def with_other_database(name)
end
end
end

12 changes: 12 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
end

require 'database_cleaner-nobrainer'
require 'nobrainer'

database_host = ENV['DB_HOST'] || 'localhost'
db_name = ENV['DB_NAME'] || 'nobrainer_test'

NoBrainer.configure do |config|
config.rethinkdb_url = "rethinkdb://#{database_host}/#{db_name}"
config.environment = :test
config.logger = Logger.new($stderr).tap do |l|
l.level = ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN
end
end

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down

0 comments on commit 4b837c2

Please sign in to comment.