CI with Gitlab CI

Set up Gitlab CI to run tests and linters. (Rspec + Rubocop)
Icons/chart bar
Used 14 times
Created by
C Clément Prod'homme

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/xGqsdY"
Template Source

Review the code before running this template on your machine.

if yes?('Voulez-vous installer Rubocop ?')
  run "rails app:template LOCATION='https://railsbytes.com/script/XE5sl5'"
end

create_file 'bin/test' do <<~EOF
#!/bin/sh -ex

bundle exec rake db:test:prepare
bundle exec rake spec SPEC_OPTS="--tag ~focus -f d"
  EOF
end

project_name = Rails.application.class.module_parent_name.to_s.underscore
create_file 'config/database.gitlab-ci.yml' do <<~YAML
default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000
  username: #{project_name}
  host: postgres

development:
  <<: *default
  database: #{project_name}_dev

test: &test
  <<: *default
  database: #{project_name}_test
YAML
end

image = ask("Quel image docker voulez-vous utiliser pour la CI (exemple : ruby:2.7.2) ? ")
create_file '.gitlab-ci.yml' do <<~YAML
image: #{image}

services:
  - postgres:latest

variables:
  POSTGRES_DB: #{project_name}_test
  POSTGRES_USER: #{project_name}
  POSTGRES_PASSWORD: ""
  POSTGRES_HOST_AUTH_METHOD: trust

stages:
  - test
  - rubocop

cache:
  key: #{project_name}
  paths:
    - vendor/ruby

before_script:
  - bundle install --path vendor

build:
  stage: test
  script:
    - cp config/database.gitlab-ci.yml config/database.yml
    - bundle exec rake db:create
    - bundle exec rake db:migrate
    - bin/test
    - bundle exec rubocop
YAML
end
Comments

Sign up or Login to leave a comment.