Posts tagged: rails

  • Run Rails tests on Gitlab CI with a Postgres database

    Gitlab CI can be used to run rails tests on every push. It’s pretty straight forward to setup.

    First create a .gitlab-ci.yml file:

    image: ruby:2.3
    
    services:
      - postgres:latest
    
    variables:
      POSTGRES_DB: dbname # set database
      POSTGRES_USER: username # set username
      POSTGRES_PASSWORD: ""
    
    before_script:
      - apt-get update -qy
      - apt-get install -y nodejs
      - bundle install --path /cache
    
    # run tests
    test:
      script:
        - cp config/database-gitlab.yml config/database.yml
        - bundle exec rake db:create RAILS_ENV=test
        - bundle exec rake test

    The before_script installs everything we need to run the tests. test job creates the database and executes the tests.

    Next, create a gitlab specific database config config/database-gitlab.yml to connect to the database setup earlier.

    test: adapter: postgresql
    pool: 5
    timeout: 5000
    host: postgres
    database: dbname # set database
    user: username # set username

    Now the tests will be run on each push!

    December 13, 2016 - 1 minute read - rails rake-test gitlab-ci postgres