Categories
Code Ruby Testing

Gitlab CI Setup with Rails

I had a few problems getting up and running with docker-based gitlab-ci builds, so here’s a description of my setup. I’m using gitlab.com (not self-hosted) and their hosted CI at ci.gitlab.com BUT I am using private build runners on my own Ubuntu 14.04 server.

Installation

This is a bit vague because I did this a few weeks ago but here’s what I remember

  1. Install gitlab-ci-multi-runner from the apt repo (Instructions)
  2. Install docker using the same instructions

Runner Config

  1. Enter your gitlab-ci URL and the key from the /runners page in gitlab-ci
  2. If you have execjs in your Gemfile.lock, you should specify your docker image as mwallasch/docker-ruby-node. Otherwise you can use ruby:2.2
  3. You can add tags on a runner via the web settings in CI. I couldn’t find how to do it from the config file.
  4. Speaking of config files, the multi-runner is installed as root so look at /etc/gitlab-runner/config.toml. My final one looks kind of like this:
    concurrent = 1
    
    [[runners]]
      name = "docker-runner-1"
      url = "https://ci.gitlab.com/"
      token = "XXXXXXXXXXXXXXXXXXXXXX"
      limit = 1
      executor = "docker"
      [runners.docker]
        image = "mwallasch/docker-ruby-node"
        privileged = false
        volumes = ["/cache"]
        services = ["postgres:latest", "redis:latest"]
    

    See that volume? The default config wizard added that, we’ll use it later.

Project Config

  1. Add .gitliab-ci.yml in your rails project.
    # Run before each script
    before_script:
      - gem install bundler
      - touch log/application.log
      - touch log/test.log
      - bundle install --jobs $(nproc) --path=/cache/bundler
      - cp config/database.gitlab-ci.yml config/database.yml
      - "bundle exec rake db:create RAILS_ENV=test"
      - "RAILS_ENV=test bundle exec rake db:reset"
    
    job1:
        script: "bundle exec rspec"
        tags:
            - ruby
            - postgres
            - docker
    
    • I added some tags here and setup my runner with matching tags in the CI web config. Note that I have a specific database.yml just for the CI environment, since we’re using docker services for postgres and redis (more below)
    • Note --jobs $(nproc) to speed up bundle installation.
    • Note --path=/cache/bundler which puts the gems on the persistent cache volume configured in config.toml
  2. Set up the database config. In config/database.gitlab-ci.yml:
    
    test: &test
      adapter: postgresql
      pool: 5
      timeout: 5000
      host: postgres
      database: mydbname
      user: postgres
    
    

Took some pain to get here but I do like that I can own the hardware the builds run on.