rubocop: Lint Ruby Code in CI
rubocop scans Ruby files against its cops and exits 1 when any offense is found, 0 when clean.
RuboCop is the de facto Ruby linter and formatter. In CI you run it over the project and gate on its exit code.
What it does
rubocop loads .rubocop.yml, inspects the given files (default the project tree), and reports offenses grouped by cop. It exits 1 on offenses and 2 on a fatal error such as a syntax error in the config. --parallel speeds up large repos.
Common usage
bundle exec rubocop
# only run two cop departments
bundle exec rubocop --only Style,Lint
# parallel and quiet, for CI logs
bundle exec rubocop --parallel --format progressFlags
| Flag | What it does |
|---|---|
| --parallel | Run cops across files in parallel |
| --only <cops> | Run only the named cops or departments |
| --except <cops> | Skip the named cops or departments |
| --format <fmt> | Output format (progress, simple, json, github) |
| --fail-level <sev> | Lowest severity that causes a non-zero exit |
| --display-cop-names | Show the cop name on each offense |
In CI
Run rubocop through bundle exec so the version matches the Gemfile.lock; a globally installed gem of a different version can flag different offenses than developers see. --parallel and --cache true keep CI fast on big trees.
Common errors in CI
"Error: RuboCop found unsupported Ruby version 2.x" means TargetRubyVersion exceeds what the runner has. "Gem::LoadError: You have already activated rubocop X, but your Gemfile requires rubocop Y" means a version clash; always use bundle exec. An "unrecognized cop" warning means a cop from a plugin you have not required.