# .github/workflows/pr-gates.yaml
# GitHub Actions workflow for PR gates: schema linting, validation, and previews.
name: PR Gates - Lint & Validate Configs
on:
pull_request:
paths:
- 'configs/**'
- 'schema/**'
- '.github/workflows/pr-gates.yaml'
jobs:
lint-schema:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install yamllint jsonschema yq
- name: Lint YAML files
run: |
yamllint configs/*.yaml schema/*.json # Extend for subdirs as needed
- name: Validate schemas against JSON Schema
run: |
# Assuming schema/config-schema.json defines the structure
for config in configs/*.yaml; do
python -c "
import yaml, jsonschema, sys, json
with open('schema/config-schema.json') as f:
schema = json.load(f)
with open('$config') as f:
data = yaml.safe_load(f)
jsonschema.validate(data, schema)
print('✓ $config valid')
"
done
- name: Preview changes (dry-run)
run: |
# Optional: Simulate apply (e.g., kubectl dry-run for K8s configs)
# kubectl apply -f configs/ --dry-run=client -o yaml | diff -u expected actual
echo "Dry-run passed (customize for your target system)"
security-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
# .github/workflows/deploy.yaml
# Deploys merged configs from main branch to your runtime (e.g., via GitOps tool).
name: Deploy Configs from Git (Source of Truth)
on:
push:
branches: [ main ]
paths:
- 'configs/**'
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # For git history/rollbacks
- name: Tag release
run: |
git tag -a v$(date +%Y%m%d)-$(git rev-parse --short HEAD) -m "Auto-release $(git rev-parse --short HEAD)"
git push origin --tags
- name: Deploy to runtime
env:
# Secrets: e.g., KUBECONFIG, ARGOCD_TOKEN
KUBECONFIG: ${{ secrets.KUBECONFIG }}
run: |
# Example for Kubernetes/ArgoCD/Flux GitOps
# argocd app sync my-app --from configs/ --prune
# Or Flux: flux reconcile kustomization my-configs
# Generic: rsync -av configs/ user@prod-server:/etc/myapp/configs/
echo "Deploying configs/ to production (implement your sync logic)"
# Validate post-deploy
yamllint configs/*.yaml
- name: Notify on success
uses: slackapi/slack-github-action@v1.26.0
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
with:
payload: |
{
"text": "🚀 Configs deployed from ${GITHUB_SHA}. View: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
}
// schema/config-schema.json
// JSON Schema for your configs (single source of truth for structure).
// Customize fields: e.g., for feature flags, DB configs, etc.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["version", "environment", "features"],
"properties": {
"version": {
"type": "string",
"pattern": "^v[0-9]+\\.[0-9]+\\.[0-9]+$"
},
"environment": {
"type": "string",
"enum": ["dev", "staging", "prod"]
},
"features": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"enabled": { "type": "boolean" },
"rollout": { "type": "number", "minimum": 0, "maximum": 100 },
"description": { "type": "string" }
},
"required": ["enabled"]
}
},
"database": {
"type": "object",
"properties": {
"host": { "type": "string" },
"port": { "type": "integer", "minimum": 1 }
}
}
},
"additionalProperties": false
}
# configs/prod.yaml
# Example config file. All changes via PRs -> gates -> merge -> auto-deploy.
# Rollback: git revert HEAD or checkout tag.
version: v1.2.0
environment: prod
features:
new_ui:
enabled: true
rollout: 100
description: "Full rollout of new UI"
analytics:
enabled: false
database:
host: prod-db.example.com
port: 5432
Setup Instructions (Git-based Config Source of Truth)
1. Repo Structure
my-config-repo/
├── configs/ # Source of truth: env-specific YAMLs (e.g., prod.yaml, staging.yaml)
├── schema/ # Schemas for linting/validation
│ └── config-schema.json
├── .github/workflows/
│ ├── pr-gates.yaml # PR checks: lint + validate
│ └── deploy.yaml # Merge-to-main: tag + deploy
└── README.md # Docs below
2. PR Workflow (Gates)
- Open PR targeting
main.
- Gates auto-run:
- YAML linting (yamllint): Syntax + style.
- Schema validation (jsonschema): Enforces structure (e.g., required fields, types).
- Security scan (Trivy): Detect secrets/vulns.
- Dry-run preview: Customize for your system (e.g.,
terraform plan, kubectl --dry-run).
- PR blocks merge until ✅.
3. Deployment (GitOps Style)
- Merge PR → Triggers
deploy.yaml.
- Tags release (e.g.,
v20240101-abc123).
- Syncs
configs/ to runtime:
| Runtime | Tool Example |
|---|
| Kubernetes | ArgoCD/Flux: Watch repo, auto-sync configs/ |
| Servers | Ansible/rsync: rsync configs/ prod:/etc/ |
| Cloud | Terraform: configs/*.tf + plan/apply |
| Apps | Consul/Etcd: Git -> API push |
4. Rollbacks (Git-Powered)
- Instant rollback:
git revert HEAD && git push → Auto-redeploy.
- To tag:
git checkout v20231231-def456 && git push (or tag/PR).
- History:
git log --oneline configs/prod.yaml for audit.
- No DB drift: Runtime always pulls from Git HEAD/main.
5. Customization & Best Practices
- Multi-env: Use branches (
staging, prod) or dir-based (configs/prod/).
- Secrets: Never in Git! Use external vaults (AWS SSM, Vault) + refs in schema.
- Advanced linting: Add
spectral for API schemas, kubeval/kics for K8s.
- Webhooks: For non-GitHub, use GitLab CI or Bitbucket Pipelines (similar YAML).
- Monitoring: Post-deploy, diff runtime vs. Git (e.g., ArgoCD status).
- Access: RBAC via Git perms (require approvals for prod PRs).
Paste these into a new GitHub repo. Customize deploy step for your stack. This ensures Git = Source of Truth: auditable, versioned, safe changes only via PRs. Questions? 🔍