I host my personal projects in gitlab because it’s free and it gives you free runners for your ci.  I recently started studying the Hanami framework in ruby and when it was time to build the test suite, I initially couldn’t properly get it to work because of a strange error.

My initial gitlab-ci.yml was as follows

1image: ruby:2.5
2 
3services:
4  - postgres:latest
5 
6variables:
8  POSTGRES_DB: budget_tracker_test
9  HANAMI_ENV: test
10 
11cache:
12  paths:
13    - vendor/ruby
14 
15rspec:
16  before_script:
17    - gem install bundler  --no-ri --no-rdoc
18    - bundle install -j $(nproc) --path vendor --quiet
19 
20  script:
21    - echo 'Trying to create db for develop'
22    - bundle exec hanami db prepare
23    - echo 'Trying to create db for test'
24    - HANAMI_ENV=test bundle exec hanami db prepare
25    - rspec spec

Pretty straightforward right? But for some reason I was encountering this error:

1Could not find executable in your PATH: `createdb`
2/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator/postgres_adapter.rb:104:in `rescue in call_db_command'
3    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator/postgres_adapter.rb:97:in `call_db_command'
4    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator/postgres_adapter.rb:36:in `create'
5    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator.rb:287:in `create'
6    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator.rb:332:in `prepare'
7    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator.rb:247:in `prepare'
8    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/lib/hanami/cli/commands/db/prepare.rb:26:in `prepare_database'
9    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/lib/hanami/cli/commands/db/prepare.rb:17:in `call'
10    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/lib/hanami/cli/commands/command.rb:85:in `call'
11    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-cli-0.2.0/lib/hanami/cli.rb:57:in `call'
12    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/bin/hanami:6:in `<top (required)>'
13    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/bin/hanami:23:in `load'
14    /builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/bin/hanami:23:in `<top (required)>'
15    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli/exec.rb:74:in `load'
16    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli/exec.rb:74:in `kernel_load'
17    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli/exec.rb:28:in `run'
18    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:424:in `exec'
19    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
20    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
21    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
22    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:27:in `dispatch'
23    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
24    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:18:in `start'
25    /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.2/exe/bundle:30:in `block in <top (required)>'
26    /usr/local/lib/ruby/site_ruby/2.5.0/bundler/friendly_errors.rb:124:in `with_friendly_errors'
27    /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.2/exe/bundle:22:in `<top (required)>'
28    /usr/local/bundle/bin/bundle:23:in `load'
29    /usr/local/bundle/bin/bundle:23:in `<main>'
30ERROR: Job failed: exit code 1

So I looked up some of the discussions on the issue tracker on Hanami’s github and I found this still open issue with Hanami still requiring a local postgres instance :|. Anyway, cart before the horse, I ended up using the postgres service while manually installing postgres in the ruby image just so I could run my specs proper.y

Here is my `gitlab-ci.yml` now

1image: ruby:2.5
2 
3services:
4  - postgres:latest
5 
6variables:
8  HANAMI_ENV: test
9 
10cache:
11  paths:
12    - vendor/ruby
13 
14before_script:
15  - apt-get update -qq && apt-get install -y -qq postgresql postgresql-contrib libpq-dev cmake
16  - gem install bundler  --no-ri --no-rdoc
17  - bundle install -j $(nproc) --path vendor --quiet
18 
19rspec:
20  script:
21  - bundle exec hanami db prepare
22  - bundle exec rspec spec

After pushing this to gitlab, viola!