This guide explains how to integrate Code Analyzer into your CI/CD pipelines for automated code analysis.
Code Analyzer provides built-in GitHub Actions workflows that you can use directly in your projects.
code_analyzer.yml
): Runs code analysis on every push and pull requestci.yml
): Runs tests, linting, and type checkingrelease.yml
): Automatically publishes to PyPI when tags are createddocs.yml
): Deploys documentation to GitHub PagesTo use the built-in code analysis workflow in your project:
mkdir -p .github/workflows
cp .github/workflows/code_analyzer.yml .github/workflows/
Here’s a complete example of integrating Code Analyzer into your GitHub Actions workflow:
name: Code Analysis
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install uv
uses: astral-sh/setup-uv@v3
- name: Install code_analyzer
run: |
uv pip install code_analyzer
- name: Run code analysis
run: |
code_analyzer analyze . --complexity --export json,html --export-dir ./reports
- name: Upload analysis reports
uses: actions/upload-artifact@v4
with:
name: code-analysis-report
path: ./reports/
retention-days: 30
For GitLab CI/CD, you can use the provided .gitlab-ci.yml
configuration file.
cp .gitlab-ci.yml ./
# In your .gitlab-ci.yml
analyze:
stage: analyze
image: python:3.13
script:
- pip install code_analyzer
- code_analyzer analyze . --complexity --export json,html --export-dir ./reports
artifacts:
paths:
- reports/
expire_in: 1 week
When integrating Code Analyzer into your CI/CD pipeline, you can customize the analysis with various options:
--complexity
: Include complexity analysis--export json,html
: Export results in multiple formats--export-dir ./reports
: Specify export directory--ignore-patterns
: Ignore specific files or directoriesExample with custom configuration:
code_analyzer analyze . \
--complexity \
--export json,html,csv \
--export-dir ./reports \
--ignore-patterns "tests/*" "docs/*"
You can configure your CI/CD pipeline to fail builds based on analysis results:
# Example: Fail if any file has >1000 lines
code_analyzer analyze . --export json --export-dir ./reports
python -c "
import json
with open('./reports/report.json') as f:
data = json.load(f)
for file in data['top_files']:
if file['file_metrics']['lines_of_code'] > 1000:
print(f'File {file[\"file_metrics\"][\"path\"]} has {file[\"file_metrics\"][\"lines_of_code\"]} lines, exceeding limit')
exit(1)
"