Skip to main content

Overview

Refactron can be integrated into your CI/CD pipeline to automatically analyze code quality on every commit, pull request, or deployment.

Generate CI/CD Templates

Refactron can generate ready-to-use CI/CD configuration files:
# GitHub Actions
refactron ci github

# GitLab CI
refactron ci gitlab

# Pre-commit hooks
refactron ci pre-commit

# Generate all
refactron ci all

GitHub Actions

Basic Workflow

.github/workflows/refactron.yml
name: Refactron Analysis

on: [push, pull_request]

jobs:
  analyze:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      
      - name: Install Refactron
        run: pip install refactron
      
      - name: Analyze Code
        run: refactron analyze . --log-format json

Fail on Critical Issues

- name: Analyze Code
  run: |
    refactron analyze . --log-format json || exit_code=$?
    if [ $exit_code -eq 1 ]; then
      echo "Critical issues found!"
      exit 1
    fi

GitLab CI

Basic Configuration

.gitlab-ci.yml
refactron:
  image: python:3.9
  before_script:
    - pip install refactron
  script:
    - refactron analyze . --log-format json
  only:
    - merge_requests
    - main

With Artifacts

refactron:
  image: python:3.9
  before_script:
    - pip install refactron
  script:
    - refactron report . --format html -o report.html
  artifacts:
    paths:
      - report.html
    expire_in: 1 week

Pre-commit Hooks

Installation

# Generate pre-commit config
refactron ci pre-commit

# Install pre-commit
pip install pre-commit
pre-commit install

Configuration

repos:
  - repo: local
    hooks:
      - id: refactron
        name: Refactron Analysis
        entry: refactron analyze
        language: system
        pass_filenames: false
        always_run: true

Best Practices

Enable JSON logging for easier parsing in CI/CD:
refactron analyze . --log-format json
Fail builds based on severity:
# .refactron.yaml
fail_on_critical: true
fail_on_errors: false
max_critical_issues: 0
max_error_issues: 10
Speed up CI runs by caching pip packages:
# GitHub Actions
- uses: actions/cache@v2
  with:
    path: ~/.cache/pip
    key: ${{ runner.os }}-pip-refactron
Create HTML reports and save as artifacts:
refactron report . --format html -o report.html

Environment-Specific Configuration

Development

.refactron.yaml
log_level: DEBUG
fail_on_critical: false
fail_on_errors: false

CI/CD

.refactron.yaml
log_level: INFO
log_format: json
fail_on_critical: true
fail_on_errors: false
max_critical_issues: 0

Production

.refactron.yaml
log_level: WARNING
log_format: json
fail_on_critical: true
fail_on_errors: true
max_critical_issues: 0
max_error_issues: 5

Advanced Patterns

Incremental Analysis

Only analyze changed files in CI:
- name: Get Changed Files
  id: changed
  run: |
    if [ "${{ github.event_name }}" == "pull_request" ]; then
      FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '\.py$' || true)
    else
      FILES=$(git diff --name-only HEAD~1..HEAD | grep '\.py$' || true)
    fi
    echo "files=$FILES" >> $GITHUB_OUTPUT

- name: Analyze Changed Files
  if: steps.changed.outputs.files != ''
  run: |
    for file in ${{ steps.changed.outputs.files }}; do
      refactron analyze "$file"
    done

Parallel Analysis

Run analysis in parallel for faster CI:
.refactron.yaml
enable_parallel_processing: true
max_parallel_workers: 4

Exit Codes

Refactron uses standard exit codes:
CodeMeaning
0Success, no issues
1Issues found
2Error during execution
Use these to control CI behavior:
refactron analyze . || exit_code=$?
if [ $exit_code -gt 1 ]; then
  echo "Refactron error!"
  exit $exit_code
fi

Next Steps