
Ci Cd Pipeline Builder
- 96 installs
- 451 repo stars
- Updated July 21, 2026
- borghei/claude-skills
ci-cd-pipeline-builder is a skill that generates CI/CD pipelines from detected stack signals across GitHub Actions, GitLab CI, CircleCI, and Buildkite.
About
This skill designs and generates CI/CD pipelines from detected project stack signals. It reads lockfiles and manifests to produce pipelines with caching, matrix builds, deployment gates, and security scanning across GitHub Actions, GitLab CI, CircleCI, and Buildkite. Developers use it when bootstrapping CI, migrating pipelines, or optimizing build times.
- Generates CI/CD pipelines from detected stack signals (lockfiles, manifests, scripts)
- Supports GitHub Actions, GitLab CI, CircleCI, and Buildkite
- Adds deployment strategies (blue-green, canary, rolling) plus SAST and container scanning
Ci Cd Pipeline Builder by the numbers
- 96 all-time installs (skills.sh)
- Ranked #556 of 1,435 DevOps & CI/CD skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
ci-cd-pipeline-builder capabilities & compatibility
- Capabilities
- pipeline generation · deployment strategy · security scanning · stack detection
- Works with
- github · gitlab · docker · kubernetes · terraform · jenkins
- Use cases
- ci cd · devops · security audit
- Pricing
- Free
What ci-cd-pipeline-builder says it does
Generate production-grade CI/CD pipelines from detected project stack signals.
Supports GitHub Actions, GitLab CI, CircleCI, and Buildkite with deployment strategies including blue-green, canary, and rolling updates.
SAST scanning (CodeQL, Semgrep, Snyk)
npx skills add https://github.com/borghei/claude-skills --skill ci-cd-pipeline-builderAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 96 |
|---|---|
| repo stars | ★ 451 |
| Last updated | July 21, 2026 |
| Repository | borghei/claude-skills ↗ |
What it does
Generate a production-grade CI/CD pipeline for a project's detected stack with deployment and security stages.
Who is it for?
Bootstrapping CI, migrating between CI platforms, or adding deployment and security stages.
Skip if: Runtime application monitoring or production incident response.
When should I use this skill?
You are bootstrapping CI, migrating pipelines, or optimizing build times.
What you get
A generated pipeline with lint/test/build/deploy stages, caching, security scanning, and a deployment strategy.
- ci/cd pipeline config
- deployment strategy
- security scanning steps
By the numbers
- 4 CI platforms supported
- 3 deployment strategies (blue-green, canary, rolling)
Files
CI/CD Pipeline Builder
Tier: POWERFUL Category: Engineering / DevOps Maintainer: Claude Skills Team
Overview
Generate production-grade CI/CD pipelines from detected project stack signals. Analyzes lockfiles, manifests, and scripts to produce optimized pipelines with proper caching, matrix strategies, security scanning, and deployment gates. Supports GitHub Actions, GitLab CI, CircleCI, and Buildkite with deployment strategies including blue-green, canary, and rolling updates.
Keywords
CI/CD, GitHub Actions, GitLab CI, pipeline, deployment, caching, matrix builds, blue-green deployment, canary deployment, security scanning, SAST, container builds, environment gates
Core Capabilities
1. Stack Detection
- Language/runtime detection from lockfiles and manifests
- Package manager inference from lock file format
- Build/test/lint command extraction from scripts
- Framework detection (Next.js, FastAPI, Go modules, etc.)
- Infrastructure detection (Docker, Kubernetes, Terraform)
2. Pipeline Generation
- Lint, test, build, deploy stages with correct dependencies
- Caching strategies matched to package manager
- Matrix builds for multi-version support
- Artifact passing between jobs
- Conditional execution (path filters, branch rules)
3. Deployment Strategies
- Blue-green with instant rollback
- Canary with percentage-based traffic shifting
- Rolling updates with health checks
- Feature flags integration
- Manual approval gates for production
4. Security Integration
- SAST scanning (CodeQL, Semgrep, Snyk)
- Dependency vulnerability scanning
- Container image scanning (Trivy, Grype)
- Secret scanning in CI
- SBOM generation
When to Use
- Bootstrapping CI/CD for a new repository
- Migrating between CI platforms
- Optimizing slow or flaky pipelines
- Adding deployment stages to an existing CI-only pipeline
- Implementing security scanning in the pipeline
- Setting up multi-environment deployment (staging, production)
Stack Detection Heuristics
File Found → Inference
─────────────────────────────────────────────────
package-lock.json → Node.js + npm
pnpm-lock.yaml → Node.js + pnpm
yarn.lock → Node.js + yarn
bun.lockb → Bun
requirements.txt / Pipfile → Python + pip/pipenv
pyproject.toml + uv.lock → Python + uv
poetry.lock → Python + poetry
go.mod → Go
Cargo.lock → Rust
Gemfile.lock → Ruby
composer.lock → PHP
next.config.* → Next.js (Node.js)
nuxt.config.* → Nuxt (Node.js)
Dockerfile → Container build needed
docker-compose.yml → Multi-service setup
terraform/*.tf → Infrastructure as Code
k8s/ or kubernetes/ → Kubernetes deploymentGitHub Actions Pipeline Templates
Node.js (pnpm + Vitest + Next.js)
name: CI/CD
on:
push:
branches: [main, dev]
pull_request:
branches: [main, dev]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
NODE_VERSION: '20'
PNPM_VERSION: '9'
jobs:
lint-and-typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: ${{ env.PNPM_VERSION }}
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: pnpm lint
- run: pnpm typecheck
test:
runs-on: ubuntu-latest
needs: lint-and-typecheck
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: testdb
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: ${{ env.PNPM_VERSION }}
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: pnpm test:ci
env:
DATABASE_URL: postgresql://test:test@localhost:5432/testdb
build:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: ${{ env.PNPM_VERSION }}
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: pnpm build
- uses: actions/upload-artifact@v4
with:
name: build-output
path: .next/
retention-days: 1
security-scan:
runs-on: ubuntu-latest
permissions:
security-events: write
steps:
- uses: actions/checkout@v4
- uses: github/codeql-action/init@v3
with:
languages: javascript-typescript
- uses: github/codeql-action/analyze@v3
deploy-staging:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: [build, security-scan]
runs-on: ubuntu-latest
environment:
name: staging
url: https://staging.myapp.com
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: build-output
path: .next/
- name: Deploy to staging
run: |
# Replace with your deployment command
echo "Deploying to staging..."
env:
DEPLOY_TOKEN: ${{ secrets.STAGING_DEPLOY_TOKEN }}
deploy-production:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: deploy-staging
runs-on: ubuntu-latest
environment:
name: production
url: https://myapp.com
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: build-output
path: .next/
- name: Deploy to production
run: |
echo "Deploying to production..."
env:
DEPLOY_TOKEN: ${{ secrets.PROD_DEPLOY_TOKEN }}Python (uv + pytest + FastAPI)
name: CI/CD
on:
push:
branches: [main]
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v4
- run: uv sync --frozen
- run: uv run ruff check .
- run: uv run ruff format --check .
- run: uv run mypy src/
test:
runs-on: ubuntu-latest
needs: lint
strategy:
matrix:
python-version: ['3.11', '3.12', '3.13']
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: testdb
ports: ['5432:5432']
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v4
with:
python-version: ${{ matrix.python-version }}
- run: uv sync --frozen
- run: uv run pytest --cov --cov-report=xml -v
env:
DATABASE_URL: postgresql://test:test@localhost:5432/testdb
- uses: codecov/codecov-action@v4
if: matrix.python-version == '3.12'
with:
file: coverage.xml
build-container:
needs: test
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.ref == 'refs/heads/main' }}
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
container-scan:
needs: build-container
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: aquasecurity/trivy-action@master
with:
image-ref: ghcr.io/${{ github.repository }}:${{ github.sha }}
severity: 'CRITICAL,HIGH'
exit-code: '1'Deployment Strategy Decision Framework
How critical is zero-downtime?
│
├─ Critical (payment processing, real-time systems)
│ └─ BLUE-GREEN DEPLOYMENT
│ Pro: Instant rollback, zero-downtime guaranteed
│ Con: Requires 2x infrastructure during deployment
│
├─ Important but can tolerate brief errors
│ ├─ Need to validate with real traffic first?
│ │ └─ CANARY DEPLOYMENT
│ │ Pro: Test with small % of traffic before full rollout
│ │ Con: Complex routing, need observability for canary metrics
│ │
│ └─ Standard web app with health checks
│ └─ ROLLING UPDATE
│ Pro: Simple, built into K8s/ECS, gradual rollout
│ Con: Both versions serve traffic during rollout
│
└─ Development/staging environment
└─ RECREATE (stop old, start new)
Pro: Simplest, cleanest
Con: Brief downtime during deploymentCaching Strategy Reference
| Package Manager | Cache Path | Key Pattern |
|---|---|---|
| npm | ~/.npm | ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }} |
| pnpm | Detected by setup-node | cache: 'pnpm' in setup-node |
| yarn | ~/.cache/yarn | ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }} |
| pip | ~/.cache/pip | ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} |
| uv | ~/.cache/uv | Handled by setup-uv |
| Go | ~/go/pkg/mod | ${{ runner.os }}-go-${{ hashFiles('go.sum') }} |
| Cargo | ~/.cargo/registry | ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }} |
| Docker | GHA cache | cache-from: type=gha in build-push-action |
Pipeline Optimization Techniques
1. Path Filtering (Skip Unnecessary Runs)
on:
push:
paths:
- 'src/**'
- 'tests/**'
- 'package.json'
- 'pnpm-lock.yaml'
paths-ignore:
- '**.md'
- 'docs/**'
- '.github/ISSUE_TEMPLATE/**'2. Job Dependency Graph
lint ──────┐
├──→ test ──→ build ──→ deploy-staging ──→ deploy-production
typecheck ─┘ │
└──→ security-scan3. Matrix Strategy with Fail-Fast
strategy:
fail-fast: true # cancel all if one fails
matrix:
node-version: [18, 20, 22]
os: [ubuntu-latest]
include:
- node-version: 20
os: macos-latest # test one combo on macOSGitLab CI Equivalent
stages:
- validate
- test
- build
- deploy
variables:
NODE_VERSION: "20"
.node-setup: &node-setup
image: node:${NODE_VERSION}
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .pnpm-store/
before_script:
- corepack enable
- pnpm install --frozen-lockfile
lint:
stage: validate
<<: *node-setup
script:
- pnpm lint
- pnpm typecheck
test:
stage: test
<<: *node-setup
services:
- postgres:16
variables:
POSTGRES_DB: testdb
POSTGRES_USER: test
POSTGRES_PASSWORD: test
DATABASE_URL: postgresql://test:test@postgres:5432/testdb
script:
- pnpm test:ci
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
build:
stage: build
<<: *node-setup
script:
- pnpm build
artifacts:
paths:
- .next/
expire_in: 1 hour
deploy_staging:
stage: deploy
environment:
name: staging
url: https://staging.myapp.com
rules:
- if: $CI_COMMIT_BRANCH == "main"
script:
- echo "Deploy to staging"
deploy_production:
stage: deploy
environment:
name: production
url: https://myapp.com
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
needs: [deploy_staging]
script:
- echo "Deploy to production"Validation Checklist
Before merging a generated pipeline:
1. YAML parses without syntax errors 2. All referenced commands exist in the repository (test, lint, build) 3. Cache strategy matches the detected package manager 4. Required secrets are documented (not embedded in YAML) 5. Branch protection rules match organization policy 6. Deployment jobs are gated by protected environments 7. Security scanning runs on the appropriate code paths 8. Artifact retention is set (do not keep build artifacts indefinitely) 9. Concurrency group prevents duplicate runs on the same branch 10. Path filters exclude documentation-only changes from full CI runs
Common Pitfalls
- Copying pipelines between projects without adapting to the actual stack
- No concurrency control leading to redundant parallel runs on rapid pushes
- Missing cache keys causing cache misses on every run (slow builds)
- Running full matrix on every PR when only main needs multi-version testing
- Hardcoding secrets in YAML instead of using CI secret stores
- No path filtering so documentation changes trigger full build+test+deploy
- Deploy jobs without environment gates allowing accidental production deployments
- No artifact retention policy causing storage costs to grow indefinitely
Best Practices
1. Detect stack first, then generate pipeline — never guess at build commands 2. Keep the generated baseline under version control and customize incrementally 3. One optimization at a time — add caching, then matrix, then split jobs 4. Require green CI before any deployment job can execute 5. Use protected environments for production credentials and manual approval gates 6. Track pipeline duration and flakiness as first-class engineering metrics 7. Separate deploy jobs from CI jobs to keep feedback fast for developers 8. Regenerate the pipeline when the stack changes significantly (new language, new framework)
Troubleshooting
| Problem | Cause | Solution |
|---|---|---|
| Pipeline YAML fails validation | Indentation errors or invalid key names | Run yamllint locally before committing; use the CI platform's built-in linter (e.g., act for GitHub Actions, gitlab-ci-lint for GitLab) |
| Cache misses on every run | Cache key does not include the correct lockfile hash | Verify the hashFiles() path matches the actual lockfile location; check the Caching Strategy Reference table above |
| Matrix build times explode | Running full OS + version matrix on every PR | Restrict the full matrix to main branch pushes; run a single representative version on PRs |
| Deployment job triggers on PRs | Missing branch/event guard on deploy jobs | Add if: github.ref == 'refs/heads/main' && github.event_name == 'push' or equivalent platform condition |
| Service containers fail to start | Health check misconfigured or image not found | Pin the service image to a specific major version; confirm health check command exists in the image |
| Secret not available in workflow | Secret not added to the repository or environment settings | Add the secret via the CI platform's secrets UI; ensure the job references the correct environment name |
| Build artifact missing in deploy job | Artifact name mismatch or retention expired | Ensure upload-artifact and download-artifact use the same name value; set retention-days high enough to survive the full pipeline |
Success Criteria
- Pipeline generates valid YAML that passes platform-native linting on first attempt for detected stacks
- Build times stay under 10 minutes for lint + test + build stages combined (excluding deploy)
- Cache hit rate exceeds 90% on repeat runs with unchanged lockfiles
- Security scanning (SAST + dependency + container) executes on every push to
mainwithout manual triggers - Deployment to staging is fully automated; production requires exactly one manual approval gate
- Pipeline flakiness rate remains below 2% over a rolling 30-day window
- Zero hardcoded secrets in generated pipeline YAML; all sensitive values reference platform secret stores
Scope & Limitations
This skill covers:
- Generating CI/CD pipelines for GitHub Actions, GitLab CI, CircleCI, and Buildkite
- Stack detection from lockfiles, manifests, Dockerfiles, and infrastructure-as-code definitions
- Deployment strategy selection (blue-green, canary, rolling, recreate) with decision framework
- Pipeline optimization including caching, matrix builds, path filtering, and concurrency control
This skill does NOT cover:
- Runtime infrastructure provisioning or cloud resource management (see
engineering/saas-scaffolder) - Application-level security hardening beyond CI-integrated scanning (see
engineering/skill-security-auditor) - Monitoring, alerting, and observability configuration after deployment (see
engineering/observability-designer) - Database migration orchestration during deployments (see
engineering/migration-architect)
Integration Points
| Skill | Integration | Data Flow |
|---|---|---|
engineering/dependency-auditor | Feeds vulnerability scan results into pipeline security gates | Auditor findings trigger pipeline failure or warning annotations |
engineering/release-manager | Coordinates versioning and changelog with deploy stages | Release tags drive conditional deployment job execution |
engineering/observability-designer | Post-deploy health checks and alerting complement pipeline gates | Pipeline triggers smoke tests; observability confirms deployment health |
engineering/env-secrets-manager | Manages secrets referenced by pipeline environment variables | Secret rotation policies feed into pipeline secret store configuration |
engineering/migration-architect | Database migrations run as a pre-deploy step in the pipeline | Migration status gates the application deployment job |
engineering/runbook-generator | Generates rollback runbooks aligned with deployment strategy | Pipeline failure triggers link to the relevant rollback runbook |
#!/usr/bin/env python3
"""Analyze CI/CD pipeline configs and suggest caching improvements.
Reads GitHub Actions or GitLab CI YAML files and identifies missing
caches, suboptimal cache keys, redundant installs across jobs, and
opportunities to use built-in caching features.
Usage:
python cache_optimizer.py .github/workflows/ci.yml
python cache_optimizer.py .gitlab-ci.yml --json
python cache_optimizer.py --dir .github/workflows/
"""
import argparse
import json
import os
import re
import sys
from pathlib import Path
# ---------------------------------------------------------------------------
# Known cache strategies
# ---------------------------------------------------------------------------
GITHUB_CACHE_STRATEGIES = {
"npm": {
"preferred": "cache: 'npm' in actions/setup-node",
"cache_path": "~/.npm",
"key_pattern": "hashFiles('**/package-lock.json')",
"lockfile": "package-lock.json",
},
"pnpm": {
"preferred": "cache: 'pnpm' in actions/setup-node (with pnpm/action-setup)",
"cache_path": "detected by setup-node",
"key_pattern": "hashFiles('**/pnpm-lock.yaml')",
"lockfile": "pnpm-lock.yaml",
},
"yarn": {
"preferred": "cache: 'yarn' in actions/setup-node",
"cache_path": "~/.cache/yarn",
"key_pattern": "hashFiles('**/yarn.lock')",
"lockfile": "yarn.lock",
},
"pip": {
"preferred": "cache: 'pip' in actions/setup-python",
"cache_path": "~/.cache/pip",
"key_pattern": "hashFiles('**/requirements*.txt')",
"lockfile": "requirements.txt",
},
"uv": {
"preferred": "Built-in caching via astral-sh/setup-uv",
"cache_path": "~/.cache/uv",
"key_pattern": "hashFiles('**/uv.lock')",
"lockfile": "uv.lock",
},
"go": {
"preferred": "Built-in caching via actions/setup-go (cache: true)",
"cache_path": "~/go/pkg/mod",
"key_pattern": "hashFiles('**/go.sum')",
"lockfile": "go.sum",
},
"cargo": {
"preferred": "actions/cache with Cargo.lock hash key",
"cache_path": "~/.cargo/registry, ~/.cargo/git, target/",
"key_pattern": "hashFiles('**/Cargo.lock')",
"lockfile": "Cargo.lock",
},
}
# ---------------------------------------------------------------------------
# Analysis functions
# ---------------------------------------------------------------------------
SEVERITY_HIGH = "high"
SEVERITY_MEDIUM = "medium"
SEVERITY_LOW = "low"
def detect_platform(filepath, text):
"""Determine CI platform from path and content."""
if ".github" in filepath or "runs-on:" in text:
return "github-actions"
if ".gitlab-ci" in os.path.basename(filepath) or ("stages:" in text and "script:" in text):
return "gitlab-ci"
return "unknown"
def detect_package_managers(text):
"""Detect which package managers are referenced in the pipeline."""
found = set()
indicators = {
"npm": [r'\bnpm\s+(ci|install)\b', r'package-lock\.json'],
"pnpm": [r'\bpnpm\s+install\b', r'pnpm-lock\.yaml'],
"yarn": [r'\byarn\s+install\b', r'yarn\.lock'],
"pip": [r'\bpip\s+install\b', r'requirements.*\.txt'],
"uv": [r'\buv\s+(sync|install)\b', r'uv\.lock'],
"poetry": [r'\bpoetry\s+install\b', r'poetry\.lock'],
"go": [r'\bgo\s+(build|test|vet)\b', r'go\.sum'],
"cargo": [r'\bcargo\s+(build|test)\b', r'Cargo\.lock'],
}
for pm, patterns in indicators.items():
for pat in patterns:
if re.search(pat, text):
found.add(pm)
break
return found
def check_missing_cache(text, platform, package_managers):
"""Identify package managers used without any caching."""
suggestions = []
for pm in package_managers:
strategy = GITHUB_CACHE_STRATEGIES.get(pm)
if not strategy:
continue
has_cache = False
# Check for setup-node/setup-python cache param
if pm in ("npm", "pnpm", "yarn") and re.search(r"cache:\s*['\"]?" + pm, text):
has_cache = True
if pm == "pip" and re.search(r"cache:\s*['\"]?pip", text):
has_cache = True
if pm == "uv" and "setup-uv" in text:
has_cache = True
if pm == "go" and "setup-go" in text:
has_cache = True
# Check for explicit actions/cache
if re.search(r"actions/cache", text) and strategy["lockfile"] in text:
has_cache = True
# GitLab cache block
if platform == "gitlab-ci" and "cache:" in text:
has_cache = True
if not has_cache:
suggestions.append({
"type": "missing-cache",
"severity": SEVERITY_HIGH,
"package_manager": pm,
"message": f"No caching detected for {pm}. Dependencies are re-downloaded on every run.",
"recommendation": strategy["preferred"],
"estimated_savings": "30-120 seconds per job",
})
return suggestions
def check_cache_key_quality(text, platform):
"""Check for suboptimal cache key patterns."""
suggestions = []
if platform == "github-actions":
# Check for cache keys without hashFiles
cache_key_lines = re.findall(r'key:\s*(.+)', text)
for key_expr in cache_key_lines:
if "hashFiles" not in key_expr and "github.sha" in key_expr:
suggestions.append({
"type": "volatile-cache-key",
"severity": SEVERITY_HIGH,
"message": "Cache key uses github.sha which changes every commit, causing 0% hit rate.",
"recommendation": "Use hashFiles('**/lockfile') for dependency caches.",
"estimated_savings": "30-120 seconds per job",
})
if "hashFiles" not in key_expr and "${{" not in key_expr and key_expr.strip().strip("'\""):
# Static string key - always same, never invalidated
suggestions.append({
"type": "static-cache-key",
"severity": SEVERITY_MEDIUM,
"message": f"Cache key appears static: {key_expr.strip()[:60]}. Cache never invalidates.",
"recommendation": "Include hashFiles() of your lockfile so cache refreshes on dependency changes.",
"estimated_savings": "Prevents stale dependency bugs",
})
if platform == "gitlab-ci":
cache_key_lines = re.findall(r'key:\s*(.+)', text)
for key_expr in cache_key_lines:
if "$CI_COMMIT_SHA" in key_expr:
suggestions.append({
"type": "volatile-cache-key",
"severity": SEVERITY_HIGH,
"message": "Cache key uses $CI_COMMIT_SHA which changes every commit.",
"recommendation": "Use $CI_COMMIT_REF_SLUG or Files(['lockfile']) for dependency caches.",
"estimated_savings": "30-120 seconds per job",
})
return suggestions
def check_redundant_installs(text, platform):
"""Detect when multiple jobs repeat the same install step without shared cache."""
suggestions = []
if platform == "github-actions":
install_patterns = [
(r'npm ci', "npm ci"),
(r'pnpm install', "pnpm install"),
(r'yarn install', "yarn install"),
(r'pip install -r', "pip install"),
(r'uv sync', "uv sync"),
]
for pat, label in install_patterns:
matches = re.findall(pat, text)
if len(matches) > 2:
suggestions.append({
"type": "redundant-install",
"severity": SEVERITY_MEDIUM,
"message": f"'{label}' appears {len(matches)} times across jobs.",
"recommendation": (
"Ensure caching is enabled so repeated installs are fast. "
"Consider a dedicated setup job with artifact passing for large dependency trees."
),
"estimated_savings": f"{(len(matches) - 1) * 20}-{(len(matches) - 1) * 60} seconds total",
})
return suggestions
def check_docker_cache(text, platform):
"""Check if Docker builds use layer caching."""
suggestions = []
if "docker" not in text.lower() and "buildx" not in text.lower():
return suggestions
has_docker_build = bool(re.search(r'docker\s+build\b', text))
has_buildx = "docker/build-push-action" in text or "buildx" in text
has_cache_from = "cache-from" in text
if has_docker_build and not has_buildx:
suggestions.append({
"type": "no-buildx",
"severity": SEVERITY_MEDIUM,
"message": "Using 'docker build' without BuildKit/Buildx. No layer caching between runs.",
"recommendation": "Switch to docker/build-push-action with cache-from: type=gha, cache-to: type=gha,mode=max",
"estimated_savings": "1-10 minutes per build depending on image size",
})
elif has_buildx and not has_cache_from:
suggestions.append({
"type": "buildx-no-cache",
"severity": SEVERITY_HIGH,
"message": "Using Buildx but no cache-from/cache-to configured. Docker layers rebuild from scratch.",
"recommendation": "Add cache-from: type=gha and cache-to: type=gha,mode=max to build-push-action",
"estimated_savings": "1-10 minutes per build depending on image size",
})
return suggestions
def check_missing_restore_keys(text, platform):
"""Suggest restore-keys for graceful cache fallback."""
suggestions = []
if platform != "github-actions":
return suggestions
if "actions/cache" in text and "restore-keys" not in text:
suggestions.append({
"type": "missing-restore-keys",
"severity": SEVERITY_LOW,
"message": "actions/cache used without restore-keys. Cache miss when lockfile changes means full re-download.",
"recommendation": (
"Add restore-keys with a prefix fallback, e.g.:\n"
" restore-keys: |\n"
" ${{ runner.os }}-npm-\n"
"This allows partial cache hits when only some deps change."
),
"estimated_savings": "10-60 seconds on lockfile changes",
})
return suggestions
# ---------------------------------------------------------------------------
# Main analysis
# ---------------------------------------------------------------------------
ALL_CHECKS = [
check_missing_cache,
check_cache_key_quality,
check_redundant_installs,
check_docker_cache,
check_missing_restore_keys,
]
def analyze_file(filepath):
"""Run all cache optimization checks on a single pipeline file."""
filepath = str(filepath)
try:
with open(filepath, "r", encoding="utf-8") as f:
text = f.read()
except (OSError, UnicodeDecodeError) as exc:
return {
"file": filepath,
"platform": "unknown",
"package_managers": [],
"suggestions": [{
"type": "file-read-error",
"severity": SEVERITY_HIGH,
"message": str(exc),
}],
}
platform = detect_platform(filepath, text)
package_managers = sorted(detect_package_managers(text))
suggestions = []
for check_fn in ALL_CHECKS:
if check_fn == check_missing_cache:
suggestions.extend(check_fn(text, platform, package_managers))
elif check_fn in (check_cache_key_quality, check_redundant_installs,
check_docker_cache, check_missing_restore_keys):
suggestions.extend(check_fn(text, platform))
# Sort: high first
severity_order = {SEVERITY_HIGH: 0, SEVERITY_MEDIUM: 1, SEVERITY_LOW: 2}
suggestions.sort(key=lambda s: severity_order.get(s["severity"], 9))
return {
"file": filepath,
"platform": platform,
"package_managers": package_managers,
"suggestions": suggestions,
}
def collect_files(path):
"""Collect YAML files from a file path or directory."""
p = Path(path)
if p.is_file():
return [p]
if p.is_dir():
return sorted(p.glob("**/*.yml")) + sorted(p.glob("**/*.yaml"))
return []
# ---------------------------------------------------------------------------
# Output formatting
# ---------------------------------------------------------------------------
_SEVERITY_BADGE = {
SEVERITY_HIGH: "HIGH",
SEVERITY_MEDIUM: "MED ",
SEVERITY_LOW: "LOW ",
}
def format_human(results):
"""Return human-readable optimization report."""
lines = []
total_suggestions = 0
for result in results:
lines.append(f"\n=== {result['file']} ===")
lines.append(f"Platform: {result['platform']}")
lines.append(f"Package managers detected: {', '.join(result['package_managers']) or 'none'}")
if not result["suggestions"]:
lines.append("\n No optimization suggestions. Caching looks good!")
continue
lines.append("")
for idx, s in enumerate(result["suggestions"], 1):
badge = _SEVERITY_BADGE.get(s["severity"], "????")
lines.append(f" [{badge}] {idx}. {s['type']}")
lines.append(f" {s['message']}")
if "recommendation" in s:
for rec_line in s["recommendation"].split("\n"):
lines.append(f" -> {rec_line}")
if "estimated_savings" in s:
lines.append(f" Estimated savings: {s['estimated_savings']}")
lines.append("")
total_suggestions += 1
lines.append(f"Total suggestions: {total_suggestions}")
high_count = sum(
1 for r in results for s in r["suggestions"] if s["severity"] == SEVERITY_HIGH
)
if high_count:
lines.append(f"High-priority items: {high_count}")
return "\n".join(lines)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Analyze CI/CD pipeline configs and suggest caching improvements.",
epilog="Examples:\n"
" %(prog)s .github/workflows/ci.yml\n"
" %(prog)s --dir .github/workflows/ --json\n"
" %(prog)s .gitlab-ci.yml --severity high",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("files", nargs="*", help="Pipeline YAML files to analyze")
parser.add_argument("--dir", help="Directory to scan recursively for YAML files")
parser.add_argument("--json", action="store_true", dest="json_output",
help="Output results as JSON")
parser.add_argument("--severity", choices=["high", "medium", "low"],
default="low",
help="Minimum severity to report (default: low)")
args = parser.parse_args()
files_to_analyze = []
for f in (args.files or []):
files_to_analyze.extend(collect_files(f))
if args.dir:
files_to_analyze.extend(collect_files(args.dir))
if not files_to_analyze:
parser.error("No files provided. Pass YAML files or use --dir.")
severity_rank = {"high": 3, "medium": 2, "low": 1}
min_rank = severity_rank[args.severity]
results = []
for fpath in files_to_analyze:
result = analyze_file(fpath)
result["suggestions"] = [
s for s in result["suggestions"]
if severity_rank.get(s["severity"], 0) >= min_rank
]
results.append(result)
if args.json_output:
print(json.dumps(results, indent=2))
else:
print(format_human(results))
has_high = any(
s["severity"] == SEVERITY_HIGH
for r in results for s in r["suggestions"]
)
sys.exit(1 if has_high else 0)
if __name__ == "__main__":
main()
#!/usr/bin/env python3
"""Generate CI/CD pipeline YAML from detected project stack signals.
Scans a project directory for lockfiles, manifests, Dockerfiles, and
framework configs, then generates an appropriate GitHub Actions or
GitLab CI pipeline with caching, testing, and deployment stages.
Usage:
python pipeline_generator.py /path/to/project
python pipeline_generator.py . --platform gitlab
python pipeline_generator.py . --platform github --deploy --json
"""
import argparse
import json
import os
import sys
from pathlib import Path
# ---------------------------------------------------------------------------
# Stack detection
# ---------------------------------------------------------------------------
STACK_SIGNALS = [
# (file_or_glob, language, package_manager, framework_hint)
("package-lock.json", "node", "npm", None),
("pnpm-lock.yaml", "node", "pnpm", None),
("yarn.lock", "node", "yarn", None),
("bun.lockb", "node", "bun", None),
("requirements.txt", "python", "pip", None),
("Pipfile", "python", "pipenv", None),
("poetry.lock", "python", "poetry", None),
("uv.lock", "python", "uv", None),
("pyproject.toml", "python", None, None),
("go.mod", "go", "go", None),
("Cargo.lock", "rust", "cargo", None),
("Gemfile.lock", "ruby", "bundler", None),
("composer.lock", "php", "composer", None),
]
FRAMEWORK_SIGNALS = [
("next.config.js", "nextjs"),
("next.config.ts", "nextjs"),
("next.config.mjs", "nextjs"),
("nuxt.config.ts", "nuxt"),
("nuxt.config.js", "nuxt"),
("angular.json", "angular"),
("svelte.config.js", "sveltekit"),
("vite.config.ts", "vite"),
("vite.config.js", "vite"),
]
INFRA_SIGNALS = [
("Dockerfile", "docker"),
("docker-compose.yml", "docker-compose"),
("docker-compose.yaml", "docker-compose"),
]
def detect_stack(project_dir):
"""Detect project stack from filesystem signals."""
root = Path(project_dir)
detected = {
"language": None,
"package_manager": None,
"framework": None,
"has_docker": False,
"has_docker_compose": False,
"has_tests": False,
}
for filename, lang, pm, _fw in STACK_SIGNALS:
if (root / filename).exists():
if detected["language"] is None:
detected["language"] = lang
if pm and detected["package_manager"] is None:
detected["package_manager"] = pm
for filename, fw in FRAMEWORK_SIGNALS:
if (root / filename).exists():
detected["framework"] = fw
break
for filename, kind in INFRA_SIGNALS:
if (root / filename).exists():
if kind == "docker":
detected["has_docker"] = True
elif kind == "docker-compose":
detected["has_docker_compose"] = True
# Detect test presence
test_indicators = [
"tests", "test", "__tests__", "spec",
"pytest.ini", "jest.config.js", "jest.config.ts",
"vitest.config.ts", "vitest.config.js",
]
for indicator in test_indicators:
if (root / indicator).exists():
detected["has_tests"] = True
break
return detected
# ---------------------------------------------------------------------------
# Pipeline templates (GitHub Actions)
# ---------------------------------------------------------------------------
def _github_node(stack, deploy):
pm = stack["package_manager"] or "npm"
install_cmd = {
"npm": "npm ci",
"pnpm": "pnpm install --frozen-lockfile",
"yarn": "yarn install --frozen-lockfile",
"bun": "bun install --frozen-lockfile",
}.get(pm, "npm ci")
setup_steps = " - uses: actions/checkout@v4\n"
if pm == "pnpm":
setup_steps += " - uses: pnpm/action-setup@v4\n with:\n version: '9'\n"
setup_steps += (
" - uses: actions/setup-node@v4\n"
" with:\n"
f" node-version: '20'\n"
)
if pm in ("npm", "pnpm", "yarn"):
setup_steps += f" cache: '{pm}'\n"
setup_steps += f" - run: {install_cmd}\n"
jobs = f""" lint:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
{setup_steps} - run: npx eslint . || true
test:
runs-on: ubuntu-latest
timeout-minutes: 15
needs: lint
steps:
{setup_steps} - run: {"npx vitest run" if stack.get("framework") in ("vite", "nextjs", "sveltekit") else "npm test"}
"""
if stack["has_docker"]:
jobs += f"""
build-image:
runs-on: ubuntu-latest
timeout-minutes: 20
needs: test
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: app:${{{{ github.sha }}}}
cache-from: type=gha
cache-to: type=gha,mode=max
"""
else:
jobs += f"""
build:
runs-on: ubuntu-latest
timeout-minutes: 15
needs: test
steps:
{setup_steps} - run: npm run build
- uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/
retention-days: 3
"""
if deploy:
jobs += """
deploy-staging:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: [build]
runs-on: ubuntu-latest
timeout-minutes: 10
environment:
name: staging
steps:
- uses: actions/checkout@v4
- name: Deploy to staging
run: echo "Add your staging deploy command here"
env:
DEPLOY_TOKEN: ${{ secrets.STAGING_DEPLOY_TOKEN }}
deploy-production:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: deploy-staging
runs-on: ubuntu-latest
timeout-minutes: 10
environment:
name: production
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: echo "Add your production deploy command here"
env:
DEPLOY_TOKEN: ${{ secrets.PROD_DEPLOY_TOKEN }}
"""
return _github_wrapper(jobs)
def _github_python(stack, deploy):
pm = stack["package_manager"] or "pip"
install_block = {
"pip": " - run: pip install -r requirements.txt",
"poetry": " - run: pip install poetry && poetry install --no-interaction",
"uv": " - uses: astral-sh/setup-uv@v4\n - run: uv sync --frozen",
"pipenv": " - run: pip install pipenv && pipenv install --deploy",
}.get(pm, " - run: pip install -r requirements.txt")
setup_steps = (
" - uses: actions/checkout@v4\n"
" - uses: actions/setup-python@v5\n"
" with:\n"
" python-version: '3.12'\n"
)
if pm != "uv":
setup_steps += install_block + "\n"
else:
setup_steps = " - uses: actions/checkout@v4\n" + install_block + "\n"
test_cmd = "uv run pytest -v" if pm == "uv" else "pytest -v"
lint_cmd = "uv run ruff check ." if pm == "uv" else "ruff check . || true"
jobs = f""" lint:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
{setup_steps} - run: {lint_cmd}
test:
runs-on: ubuntu-latest
timeout-minutes: 15
needs: lint
strategy:
matrix:
python-version: ['3.11', '3.12', '3.13']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{{{ matrix.python-version }}}}
{install_block}
- run: {test_cmd}
"""
if stack["has_docker"]:
jobs += """
build-image:
runs-on: ubuntu-latest
timeout-minutes: 20
needs: test
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: app:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
"""
if deploy:
jobs += """
deploy-staging:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: [test]
runs-on: ubuntu-latest
timeout-minutes: 10
environment:
name: staging
steps:
- uses: actions/checkout@v4
- name: Deploy to staging
run: echo "Add your staging deploy command here"
deploy-production:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: deploy-staging
runs-on: ubuntu-latest
timeout-minutes: 10
environment:
name: production
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: echo "Add your production deploy command here"
"""
return _github_wrapper(jobs)
def _github_go(stack, deploy):
jobs = """ lint:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go vet ./...
test:
runs-on: ubuntu-latest
timeout-minutes: 15
needs: lint
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go test -race -coverprofile=coverage.out ./...
build:
runs-on: ubuntu-latest
timeout-minutes: 10
needs: test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go build -o bin/app ./...
- uses: actions/upload-artifact@v4
with:
name: binary
path: bin/
retention-days: 3
"""
return _github_wrapper(jobs)
def _github_wrapper(jobs_block):
return f"""name: CI/CD
on:
push:
branches: [main, dev]
pull_request:
branches: [main, dev]
concurrency:
group: ${{{{ github.workflow }}}}-${{{{ github.ref }}}}
cancel-in-progress: true
permissions:
contents: read
jobs:
{jobs_block}"""
def _github_generic():
return _github_wrapper(""" build:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Build
run: echo "Configure your build steps here"
- name: Test
run: echo "Configure your test steps here"
""")
# ---------------------------------------------------------------------------
# Pipeline templates (GitLab CI)
# ---------------------------------------------------------------------------
def _gitlab_node(stack, deploy):
pm = stack["package_manager"] or "npm"
install = {"npm": "npm ci", "pnpm": "corepack enable && pnpm install --frozen-lockfile",
"yarn": "yarn install --frozen-lockfile", "bun": "bun install"}.get(pm, "npm ci")
cache_path = {"npm": "node_modules/", "pnpm": ".pnpm-store/\n - node_modules/",
"yarn": "node_modules/", "bun": "node_modules/"}.get(pm, "node_modules/")
deploy_block = ""
if deploy:
deploy_block = """
deploy_staging:
stage: deploy
environment:
name: staging
rules:
- if: $CI_COMMIT_BRANCH == "main"
script:
- echo "Add staging deploy command"
deploy_production:
stage: deploy
environment:
name: production
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
needs: [deploy_staging]
script:
- echo "Add production deploy command"
"""
return f"""stages:
- validate
- test
- build
- deploy
default:
image: node:20
cache:
key: ${{CI_COMMIT_REF_SLUG}}
paths:
- {cache_path}
lint:
stage: validate
before_script:
- {install}
script:
- npx eslint . || true
timeout: 10m
test:
stage: test
before_script:
- {install}
script:
- npm test
timeout: 15m
build:
stage: build
before_script:
- {install}
script:
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 hour
{deploy_block}"""
def _gitlab_python(stack, deploy):
pm = stack["package_manager"] or "pip"
install = {"pip": "pip install -r requirements.txt",
"uv": "pip install uv && uv sync --frozen",
"poetry": "pip install poetry && poetry install"}.get(pm, "pip install -r requirements.txt")
return f"""stages:
- validate
- test
- build
- deploy
default:
image: python:3.12
cache:
key: ${{CI_COMMIT_REF_SLUG}}
paths:
- .cache/pip/
lint:
stage: validate
before_script:
- {install}
script:
- ruff check . || true
timeout: 10m
test:
stage: test
before_script:
- {install}
script:
- pytest -v
timeout: 15m
parallel:
matrix:
- PYTHON_VERSION: ["3.11", "3.12", "3.13"]
"""
# ---------------------------------------------------------------------------
# Generator dispatch
# ---------------------------------------------------------------------------
GENERATORS = {
("github", "node"): _github_node,
("github", "python"): _github_python,
("github", "go"): _github_go,
("gitlab", "node"): _gitlab_node,
("gitlab", "python"): _gitlab_python,
}
def generate_pipeline(project_dir, platform="github", deploy=False):
"""Generate a CI/CD pipeline for the given project."""
stack = detect_stack(project_dir)
lang = stack["language"]
gen_fn = GENERATORS.get((platform, lang))
if gen_fn:
yaml_content = gen_fn(stack, deploy)
elif platform == "github":
yaml_content = _github_generic()
else:
yaml_content = "# Could not detect stack. Add pipeline configuration manually.\n"
return {
"stack": stack,
"platform": platform,
"yaml": yaml_content,
}
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Generate CI/CD pipeline YAML from project stack detection.",
epilog="Examples:\n"
" %(prog)s /path/to/project\n"
" %(prog)s . --platform gitlab --deploy\n"
" %(prog)s . --json",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_dir", nargs="?", default=".",
help="Project directory to scan (default: current directory)")
parser.add_argument("--platform", choices=["github", "gitlab"],
default="github", help="Target CI platform (default: github)")
parser.add_argument("--deploy", action="store_true",
help="Include deployment stages (staging + production)")
parser.add_argument("--json", action="store_true", dest="json_output",
help="Output results as JSON (includes detected stack info)")
parser.add_argument("--detect-only", action="store_true",
help="Only detect stack, do not generate pipeline")
args = parser.parse_args()
project_dir = os.path.abspath(args.project_dir)
if not os.path.isdir(project_dir):
print(f"Error: '{project_dir}' is not a valid directory.", file=sys.stderr)
sys.exit(1)
if args.detect_only:
stack = detect_stack(project_dir)
if args.json_output:
print(json.dumps({"project_dir": project_dir, "stack": stack}, indent=2))
else:
print(f"Project: {project_dir}")
print(f"Language: {stack['language'] or 'unknown'}")
print(f"Package Manager: {stack['package_manager'] or 'unknown'}")
print(f"Framework: {stack['framework'] or 'none'}")
print(f"Docker: {'yes' if stack['has_docker'] else 'no'}")
print(f"Tests detected: {'yes' if stack['has_tests'] else 'no'}")
sys.exit(0)
result = generate_pipeline(project_dir, platform=args.platform, deploy=args.deploy)
if args.json_output:
print(json.dumps({
"project_dir": project_dir,
"stack": result["stack"],
"platform": result["platform"],
"yaml": result["yaml"],
}, indent=2))
else:
print(f"# Detected stack: {result['stack']['language'] or 'unknown'}"
f" / {result['stack']['package_manager'] or 'unknown'}"
f" / {result['stack']['framework'] or 'none'}")
print(f"# Platform: {result['platform']}")
print(f"# Docker: {'yes' if result['stack']['has_docker'] else 'no'}")
print()
print(result["yaml"])
sys.exit(0)
if __name__ == "__main__":
main()
#!/usr/bin/env python3
"""Lint GitHub Actions and GitLab CI YAML files for common CI/CD issues.
Checks for missing permissions, hardcoded secrets, missing timeouts,
unpinned actions, missing concurrency controls, and more.
Usage:
python pipeline_linter.py .github/workflows/ci.yml
python pipeline_linter.py .gitlab-ci.yml --json
python pipeline_linter.py --dir .github/workflows/
"""
import argparse
import json
import os
import re
import sys
from pathlib import Path
# ---------------------------------------------------------------------------
# Rule definitions
# ---------------------------------------------------------------------------
SEVERITY_ERROR = "error"
SEVERITY_WARNING = "warning"
SEVERITY_INFO = "info"
def _lines_containing(text, pattern):
"""Return list of (line_number, line_text) tuples matching *pattern*."""
results = []
for idx, line in enumerate(text.splitlines(), start=1):
if re.search(pattern, line):
results.append((idx, line.strip()))
return results
def check_hardcoded_secrets(text, _platform):
"""Detect potential hardcoded secrets or tokens in the YAML."""
findings = []
patterns = [
(r'(?i)(password|secret|token|api[_-]?key)\s*[:=]\s*["\']?[A-Za-z0-9+/=]{16,}',
"Possible hardcoded secret value"),
(r'(?i)(ghp_|gho_|github_pat_|sk-|AKIA)[A-Za-z0-9]{10,}',
"Possible hardcoded API token"),
(r'(?i)BEGIN\s+(RSA|DSA|EC|OPENSSH)\s+PRIVATE\s+KEY',
"Private key embedded in pipeline file"),
]
for pat, msg in patterns:
for lineno, line in _lines_containing(text, pat):
findings.append({
"rule": "no-hardcoded-secrets",
"severity": SEVERITY_ERROR,
"line": lineno,
"message": f"{msg}: {line[:80]}",
})
return findings
def check_unpinned_actions(text, platform):
"""Warn when GitHub Actions use @main or @master instead of a pinned SHA/tag."""
if platform != "github-actions":
return []
findings = []
pat = r'uses:\s+([^@\s]+)@(main|master)\s*$'
for lineno, line in _lines_containing(text, pat):
findings.append({
"rule": "pin-action-versions",
"severity": SEVERITY_WARNING,
"line": lineno,
"message": f"Action pinned to mutable branch: {line}",
})
return findings
def check_missing_timeout(text, platform):
"""Flag jobs that lack a timeout setting."""
findings = []
if platform == "github-actions":
# Heuristic: look for 'runs-on' (marks a job) without a nearby timeout-minutes
in_job = False
job_name = ""
job_line = 0
has_timeout = False
for idx, line in enumerate(text.splitlines(), start=1):
stripped = line.strip()
if re.match(r'^[a-zA-Z0-9_-]+:\s*$', stripped) or re.match(r'^[a-zA-Z0-9_-]+:$', stripped):
if in_job and not has_timeout:
findings.append({
"rule": "require-timeout",
"severity": SEVERITY_WARNING,
"line": job_line,
"message": f"Job '{job_name}' has no timeout-minutes (default is 6 hours)",
})
in_job = False
has_timeout = False
if "runs-on:" in stripped:
in_job = True
job_name = stripped
job_line = idx
if "timeout-minutes:" in stripped:
has_timeout = True
if in_job and not has_timeout:
findings.append({
"rule": "require-timeout",
"severity": SEVERITY_WARNING,
"line": job_line,
"message": f"Job '{job_name}' has no timeout-minutes (default is 6 hours)",
})
elif platform == "gitlab-ci":
if "timeout:" not in text:
findings.append({
"rule": "require-timeout",
"severity": SEVERITY_INFO,
"line": 1,
"message": "No global or per-job timeout set (GitLab default is 1 hour)",
})
return findings
def check_missing_concurrency(text, platform):
"""Flag GitHub Actions workflows missing a concurrency group."""
if platform != "github-actions":
return []
if "concurrency:" not in text:
return [{
"rule": "require-concurrency",
"severity": SEVERITY_WARNING,
"line": 1,
"message": "Workflow has no concurrency group; duplicate runs can waste resources",
}]
return []
def check_missing_permissions(text, platform):
"""Flag GitHub Actions workflows without explicit permissions."""
if platform != "github-actions":
return []
if "permissions:" not in text:
return [{
"rule": "require-permissions",
"severity": SEVERITY_WARNING,
"line": 1,
"message": "No explicit permissions block; workflow runs with default (often broad) token scope",
}]
return []
def check_missing_path_filters(text, platform):
"""Info-level hint when pushes trigger on all paths."""
if platform != "github-actions":
return []
if re.search(r'on:\s*\n\s+push:', text) and "paths" not in text:
return [{
"rule": "suggest-path-filters",
"severity": SEVERITY_INFO,
"line": 1,
"message": "No path filters; documentation-only changes will trigger full CI",
}]
return []
def check_artifact_retention(text, platform):
"""Warn when upload-artifact has no retention-days."""
findings = []
if platform == "github-actions":
in_upload = False
upload_line = 0
has_retention = False
for idx, line in enumerate(text.splitlines(), start=1):
stripped = line.strip()
if "actions/upload-artifact" in stripped:
if in_upload and not has_retention:
findings.append({
"rule": "set-artifact-retention",
"severity": SEVERITY_WARNING,
"line": upload_line,
"message": "upload-artifact without retention-days; artifacts kept for 90 days by default",
})
in_upload = True
upload_line = idx
has_retention = False
if in_upload and "retention-days:" in stripped:
has_retention = True
if in_upload and stripped.startswith("- ") and "upload-artifact" not in stripped:
if not has_retention:
findings.append({
"rule": "set-artifact-retention",
"severity": SEVERITY_WARNING,
"line": upload_line,
"message": "upload-artifact without retention-days; artifacts kept for 90 days by default",
})
in_upload = False
has_retention = False
elif platform == "gitlab-ci":
if "artifacts:" in text and "expire_in:" not in text:
findings.append({
"rule": "set-artifact-retention",
"severity": SEVERITY_WARNING,
"line": 1,
"message": "Artifacts defined without expire_in; storage costs grow indefinitely",
})
return findings
def check_deploy_without_gate(text, platform):
"""Flag deploy jobs that lack environment protection or branch guards."""
findings = []
if platform == "github-actions":
for idx, line in enumerate(text.splitlines(), start=1):
if re.search(r'deploy.*production', line, re.IGNORECASE):
# Check next ~15 lines for environment or if guard
block = "\n".join(text.splitlines()[idx:idx + 15])
if "environment:" not in block and 'if:' not in block:
findings.append({
"rule": "gate-production-deploy",
"severity": SEVERITY_ERROR,
"line": idx,
"message": "Production deploy job lacks environment gate or branch condition",
})
elif platform == "gitlab-ci":
for idx, line in enumerate(text.splitlines(), start=1):
if re.search(r'deploy.*production', line, re.IGNORECASE):
block = "\n".join(text.splitlines()[idx:idx + 15])
if "when: manual" not in block and "rules:" not in block:
findings.append({
"rule": "gate-production-deploy",
"severity": SEVERITY_ERROR,
"line": idx,
"message": "Production deploy job lacks manual gate or rules guard",
})
return findings
# ---------------------------------------------------------------------------
# Platform detection & orchestration
# ---------------------------------------------------------------------------
ALL_CHECKS = [
check_hardcoded_secrets,
check_unpinned_actions,
check_missing_timeout,
check_missing_concurrency,
check_missing_permissions,
check_missing_path_filters,
check_artifact_retention,
check_deploy_without_gate,
]
def detect_platform(filepath, text):
"""Determine CI platform from path and content."""
name = os.path.basename(filepath)
parent = os.path.basename(os.path.dirname(filepath))
if parent == "workflows" or ".github" in filepath:
return "github-actions"
if name == ".gitlab-ci.yml" or "stages:" in text:
return "gitlab-ci"
# Fallback heuristic
if "runs-on:" in text:
return "github-actions"
if "image:" in text and "script:" in text:
return "gitlab-ci"
return "unknown"
def lint_file(filepath):
"""Run all checks on a single file. Returns dict with findings."""
filepath = str(filepath)
try:
with open(filepath, "r", encoding="utf-8") as f:
text = f.read()
except (OSError, UnicodeDecodeError) as exc:
return {
"file": filepath,
"platform": "unknown",
"findings": [{
"rule": "file-read-error",
"severity": SEVERITY_ERROR,
"line": 0,
"message": str(exc),
}],
}
platform = detect_platform(filepath, text)
findings = []
for check_fn in ALL_CHECKS:
findings.extend(check_fn(text, platform))
findings.sort(key=lambda f: (f["line"], f["severity"]))
return {"file": filepath, "platform": platform, "findings": findings}
def collect_files(path):
"""Collect YAML files from a file path or directory."""
p = Path(path)
if p.is_file():
return [p]
if p.is_dir():
yamls = sorted(p.glob("**/*.yml")) + sorted(p.glob("**/*.yaml"))
return yamls
return []
# ---------------------------------------------------------------------------
# Output formatting
# ---------------------------------------------------------------------------
_SEVERITY_SYMBOL = {
SEVERITY_ERROR: "E",
SEVERITY_WARNING: "W",
SEVERITY_INFO: "I",
}
def format_human(results):
"""Return human-readable report string."""
lines = []
total_errors = 0
total_warnings = 0
for result in results:
lines.append(f"\n--- {result['file']} (platform: {result['platform']}) ---")
if not result["findings"]:
lines.append(" No issues found.")
continue
for f in result["findings"]:
sym = _SEVERITY_SYMBOL.get(f["severity"], "?")
lines.append(f" [{sym}] L{f['line']:>4d} {f['rule']}: {f['message']}")
if f["severity"] == SEVERITY_ERROR:
total_errors += 1
elif f["severity"] == SEVERITY_WARNING:
total_warnings += 1
lines.append(f"\nSummary: {total_errors} error(s), {total_warnings} warning(s)")
return "\n".join(lines)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Lint CI/CD pipeline YAML files for common issues.",
epilog="Examples:\n"
" %(prog)s .github/workflows/ci.yml\n"
" %(prog)s --dir .github/workflows/ --json\n"
" %(prog)s .gitlab-ci.yml --severity warning",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("files", nargs="*", help="Pipeline YAML files to lint")
parser.add_argument("--dir", help="Directory to scan recursively for YAML files")
parser.add_argument("--json", action="store_true", dest="json_output",
help="Output results as JSON")
parser.add_argument("--severity", choices=["error", "warning", "info"],
default="info",
help="Minimum severity to report (default: info)")
args = parser.parse_args()
files_to_lint = []
for f in (args.files or []):
files_to_lint.extend(collect_files(f))
if args.dir:
files_to_lint.extend(collect_files(args.dir))
if not files_to_lint:
parser.error("No files provided. Pass YAML files or use --dir.")
severity_rank = {"error": 3, "warning": 2, "info": 1}
min_rank = severity_rank[args.severity]
results = []
for fpath in files_to_lint:
result = lint_file(fpath)
result["findings"] = [
f for f in result["findings"]
if severity_rank.get(f["severity"], 0) >= min_rank
]
results.append(result)
if args.json_output:
print(json.dumps(results, indent=2))
else:
print(format_human(results))
has_errors = any(
f["severity"] == SEVERITY_ERROR
for r in results for f in r["findings"]
)
sys.exit(1 if has_errors else 0)
if __name__ == "__main__":
main()
Related skills
FAQ
Which CI platforms does it support?
GitHub Actions, GitLab CI, CircleCI, and Buildkite.
What deployment strategies does it generate?
Blue-green with instant rollback, canary with percentage-based traffic shifting, and rolling updates with health checks.