Use the CLI in CI pipelines
A common pattern: report differences on every pull request, then apply after merge once someone approves the production environment.
On this page
Before you start
- The runner must reach your databases. Hosted CI runners use changing public IP addresses, so most teams use a self-hosted runner inside their network.
- The runner also needs HTTPS access to
api.dbhydrate.com, directly or through a proxy. - Keep the exported file in your repository if you like, since it is encrypted. Keep the passphrase, passwords and license key in the CI system’s secret store, never next to the file.
- Run one job at a time per seat, using your CI system’s concurrency or resource lock.
- Put a human approval in front of
applyto production.
Secrets to create
| Secret | Value | Used by |
|---|---|---|
DBHYDRATE_CONN_PASSPHRASE | The passphrase chosen at export | Every command |
DBHYDRATE_TARGET_PASSWORD | The target database password, if the file has none | Every command |
DBHYDRATE_LICENSE_KEY | The license key of the seat that exported the file | apply only |
GitHub Actions
# .github/workflows/database.yml
on:
pull_request:
push: { branches: [main] }
concurrency: dbhydrate-prod # one run at a time per seat
jobs:
compare:
if: github.event_name == 'pull_request'
runs-on: self-hosted # must reach your databases
steps:
- uses: actions/checkout@v4
- run: ./dbhydrate compare --run release.dbhrun --report report.md --no-fail-on-drift
- uses: actions/upload-artifact@v4
with: { name: schema-report, path: report.md }
env:
DBHYDRATE_CONN_PASSPHRASE: ${{ secrets.DBHYDRATE_CONN_PASSPHRASE }}
DBHYDRATE_TARGET_PASSWORD: ${{ secrets.PROD_DB_PASSWORD }}
apply:
if: github.event_name == 'push'
runs-on: self-hosted
environment: production # a reviewer approves before this job runs
steps:
- uses: actions/checkout@v4
- run: ./dbhydrate dry-run --run release.dbhrun --report dry-run.md --wait 10m
- run: ./dbhydrate apply --run release.dbhrun --confirm-target orders_prod --report apply.md --wait 10m
- uses: actions/upload-artifact@v4
if: always()
with: { name: apply-reports, path: '*.md' }
env:
DBHYDRATE_CONN_PASSPHRASE: ${{ secrets.DBHYDRATE_CONN_PASSPHRASE }}
DBHYDRATE_TARGET_PASSWORD: ${{ secrets.PROD_DB_PASSWORD }}
DBHYDRATE_LICENSE_KEY: ${{ secrets.DBHYDRATE_LICENSE_KEY }}Set up the production environment in the repository settings with required reviewers. The dbhydrate binary is assumed to be on the runner; download and verify it in an earlier step if not.
GitLab CI
# .gitlab-ci.yml
schema_compare:
tags: [dbhydrate] # a runner inside your network
resource_group: dbhydrate-prod # one run at a time per seat
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- ./dbhydrate compare --run release.dbhrun --report report.md --no-fail-on-drift
artifacts:
paths: [report.md]
schema_apply:
tags: [dbhydrate]
resource_group: dbhydrate-prod
environment: production
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual # someone starts the apply
script:
- ./dbhydrate dry-run --run release.dbhrun --report dry-run.md --wait 10m
- ./dbhydrate apply --run release.dbhrun --confirm-target orders_prod --report apply.md --wait 10m
artifacts:
when: always
paths: ["*.md"]Set DBHYDRATE_CONN_PASSPHRASE, DBHYDRATE_TARGET_PASSWORD and DBHYDRATE_LICENSE_KEY as masked, protected CI/CD variables. Protected variables are only given to protected branches, which keeps the license key away from merge-request pipelines.
Azure DevOps
# azure-pipelines.yml
trigger: [main]
pool: dbhydrate-agents # self-hosted agents inside your network
stages:
- stage: Compare
jobs:
- job: compare
steps:
- script: ./dbhydrate compare --run release.dbhrun --report $(Build.ArtifactStagingDirectory)/report.md --no-fail-on-drift
env:
DBHYDRATE_CONN_PASSPHRASE: $(DBHYDRATE_CONN_PASSPHRASE)
DBHYDRATE_TARGET_PASSWORD: $(PROD_DB_PASSWORD)
- publish: $(Build.ArtifactStagingDirectory)
artifact: schema-report
- stage: Apply
jobs:
- deployment: apply
environment: production # approvals and an exclusive lock on the environment
strategy:
runOnce:
deploy:
steps:
- checkout: self
- script: |
./dbhydrate dry-run --run release.dbhrun --report dry-run.md --wait 10m
./dbhydrate apply --run release.dbhrun --confirm-target orders_prod --report apply.md --wait 10m
env:
DBHYDRATE_CONN_PASSPHRASE: $(DBHYDRATE_CONN_PASSPHRASE)
DBHYDRATE_TARGET_PASSWORD: $(PROD_DB_PASSWORD)
DBHYDRATE_LICENSE_KEY: $(DBHYDRATE_LICENSE_KEY)Store the secrets as secret pipeline variables or in a variable group. Secret variables must be mapped into env explicitly, as shown. Add approvals and an exclusive lock to the production environment.
Other CI systems
Jenkins, TeamCity, Bitbucket Pipelines or a scheduled task work the same way: put the secrets in the environment, run the command, act on the exit code and keep the report. See exit codes and reports.
Common patterns
| Goal | How |
|---|---|
| Fail a pull request on any drift | compare without --no-fail-on-drift |
| Report drift without failing | compare --no-fail-on-drift --report report.md, then publish the report |
| Nightly drift check | A scheduled job running compare, alerting on exit code 1 |
| Safe release | dry-run then apply in the same approved job |
| Queue instead of failing on a busy seat | --wait 10m plus the CI system’s concurrency setting |