Support Library
Safe DNS Changes in CI/CD
Once DNS lives in code, a pipeline can show every proposed change before it lands and apply it the same way every time. The same pipeline holds credentials that can take your domain offline, so it needs the guard rails you would give a production deploy.
The short answer
On every pull request, run a read-only preview (terraform plan or dnscontrol preview) and show its output to reviewers. Apply only from the main branch, through a protected environment that requires approval, with one run at a time. Give the pipeline short-lived credentials limited to the DNS zones it manages, never let pull requests from forks see them, pin tool and action versions, check for drift on a schedule, and roll back by reverting the commit.
The shape of a safe pipeline
| Stage | Runs on | Credentials | Does |
|---|---|---|---|
| Validate | Every pull request | None | Syntax check (terraform validate, dnscontrol check) |
| Preview | Pull requests from the same repository | Read-only | Shows the exact changes for review |
| Apply | Main branch, after approval | Write, limited to the managed zones | Makes the reviewed change |
| Verify | After apply | None | Queries the authoritative servers for the changed names |
| Drift check | Daily schedule | Read-only | Runs the preview against main and alerts on any difference |
GitHub Actions example
This workflow previews DNSControl changes on pull requests and pushes them from main through a protected environment called dns-production. Configure that environment to require a reviewer and to allow only the main branch, and store the token there as an environment secret.
name: DNS
on:
pull_request:
paths: ["dnsconfig.js", "creds.json"]
push:
branches: [main]
paths: ["dnsconfig.js", "creds.json"]
permissions:
contents: read
jobs:
preview:
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
environment: dns-preview
concurrency:
group: dns-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v7
- run: docker run --rm -v "$PWD:/dns" -w /dns -e CLOUDFLARE_API_TOKEN ghcr.io/stackexchange/dnscontrol:DNSCONTROL_VERSION preview
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_DNS_READ_TOKEN }}
push:
if: github.event_name == 'push'
runs-on: ubuntu-latest
environment: dns-production
concurrency:
group: dns-production
cancel-in-progress: false
steps:
- uses: actions/checkout@v7
- run: docker run --rm -v "$PWD:/dns" -w /dns -e CLOUDFLARE_API_TOKEN ghcr.io/stackexchange/dnscontrol:DNSCONTROL_VERSION push
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_DNS_EDIT_TOKEN }}- Replace DNSCONTROL_VERSION with an exact release, and pin actions to a full commit SHA rather than a tag. Let Dependabot or Renovate propose updates, so a tool upgrade is its own reviewed change.
- The if on the preview job skips pull requests from forks, which must never receive DNS credentials. Do not switch the trigger to pull_request_target to work around this.
- concurrency group on the push job queues pushes, so two merges never apply at the same time. Previews get their own group per pull request: if they shared the production group, a new preview could cancel a push that is waiting its turn. When several pushes queue, GitHub keeps only the newest pending one, which is fine here because it applies the latest main.
- Two tokens: the preview token can only read DNS; the push token can edit DNS on the managed zones only.
For Terraform the shape is the same: terraform plan -out=dns.tfplan on pull requests, and on main a fresh plan that is applied after environment approval. On AWS, Azure and Google Cloud, prefer OIDC federation (permissions: id-token: write and a role that trusts only this repository and environment) over long-lived keys.
GitLab CI example
stages: [preview, push]
.dnscontrol:
image:
name: ghcr.io/stackexchange/dnscontrol:DNSCONTROL_VERSION
entrypoint: [""]
preview:
extends: .dnscontrol
stage: preview
variables:
CLOUDFLARE_API_TOKEN: $CLOUDFLARE_DNS_READ_TOKEN
script:
- dnscontrol preview
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
push:
extends: .dnscontrol
stage: push
resource_group: dns-production
variables:
CLOUDFLARE_API_TOKEN: $CLOUDFLARE_DNS_EDIT_TOKEN
script:
- dnscontrol push
environment:
name: dns-production
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manualProtect the dns-production environment so only maintainers can run the manual push, and store the edit token (CLOUDFLARE_DNS_EDIT_TOKEN) as a protected, masked CI/CD variable so it is only available on protected branches. Merge request pipelines from ordinary feature branches cannot read protected variables, so the preview needs its own read-only token (CLOUDFLARE_DNS_READ_TOKEN), stored as masked but not protected and scoped to read DNS on the managed zones only. Anyone who can push a branch can read that token, so never give it edit rights. resource_group on the push job makes pushes run one at a time without making previews wait.
Before you merge
- The preview lists only the records you meant to change. Treat any unexpected delete as a stop.
- TTLs were lowered in an earlier change if values are changing (see planning a DNS change).
- NS, DS and DNSSEC changes are in a pull request of their own.
- Preview output in logs and comments shows record values only. Never print credentials or creds.json with secrets in it.
Verifying and rolling back
After a push, query the authoritative servers for the changed names, and fail the job if the answer is not the new value.
| Command | What it confirms |
|---|---|
| dig +norecurse @ns1.example.com www.example.com A | The answer published by one authoritative server; repeat for each nameserver |
To roll back, revert the merge commit; the pipeline previews and applies the old records the same way. Keep the scheduled drift check running so dashboard edits are noticed before the next pipeline run overwrites them, and review each difference as described in DNS drift.
How DNS Panopticon helps
DNS Panopticon does not run in your pipeline or hold your credentials. After a deploy, the DNS Propagation Checker confirms the new record from authoritative servers and public resolvers, and Security Posture re-checks the domain's DNS, email and certificate records.
FAQ
Should pull requests from forks run the DNS preview?
Not with credentials. Run only the syntax check for forks, and run the preview after a maintainer has moved the change to a branch in the main repository.
Why limit the pipeline to one run at a time?
Two applies at once can each start from an old view of the zone and undo each other's changes. A concurrency group or resource group queues them.
Is it safe to post the preview as a pull request comment?
Yes, if the repository is private or the records are public anyway. DNS records are published to the world once live, but check that no tool output includes credentials.
How do I roll back a bad DNS change from CI?
Revert the commit and let the pipeline apply the revert. With a short TTL on the changed record, users return to the old value within minutes.
Sources and further reading
- GitHub Docs: deployment environments and protection rules
- GitHub Docs: control the concurrency of workflows and jobs
- GitHub Docs: security hardening for GitHub Actions
- GitHub Docs: OpenID Connect in cloud providers
- GitLab Docs: protected environments
- GitLab Docs: resource groups
- GitLab Docs: merge request pipelines and protected variables
- DNSControl: CI/CD examples