Test data during development
Verify data contracts on every pull request with GitHub Actions, so a change that breaks the data is caught before it merges.
This guide shows how to verify Soda data contracts in a GitHub Actions workflow that runs on every pull request. The workflow builds the dbt models in a CI schema, verifies their contracts, and posts the results to the pull request. When a check fails, the job fails, so the change is caught before it reaches production.
Use the example as a starting point and adapt the data source, models, and contracts to your own repository.
Example repository
A team keeps its dbt models and their Soda contracts in the same repository. One model, fact_product_category, joins products to their subcategories and categories. Its contract requires every product to appear exactly once.
A pull request that changes the model triggers the workflow, which:
Builds the models with dbt in a CI schema, separate from production
Verifies every contract against the CI schema
Writes the results to the job summary and posts them as a comment on the pull request
Fails the job if any check fails
The repository has this layout:
.github/
└── workflows/
└── verify-data-contracts.yml
dbt/
├── dbt_project.yml
├── profiles.yml
└── models/
├── sources.yml
└── fact_product_category.sql
soda/
├── ds_config.yml
└── contracts/
└── fact_product_category.ymlPrerequisites
A GitHub repository with GitHub Actions enabled
A PostgreSQL data source that GitHub-hosted runners can reach, with a schema the workflow can write to for CI builds
A dbt project that builds models in that data source
Configure the data source
Create a data source configuration file in the soda folder. The values in ${env.…} are read from environment variables, which the workflow sets from the repository's secrets and variables.
dbt reads the same environment variables. The CI target builds models in the schema named by CI_SCHEMA, and the sources read from SOURCE_SCHEMA:
For the configuration keys of each data source, see the Data source reference for Soda Core.
Write the contract
The contract uses two variables, DATABASE and SCHEMA, in the dataset name. The workflow sets them to the CI schema, so the same contract file can verify the production table when you pass the production schema instead.
A product without a category isn't an error in this model, so that check warns instead of failing.
To learn more about variables, see Make contracts dynamic with variables.
Create the workflow
Add the workflow file to .github/workflows:
A few details in the workflow:
soda contract verifyexits with2when checks only warn. The verify step treats2as a pass, and1or any other code as a failure. See Exit codes in the CLI reference.The verify step runs every contract before it reports, so one failing contract doesn't hide the results of the others. The last step fails the job.
pull-requests: writelets the workflow post the comment with the built-inGITHUB_TOKEN.concurrencyruns one workflow at a time, because every pull request builds in the same CI schema.
Add the secrets and variables
In the repository, open Settings > Secrets and variables > Actions, and add the values the workflow reads:
POSTGRES_HOST
Secret
Host name of the PostgreSQL server
POSTGRES_USER
Secret
User that builds the CI schema and runs the checks
POSTGRES_PASSWORD
Secret
Password of that user
POSTGRES_DATABASE
Variable
Database name
SOURCE_SCHEMA
Variable
Schema of the source tables
CI_SCHEMA
Variable
Schema where the workflow builds the models
Open a pull request
Open a pull request that changes a model. The workflow runs, and its results appear in three places: the check on the pull request, a comment on the pull request, and the job summary.
When a check fails, the job fails. For example, a change that joins fact_internet_sales into fact_product_category without aggregating it repeats each product once per sale. dbt builds the model without an error, but the contract's Each product appears once check fails, and the pull request shows a failed check.


To block merging while the check fails, make the verify job a required status check in a branch protection rule. See About protected branches in the GitHub documentation.
Next steps
Publish contracts to Soda Cloud when they merge, with the GitHub integration.
Verify the same contracts in your production pipeline. See Test data quality in an Airflow pipeline.
Learn every check type in the Contract Language reference.
Last updated
Was this helpful?
