๐งช Local CI Integration
Run Tests Locally with CI Configuration
Run Tests Locally with CI Configuration
Local CI Integration brings your CI/CD pipeline to your local development environment. Run the exact same tests and checks that run in your CI pipeline, right on your machine, before pushing code.
Rails 8.1 introduces a built-in CI DSL that defines your test pipeline in config/ci.rb
:
# config/ci.rb ci do # Run test suites test :units test :integration test :system # Code quality checks lint # Security scanning security_scan # Performance checks performance_test end
# Run the full CI suite locally bin/ci # Run specific test groups bin/ci test:units bin/ci lint # Run with coverage bin/ci --coverage
Run CI checks before committing to catch issues early:
# In your git pre-commit hook #!/bin/bash bin/ci test:units lint
Validate entire feature branches before opening PRs:
# Full CI suite before PR bin/ci --full
Reproduce CI failures locally for faster debugging:
# Run exact CI configuration bin/ci --env=ci
# config/ci.rb ci do # Define your test pipeline test :models test :controllers test :integration # Add quality checks lint security_scan end
# Test the configuration bin/ci # See available commands bin/ci --help
# .git/hooks/pre-push #!/bin/bash bin/ci test:units lint || exit 1
ci do # Parallel test execution test :units, parallel: 4 # With specific options test :system, headless: true # Conditional execution test :integration, if: -> { ENV['FULL_CI'] } end
ci do # RuboCop lint do rubocop '--parallel' end # Brakeman security scan security_scan do brakeman '--format json' end end
ci do # Custom checks command 'bundle exec rspec spec/models' command 'yarn test' # With descriptions command 'npm run lint', description: 'Frontend linting' end
ci do # Fast checks first lint test :units, parallel: 4 # Slower tests later test :integration test :system end
# config/ci.rb ci do # Set CI environment env 'CI' => 'true' env 'RAILS_ENV' => 'test' # Use same database as CI database :test end
--fail-fast
flag# Stop on first error bin/ci --fail-fast
ci do # Run tests in parallel test :units, parallel: :auto # Uses CPU count test :integration, parallel: 4 end
ci do # Cache dependencies cache 'vendor/bundle' cache 'node_modules' # Cache test results cache '.test-cache' end
# Run only changed tests bin/ci --changed # Run based on git diff bin/ci --since origin/main
# .github/workflows/ci.yml name: CI on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 - run: bundle install - run: bin/ci # Same command locally and in CI
# .gitlab-ci.yml test: script: - bundle install - bin/ci # Identical to local runs
# .circleci/config.yml jobs: test: steps: - checkout - run: bundle install - run: bin/ci
Problem: Different results locally vs CI
Solution:
ci do # Force CI environment env 'CI' => 'true' env 'RAILS_ENV' => 'test' end
Problem: Tests fail due to dirty database
Solution:
ci do # Reset database before tests before_suite do system 'bin/rails db:test:prepare' end end
Problem: Local CI fails on missing gems/packages
Solution:
# Always bundle before CI bundle install && bin/ci