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.
What is RuboCop?
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
Prerequisites
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
Step 1: Add RuboCop to Your Gemfile
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
Step 2: Configure RuboCop
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
Step 3: Configure GitLab CI
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
Step 4: Advanced RuboCop Configuration
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'
Step 5: Local Development Setup
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/
Step 6: Editor Integration
For better development experience, integrate RuboCop with your editor:
Best Practices
- Start Small: Begin with basic rules and gradually add more strict configurations
- Team Agreement: Ensure your team agrees on the style rules
- Regular Updates: Keep RuboCop and its extensions updated
- Custom Rules: Create project-specific rules when needed
- Documentation: Document any custom configurations or rule exceptions
Common Issues and Solutions
Performance Issues
If RuboCop runs slowly:
# .rubocop.yml
AllCops:
NewCops: enable
TargetRubyVersion: 3.2
Exclude:
- 'vendor/**/*'
- 'node_modules/**/*'
- 'tmp/**/*'
Monitoring and Reports
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
Conclusion
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.