Encrypt Secrets in Git with Mozilla SOPS and age: CI/CD & GitOps Best Practices

Security tutorial - IT technology blog
Security tutorial - IT technology blog

The Problem: Plaintext Secrets in Git Never Stay Secret

After my server got hit by SSH brute-force attacks at midnight, security became part of every initial server setup I do. That shift forced me to examine how I handled application secrets—database passwords, API tokens, and TLS keys—across my repositories and deployment pipelines.

Most teams hit the same wall sooner or later. You add .env to .gitignore to keep credentials out of version control. Then a teammate pings you on Slack: “Hey, can you send me the staging env vars?” Within a week, passwords are scattered across chat threads, emails, and local notes. Worse, one rushed git add . can push a production database password into Git history, where automated scanners on public repos can spot leaked tokens in under a minute.

Running a dedicated secrets server like HashiCorp Vault solves that, yet it is heavy for smaller setups. Managing HA storage, TLS certificates, and unsealing workflows is overkill when you only need to deploy five or ten services via CI/CD or GitOps. You want config files that live next to your code, pass through pull requests, and roll back cleanly—without exposing plaintext values.

Core Concepts: Why Mozilla SOPS and age Fit Together

Across small VPS deployments and multi-node Kubernetes clusters, pairing Mozilla SOPS (Secrets OPerationS) with age (Actually Good Encryption) has been the most reliable pattern I have used. Both tools ship as single static binaries—roughly 30 MB combined—with no background daemon to babysit.

What Mozilla SOPS Actually Does

Instead of turning an entire file into an opaque binary blob, SOPS parses structured formats like YAML, JSON, .env, and INI. It encrypts only the values while leaving every key in plaintext.

That single design choice keeps Git diffs useful. When someone rotates a Stripe API key or adds a REDIS_URL variable, the pull request shows the exact key that changed without revealing the secret itself.

Why Choose age Over GPG

SOPS integrates with AWS KMS, GCP KMS, Azure Key Vault, and GPG. Anyone who has debugged broken GPG keyrings or pinentry prompts inside a minimal Alpine CI container knows how painful that gets. age keeps things simple:

  • Single-line keys: Public and private keys are plain text strings, not sprawling keyring databases.
  • One-variable CI setup: Pass a single private key via the SOPS_AGE_KEY environment variable in your pipeline.
  • Effortless multi-recipient sharing: Encrypting a file for three engineers and two environments takes a comma-separated list of public keys.

Hands-On Practice: Setting Up SOPS and age

Here is the exact setup I use to keep secrets version-controlled in Git while keeping local work and automated deploys fast.

Tip 1: Install the Binaries and Generate a Dedicated age Key

Grab both tools first. On macOS or Ubuntu/Debian, installation takes less than a minute:

# macOS with Homebrew
brew install sops age

# Ubuntu / Debian (age via apt, SOPS from GitHub releases)
sudo apt-get update && sudo apt-get install -y age
curl -LO https://github.com/getsops/sops/releases/download/v3.9.0/sops-v3.9.0.linux.amd64
sudo mv sops-v3.9.0.linux.amd64 /usr/local/bin/sops
sudo chmod +x /usr/local/bin/sops

Create the default key directory and generate your key pair. By default, SOPS checks ~/.config/sops/age/keys.txt on Linux and ~/Library/Application Support/sops/age/keys.txt on macOS:

mkdir -p ~/.config/sops/age
age-keygen -o ~/.config/sops/age/keys.txt
chmod 600 ~/.config/sops/age/keys.txt

Pull out your public key so you can share it with teammates and add it to your repo config:

grep "public key" ~/.config/sops/age/keys.txt
# Example output: # public key: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p

Rule of thumb: Never commit keys.txt to Git. Generate separate age keys for your laptop and your production CI/CD runner so revoking one never breaks the other.

Tip 2: Automate Encryption Rules with .sops.yaml

Passing public keys on the command line every time leads to typos. Drop a .sops.yaml file at the root of your repo instead. SOPS reads it automatically and matches files by path:

creation_rules:
  - path_regex: secrets/.*\.enc\.yaml$
    age: >-
      age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p,
      age1syj4k8m2n9p0q3r5t7v9w1x3z5a7c9e1g3i5k7m9p1q3s5u7w9y1z3b5d
  - path_regex: .*\.enc\.env$
    age: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p

Stick to explicit suffixes like *.enc.yaml and *.enc.env. Anyone browsing the repository can tell encrypted files apart from plaintext ones at a glance.

Tip 3: Encrypt Existing Files and Edit Secrets Inline

Say you have a staging config at secrets/app.enc.yaml. Encrypt it in place using the rules from .sops.yaml:

# Create a plaintext file first
cat << 'EOF' > secrets/app.enc.yaml
database:
  host: db.internal
  password: super-secret-db-password
api_key: sk_live_1234567890abcdef
EOF

# Encrypt it in place using .sops.yaml rules
sops -e -i secrets/app.enc.yaml

Open secrets/app.enc.yaml afterward and you will see the YAML keys intact while every value is wrapped in AES256-GCM encryption:

database:
    host: ENC[AES256_GCM,data:...,type:str]
    password: ENC[AES256_GCM,data:...,type:str]
api_key: ENC[AES256_GCM,data:...,type:str]
sops:
    age:
        - recipient: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p
          enc: |
            -----BEGIN AGE ENCRYPTED FILE-----
            ...
            -----END AGE ENCRYPTED FILE-----
    lastmodified: "2026-12-03T10:00:00Z"
    mac: ENC[AES256_GCM,data:...,type:str]
    version: 3.9.0

Never decrypt a file to disk just to change one password. Plaintext copies have a habit of lingering in backups or getting staged by mistake. Run this instead:

sops secrets/app.enc.yaml

Your $EDITOR opens with decrypted values in a temporary buffer. As soon as you save and close, SOPS re-encrypts only the fields you changed.

Tip 4: Decrypt at Runtime in CI/CD and GitOps Pipelines

Store your CI private key as a masked repository secret named SOPS_AGE_KEY in GitHub Actions or GitLab CI. SOPS reads that variable automatically, so your runner needs no keys.txt file on disk.

# Example GitHub Actions deploy step
- name: Deploy with decrypted environment
  env:
    SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
  run: |
    sops -d --output-type dotenv secrets/prod.enc.env > .env.runtime
    docker compose --env-file .env.runtime up -d
    rm -f .env.runtime

Want to avoid writing .env.runtime to disk altogether? Use sops exec-env secrets/prod.enc.env 'docker compose up -d' to inject variables straight into the process environment.

On Kubernetes, GitOps tools like FluxCD integrate with SOPS natively. Create a Kubernetes Secret holding your private key under the age.agekey data field, point your Kustomization to it via spec.decryption.provider: sops, and Flux decrypts your *.enc.yaml manifests in memory before applying them.

Tip 5: Block Accidental Plaintext Commits Before They Happen

Deadline pressure causes sloppy commits. Pair .gitignore exclusions with a lightweight pre-commit check so unencrypted secret files cannot enter Git history:

# .gitignore
*.env
!*.enc.env
secrets/*.yaml
!secrets/*.enc.yaml

You can also drop a three-line check in .git/hooks/pre-commit (or use pre-commit / gitleaks) to reject any staged *.enc.yaml file missing the sops: metadata block:

#!/usr/bin/env bash
for f in $(git diff --cached --name-only | grep '\.enc\.yaml$'); do
  grep -q '^sops:' "$f" || { echo "Error: $f is not encrypted with SOPS!"; exit 1; }
done

Keep Your Git History Clean from Day One

With Mozilla SOPS and age, your team gets readable Git diffs, zero-dependency key management, and direct integration with CI/CD and FluxCD. Generate your age key pair, commit a .sops.yaml file, and lock down your repo before your first production deploy.

Share: