Contents

How to Add RuboCop Checking for Your Ruby Project in GitLab CI

How to Add RuboCop Checking for Your Ruby Project in GitLab CI

RuboCop is a Ruby static code analyzer and formatter that helps maintain consistent code style and catch potential issues. Integrating it into your GitLab CI pipeline ensures that all code changes meet your project’s quality standards before merging.

RuboCop is a Ruby gem that:

  • Enforces Ruby style guide conventions
  • Detects potential bugs and code smells
  • Provides automatic code formatting
  • Integrates with popular editors and CI systems
  • Supports custom rules and configurations

Before setting up RuboCop in GitLab CI, ensure you have:

  • A Ruby project with a Gemfile
  • GitLab repository with CI/CD enabled
  • Basic understanding of GitLab CI configuration

First, add RuboCop and related gems to your project’s Gemfile:

group :development, :test do
  gem "rubocop", require: false
  gem "rubocop-rails", require: false  # For Rails projects
  gem "rubocop-rspec", require: false  # For RSpec projects
  gem "pronto", require: false
  gem "pronto-rubocop", require: false
  gem "pronto-flay", require: false
end

Then run:

bundle install

Create a .rubocop.yml file in your project root:

# .rubocop.yml
AllCops:
  NewCops: enable
  TargetRubyVersion: 3.2
  Exclude:
    - 'db/**/*'
    - 'config/**/*'
    - 'script/**/*'
    - 'bin/**/*'
    - 'vendor/**/*'
    - 'node_modules/**/*'

Style/Documentation:
  Enabled: false

Style/StringLiterals:
  EnforcedStyle: single_quotes

Layout/LineLength:
  Max: 120

Metrics/MethodLength:
  Max: 20

Metrics/AbcSize:
  Max: 30

Create or update your .gitlab-ci.yml file:

# .gitlab-ci.yml
image: "ruby:3.3"

variables:
  BUNDLE_PATH: vendor/bundle
  BUNDLE_APP_CONFIG: .bundle

before_script:
  - apt-get update -qq && apt-get install -y -qq cmake build-essential pkg-config libgit2-dev
  - ruby -v
  - which ruby
  - gem install bundler --no-document
  - bundle config set --local path 'vendor/bundle'
  - bundle install --jobs $(nproc) --retry 3

stages:
  - test
  - quality

rubocop:
  stage: quality
  script:
    - bundle exec rubocop --parallel
  allow_failure: false
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

pronto:
  stage: quality
  script:
    - git fetch origin $CI_DEFAULT_BRANCH:refs/remotes/origin/$CI_DEFAULT_BRANCH
    - bundle exec pronto run -c=origin/$CI_DEFAULT_BRANCH --exit-code
  allow_failure: false
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

For more complex projects, you might want to create separate configuration files:

# .rubocop_todo.yml (auto-generated)
# This file is generated by RuboCop and contains offenses that are not fixed yet.
# Run 'rubocop --auto-gen-config' to generate this file.

# Offense count: 1
# Cop: Style/StringLiterals
# Configuration parameters: EnforcedStyle, ConsistentQuotingInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
  ExcludedFiles:
    - 'app/controllers/application_controller.rb'

To run RuboCop locally before pushing:

# Check all files
bundle exec rubocop

# Auto-correct issues where possible
bundle exec rubocop -a

# Generate configuration for existing violations
bundle exec rubocop --auto-gen-config

# Check specific files
bundle exec rubocop app/models/

For better development experience, integrate RuboCop with your editor:

  1. Start Small: Begin with basic rules and gradually add more strict configurations
  2. Team Agreement: Ensure your team agrees on the style rules
  3. Regular Updates: Keep RuboCop and its extensions updated
  4. Custom Rules: Create project-specific rules when needed
  5. Documentation: Document any custom configurations or rule exceptions

If RuboCop runs slowly:

# .rubocop.yml
AllCops:
  NewCops: enable
  TargetRubyVersion: 3.2
  Exclude:
    - 'vendor/**/*'
    - 'node_modules/**/*'
    - 'tmp/**/*'

RuboCop can generate various reports:

# HTML report
bundle exec rubocop --format html -o rubocop_report.html

# JSON report
bundle exec rubocop --format json -o rubocop_report.json

# Progress format
bundle exec rubocop --format progress

Integrating RuboCop into your GitLab CI pipeline is a straightforward process that significantly improves code quality. By following this guide, you’ll have:

  • Automated code style checking on every merge request
  • Consistent code formatting across your team
  • Early detection of potential issues
  • Better code maintainability

Remember to start with basic configurations and gradually refine them based on your project’s needs and team preferences.