Create a git-based config source of truth: PR gates, schema linting, and rollbacks.
- User
Create a git-based config source of truth: PR gates, schema linting, and rollbacks.
Assistant
The user wants to create a "git-based config source of truth" system. This system needs to include three specific components:
- PR gates: Changes shouldn't just be merged; they need approval or validation before becoming "live".
- Schema linting: The config files (e.g., JSON, YAML) must adhere to a specific structure.
- Rollbacks: If something goes wrong, we need to revert to a previous state.
I need to design a solution architecture for this. The most common and robust way to implement this is using a GitOps workflow with tools like GitHub Actions (or GitLab CI/ArgoCD).
Key Components:
- Git Repository: The storage.
- Config Files: The data (let's assume YAML/JSON for generality).
- Schema Definition: JSON Schema is the standard here.
- CI/CD Pipeline: The engine for PR gates and linting (e.g., GitHub Actions).
- Deployment/Sync Mechanism: How the config gets to the application.
- Rollback Strategy:
git revertor specific deployment tooling.
Step-by-Step Implementation Plan:
-
Repository Structure:
- Needs a place for configs (
/configs). - Needs a place for schemas (
/schemas). - Needs the workflow definition (
/.github/workflows).
- Needs a place for configs (
-
Schema Linting (The "What"):
- Use
json-schemaorcheckovoryamllint. - Let's stick to standard JSON Schema validation using a tool like
ajv-clior a python script, or a dedicated linter action. - Action: Create a simple JSON schema and a sample config.
- Use
-
PR Gates (The "When"):
- GitHub Branch Protection Rules (Require status checks).
- The CI pipeline must run the linting on Pull Requests.
- Action: Define a GitHub Workflow that triggers on
pull_request.
-
Rollbacks (The "How"):
- Rollback is just a revert in Git.
- However, the system needs to support "Atomic Deployments" or versioning.
- Strategy: When a PR is merged to
main, the pipeline "publishes" the config (maybe uploads to S3, updates a ConfigMap in K8s, or triggers a webhook). - To rollback: Open a PR reverting the commit, or use
git revert.
-
Deployment/Sync (Connecting to the app):
- The prompt asks for "Source of Truth", not necessarily the deployment mechanics, but "Rollbacks" imply a change in the running system. I should briefly touch on how the config gets applied.
Refining the Solution (Putting it together):
- Scenario: Managing application feature flags or environment variables.
- Tools: GitHub (for repo/CI), JSON Schema (for validation), Python/Node (for validation logic).
Drafting the Content:
- Phase 1: Structure & Schema. Define the
config.yamlandschema.json. - Phase 2: Validation (Linting). How to check the YAML against the Schema. I'll suggest a script or tool.
- Phase 3: CI/CD (PR Gates). The GitHub Action workflow file.
- Phase 4: The "Apply" & Rollback. Explain the workflow of merging and reverting.
Self-Correction/Edge Cases:
- What if the config contains sensitive data? Mention secrets management briefly (GitOps shouldn't store secrets in plain text), but focus on the config structure requested.
- What format? YAML is most common for config. I will use YAML.
- How to validate?
ajvis good, butyamllintis also good for syntax. I'll combine them: Schema validation (structural logic) + Yamlint (syntax).
Structured Output:
- Concept: Explain the "GitOps" loop.
- Repo Setup: Folder structure.
- Schema: The definition of "Correct".
- Validation: The tool to check.
- Automation (The Gate): The CI pipeline.
- Rollback Procedure: How to fix it.
Let's build a concrete example using GitHub Actions and JSON Schema.
- File:
config/settings.yaml - File:
schema/config-schema.json - File:
.github/workflows/config-validation.yaml - Tool:
ajv-cli(Another JSON Schema Validator) is standard and easy to run in Node.js environments.
Let's write the response following this structure. I will provide code snippets for every critical part.
- Linter choice: I'll use a python script or a generic linter. Actually, using
ajvvia NPM is very standard in GitHub Actions. - Rollback: Explain that "Rollback" =
git revert.
Let's refine the "PR Gate" part. It's not just CI. It's also Branch Protection settings in the UI. I must mention that.
Let's refine "Rollback". Is it just reverting the commit? Yes, but the pipeline needs to redeploy automatically.
Final Plan:
- Architecture: Git -> CI (Lint) -> Merge -> CD (Apply).
- The Schema: JSON Schema.
- The Config: YAML.
- The Linter:
ajvvia a custom script or action. - The Workflow: GitHub Actions YAML.
- The Rollback: Explanation of the revert process.
Let's create the artifacts. This is a blueprint for building a GitOps Workflow where Git is the absolute source of truth for your configuration. This implementation uses industry-standard tools: GitHub (or GitLab/Bitbucket) for hosting, GitHub Actions for the pipeline, JSON Schema for validation, and YAML for the config format.
Architecture Overview
- The Source of Truth: A Git repository containing configuration files (YAML/JSON).
- The Schema: A definition of what valid config looks like.
- The Gate (PR Checks): An automated pipeline that lints and validates the config against the schema before allowing a merge.
- The Rollback: The ability to revert commits to restore previous states.
Step 1: Repository Structure
First, define the structure of your configuration repository.
config-repo/ ├── .github/ │ └── workflows/ │ └── validate-config.yml # The PR Gate logic ├── schemas/ │ └── config.schema.json # The Source of Truth rules ├── config/ │ ├── production.yaml # Actual config data │ └── staging.yaml └── scripts/ └── validate.sh # Helper scriptStep 2: Define the Schema (The "Law")
We use JSON Schema to define strict validation rules. This prevents typo errors or wrong data types (e.g., setting a port number to a string).
File:
schemas/config.schema.json{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": ["service_name", "replicas", "database"], "properties": { "service_name": { "type": "string", "description": "The name of the microservice" }, "replicas": { "type": "integer", "minimum": 1, "maximum": 10, "description": "Number of running instances" }, "debug_mode": { "type": "boolean", "default": false }, "database": { "type": "object", "properties": { "host": { "type": "string" }, "port": { "type": "integer" } }, "required": ["host"] } } }Step 3: Create Sample Config
File:
config/production.yamlservice_name: "payment-api" replicas: 5 debug_mode: false database: host: "db.prod.example.com" port: 5432Step 4: The Validation Script (Local & CI)
To ensure compatibility locally and in CI, we use a simple shell script that utilizes a validation tool. We will use
check-jsonschema, a popular Python tool, but you can also useyamllintorajv.File:
scripts/validate.sh#!/bin/bash # Exit on error set -e echo "Validating YAML configs against Schema..." # Install check-jsonschema if not present (useful in CI) pip install check-jsonschema -q # Find all yaml files in config/ and validate them for file in config/*.yaml; do echo "Checking $file..." check-jsonschema --schemafile schemas/config.schema.json "$file" done echo "All configurations are valid!"Step 5: The PR Gate (GitHub Actions)
This is the core "Gate." It runs automatically when a Pull Request is opened. It blocks the merge if the validation fails.
File:
.github/workflows/validate-config.ymlname: Config Validation Gate on: pull_request: branches: [ "main" ] paths: - 'config/**' # Only trigger if config files change - 'schemas/**' # Or if the schema changes - '.github/workflows/validate-config.yml' jobs: lint-and-validate: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.9' - name: Install Linter run: pip install check-jsonschema yamllint - name: Lint YAML Syntax # Checks for indentation errors, trailing spaces, etc. run: yamllint config/ - name: Validate Schema run: | chmod +x scripts/validate.sh ./scripts/validate.shStep 6: Branch Protection (Enforcing the Gate)
The YAML file above defines how to validate, but a user could technically dismiss the warning and merge anyway. You must enforce this in your Git provider settings.
In GitHub (Settings -> Branches -> Branch Protection Rules -> Main):
- Require status checks to pass before merging: Checked.
- Search for the job name: Select
lint-and-validate(the job name from Step 5). - Require branches to be up to date before merging: Checked.
Now, it is mathematically impossible to merge invalid config into
main.Step 7: Rollback Strategy
In a Git-based source of truth, a "Rollback" is simply a "Revert."
Scenario: Bad Config pushed to Production
- Identify the Error: You merged a PR that set
replicas: 100, crashing the system. - The Rollback Command:
- Via UI: Go to the specific Commit in GitHub/GitLab and click Revert.
- Via CLI:
# Find the bad commit hash git log # Revert the commit locally git revert <bad_commit_hash> # Push the revert git push origin main
- The Pipeline Triggers: The revert commit triggers the pipeline again. It validates that the old (working) config is valid, passes the gate, and deploys the working state.
Step 8: (Optional) The "Apply" Phase
The steps above maintain the truth. To actually apply this truth to your infrastructure (e.g., update a Kubernetes ConfigMap or AWS Parameter Store), you add a
deployjob to your workflow that runs after the validation passes.Add to
validate-config.yml:deploy: needs: lint-and-validate # Only run if validation succeeds runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' # Only run on main branch steps: - name: Checkout uses: actions/checkout@v4 - name: Apply Config to Infrastructure run: | echo "Applying config to Server..." # Example: kubectl apply -f config/production.yaml # Example: aws ssm put-parameter --name "/myapp/config" --value file://config/production.yamlSummary of Workflow
- Change: Developer edits
production.yaml. - PR: Developer opens Pull Request.
- Gate: GitHub Actions runs
yamllintandcheck-jsonschema.- Fail? Merge is blocked. Developer must fix errors.
- Pass? Merge is allowed.
- Merge: Config is merged to
main. - Deploy: (Optional) Pipeline syncs
mainto the live environment. - Rollback:
git revertcreates a new commit that restores the old state, triggering the whole pipeline again automatically.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.