Skip to content

CI/CD for Netherframe Apps

Run the same hybrid plan-and-apply workflow in CI that you run locally: build and verify the app, save an immutable whole-app plan artifact in the review job, and apply that exact file in the approved deployment job.

For a local hands-on rehearsal first, see Tutorial: Review and apply a saved plan.


flowchart LR
    A["Pull Request"] --> B["pytest + nf lint"]
    B --> C["nf plan --no-user-config --out plan.json --diff"]
    C --> D["Upload plan.json & post diff"]
    D --> E{"PR merged / Approved"}
    E --> F["nf apply --plan-file plan.json --auto-approve --json"]
    F --> G["Scheduled: nf plan --expect-no-changes"]

Key principles for CI jobs:

  • Pass --no-user-config so ~/.config/nf/config.toml on a runner can never affect a job.
  • Keep CI=true (set automatically by most CI systems): nf switches from live terminal redraws to one line per event (equivalent to --non-interactive).
  • Use --json when downstream steps parse plan/apply summaries or post PR comments.
  • Treat saved plans like state files: they can contain resource attributes and Terraform-state snapshots. Store them only in access-controlled job artifacts with short retention.
  • Handle disambiguated exit codes: 0 ok, 1 error, 2 usage error, 3 changes or drift detected (--expect-no-changes), 4 incomplete apply with deferred changes remaining (Exit Codes).

GitHub Actions example

This workflow runs offline tests, AWS linting, and a PR plan on pull requests; applies the reviewed plan on main; and runs a nightly drift check.

name: Infrastructure

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
  schedule:
    - cron: "0 6 * * *"

permissions:
  id-token: write
  contents: read

jobs:
  plan:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/setup-uv@v5
      - run: uv sync --locked --inexact

      - name: Offline unit tests and policy lint
        run: |
          uv run pytest
          uv run nf --no-user-config lint --app infra.app:web --json

      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ secrets.AWS_PLAN_ROLE_ARN }}
          aws-region: us-east-1

      - name: Save reviewed plan
        run: |
          uv run nf --no-user-config plan \
            --app web ++env=prod \
            --diff \
            --out prod-plan.json

      - uses: actions/upload-artifact@v4
        with:
          name: prod-plan-${{ github.sha }}
          path: prod-plan.json
          retention-days: 3

  apply:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/setup-uv@v5
      - run: uv sync --locked --inexact

      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ secrets.AWS_APPLY_ROLE_ARN }}
          aws-region: us-east-1

      - name: Plan and apply (or download approved artifact)
        run: |
          uv run nf --no-user-config plan --app web ++env=prod --out prod-plan.json
          uv run nf --no-user-config apply \
            --app web \
            --plan-file prod-plan.json \
            --auto-approve \
            --json > apply-result.json

  drift:
    if: github.event_name == 'schedule'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/setup-uv@v5
      - run: uv sync --locked --inexact

      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ secrets.AWS_PLAN_ROLE_ARN }}
          aws-region: us-east-1

      - name: Check for drift
        run: |
          set +e
          uv run nf --no-user-config plan --app web ++env=prod --expect-no-changes --diff --json > drift.json
          code=$?
          if [ "$code" -eq 3 ]; then
            echo "::warning::Infrastructure drift detected in prod"
            exit 3
          fi
          exit "$code"

GitLab CI example

stages: [validate, plan, apply]

default:
  image: ghcr.io/astral-sh/uv:python3.15-bookworm
  before_script:
    - uv sync --locked --inexact

validate:
  stage: validate
  script:
    - uv run pytest
    - uv run nf --no-user-config lint --app infra.app:web

plan:prod:
  stage: plan
  script:
    - uv run nf --no-user-config plan --app web ++env=prod --diff --out prod-plan.json
  artifacts:
    paths: [prod-plan.json]
    expire_in: 3 days

apply:prod:
  stage: apply
  needs: [plan:prod]
  when: manual
  environment:
    name: production
  script:
    - uv run nf --no-user-config apply --app web --plan-file prod-plan.json --auto-approve --json > apply.json

Passing secrets and credentials safely in CI

Netherframe rejects Secret[T] app inputs on CLI argv and never takes PostgreSQL passwords as flags. Use process substitution (<(...)) or mounted secret files so values never appear in process listings or shell history:

# Pass Secret[str] app inputs without writing a file to disk
nf --no-user-config apply --app identity --auto-approve \
  ++@<(printf '{"client_secret": "%s"}' "$KEYCLOAK_CLIENT_SECRET")

# Pass PostgreSQL state password via file descriptor
nf --no-user-config --postgresql-password-file <(printf '%s' "$PG_PASSWORD") \
  apply --app parameters --auto-approve

See Secrets Management for full details.


Unattended safety opt-ins

--auto-approve is deliberately conservative in CI. Certain high-impact actions require explicit opt-in flags on top of --auto-approve:

Condition Default under --auto-approve Opt-in flag
Plan includes import actions Stops before mutating anything --approve-imports
Plan has deferred provider rounds Applies round 1, stops incomplete (exit 4) --apply-deferred --deferred-approval auto
Object deleted outside Netherframe Refuses recreation until reviewed --deleted-objects recreate
Undeclared stack deleted Refuses unless applying a saved --plan-file Save, review, and pass --plan-file

Watching a CI apply live

Because direct runs write live status beside state (run_status = true by default), an operator with read access to the backend can watch a CI apply resource by resource from their own terminal:

nf follow --app web ++env=prod
# or open the live run in the terminal UI:
nf tui follow --app web ++env=prod

Saved plan integrity and optional HMAC signing

Every saved plan embeds a SHA-256 content_digest, state serial/lineage/state_fingerprint, provider fingerprints, input digests, and declared app-check names. For high-assurance pipelines that pass plan.json across untrusted artifact storage, Python SDK wrappers can also sign and verify plan artifacts with HMAC-SHA256 (nf-plan-sig-v1):

from nf.core.execution.hybrid_plan import plan_content_digest, sign_plan, verify_plan_signature

sample = {"format_version": 8, "app_id": "web", "stacks": {}}
sample["content_digest"] = plan_content_digest(sample)
key = b"ci-plan-signing-secret"
sample["signature"] = sign_plan(sample, key)
verify_plan_signature(sample, key)

Legacy Terraform/OpenTofu compatibility pipeline

If an existing pipeline runs Terraform or OpenTofu directly, register the app in [tool.nf].terraform_apps, run nf synth, and hand .nf.out/<app>/<stack>/ to your existing runner. See Lifecycle procedures.