Sub topic 12
Working in a team and in CI
This chapter assumes you can use dvc add, dvc push, dvc pull, and dvc repro, and that you know Git branches, pull requests, and merge conflicts. It also assumes you can read a GitHub Actions workflow file. It covers how a team shares one DVC project and how a CI job checks it automatically.
The team model: Git for pointers, the remote for data
A DVC project has two stores that must stay in sync:
- Git holds code,
.dvcpointer files,dvc.yaml,dvc.lock,params.yaml, and.dvc/config. - The DVC remote holds the data and model files, addressed by their content hash.
A teammate who clones the repository gets the pointers but not the data. They run dvc pull to fetch the data that the checked-out commit refers to. The rule that follows is the main one in this chapter: a pointer file is only useful if the data it names exists on the shared remote.
The safe push order
Push data first, then code:
dvc push
git push origin my-branchIf you reverse the order, a teammate can fetch your branch while the data is still only in your local cache. Their dvc pull then fails with a "missing cache" error for the hashes in your pointer files. Before you open a pull request, you can check that the remote has everything:
dvc status --clouddvc status --cloud (short form -c) compares your local cache with the default remote. It reports files that are in the cache but not on the remote, and the reverse.
You can automate the push order with Git hooks. dvc install adds hooks that run dvc checkout after git checkout and dvc push before git push. The checkout hook helps when you switch branches often, because the workspace follows the pointers without a manual step. The push hook slows every git push, so some teams prefer to do it in CI instead.
Credentials and configuration
The remote's URL is shared, but credentials are personal. DVC has three config scopes:
| Scope | File | Committed to Git? | Use for |
|---|---|---|---|
| Project (default) | `.dvc/config` | Yes | Remote URL, default remote name |
| Local | `.dvc/config.local` | No (ignored by Git) | Access keys, personal overrides |
| Global | User config directory | No | Settings shared by all your projects |
Use --local for secrets so they never reach a commit:
dvc remote add -d storage s3://my-bucket/dvc-store
dvc remote modify --local storage access_key_id "$AWS_ACCESS_KEY_ID"
dvc remote modify --local storage secret_access_key "$AWS_SECRET_ACCESS_KEY"
git add .dvc/config
git commit -m "Configure default DVC remote"For cloud storage such as S3, it is often simpler to rely on the provider's own credential chain: environment variables, a named profile, or an instance role. DVC uses those without any DVC-specific secret.
Branches and pull requests
A good default is one branch per change, with the data change and the code change in the same pull request. A reviewer then sees small text diffs:
- A
.dvcfile diff shows a changedmd5andsize, which tells the reviewer that the dataset changed. - A
dvc.lockdiff shows which stages reran and which input and output hashes changed. - A
params.yamldiff shows the hyperparameters that changed. - A metrics file diff, or the output of
dvc metrics diff, shows the effect on results.
The reviewer can check out the branch and run dvc pull to get exactly the data and model that the author used.
Sharing experiments without branches
dvc exp run creates experiments that are not branches. To share them, push them to the Git remote under hidden references, and push their data to the DVC remote:
dvc exp run -S train.n_estimators=300
dvc exp push originA teammate can then list and fetch them:
dvc exp list origin
dvc exp pull originAn experiment that looks good can be turned into a normal branch with dvc exp branch, and it then goes through a regular pull request.
Merge conflicts in DVC files
DVC files are text, so Git can conflict on them. The usual cases:
.dvcpointer file. Two branches changed the same dataset, so both edited themd5line. Git cannot merge two hashes. You must pick which dataset version wins, or build a combined dataset and rundvc addagain to make a new pointer.dvc.lock. Two branches reran the same stage. Do not edit hashes by hand. Take either side, or delete the conflict markers in any way that leaves a valid file, then rundvc repro. DVC recomputesdvc.lockfrom the real inputs.dvc.yamlandparams.yaml. These are ordinary YAML. Merge them like code.
After resolving any of them, run dvc checkout so the workspace matches the merged pointers, then dvc status to confirm that nothing is stale.
Running DVC in CI
CI has no local cache and no personal credentials, so each job has to bring its own. A typical job does four things:
- Check out the commit with enough Git history to compare against the base branch.
- Install DVC and configure remote credentials from the CI secret store.
- Run
dvc pullanddvc repro. - Report the result, and optionally push new outputs.
This GitHub Actions workflow follows that shape:
name: dvc-pipeline
on:
pull_request:
push:
branches: [main]
jobs:
reproduce:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- uses: iterative/setup-dvc@v1
- name: Install project dependencies
run: pip install -r requirements.txt
- name: Pull data
run: dvc pull
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Reproduce pipeline
run: dvc repro
- name: Fail if dvc.lock is out of date
run: dvc status --quiet
- name: Compare metrics with main
run: dvc metrics diff origin/main
- name: Push outputs
if: github.ref == 'refs/heads/main'
run: dvc push
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}A few details in this file matter:
fetch-depth: 0fetches the full history. Without it,origin/mainmay not exist in the shallow clone, anddvc metrics diff origin/mainfails.dvc pullbrings in the inputs and, because it pulls everything, the previously committed outputs. DVC's change detection then letsdvc reproskip stages that are still valid.dvc status --quietprints nothing and exits with 1 if any stage is out of date. After a successfuldvc reproit should pass. It is more useful as a separate check when you skipdvc reproand only want to verify that the author committed an up-to-datedvc.lock.- The push step is limited to
mainso that pull requests cannot overwrite shared storage, and so that jobs from forks, which have no access to secrets, do not fail on credentials.
Check only, or retrain?
Retraining in CI is not always sensible. Choose by cost:
| Strategy | What CI runs | Good when | Risk |
|---|---|---|---|
| Verify only | `dvc pull`, `dvc status --quiet` | Training is slow or needs GPUs | Does not prove the code still runs |
| Reproduce | `dvc pull`, `dvc repro` | Pipeline is cheap, a few minutes | CI cost grows with data size |
| Hybrid | Reproduce cheap stages, skip training with `dvc repro prepare` | Preprocessing breaks often | Needs stage names kept stable |
Naming a target, as in dvc repro prepare, reproduces that stage and the stages it depends on. This keeps the job short.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `dvc pull` reports missing cache for some files | The author ran `git push` but not `dvc push` | The author runs `dvc push`. Confirm with `dvc status -c` |
| CI fails with an authentication error | Secrets are not set, or the job runs from a fork | Add secrets in the CI settings, or skip data steps for forks |
| `dvc metrics diff origin/main` fails with an unknown revision | Shallow checkout | Use `fetch-depth: 0` |
| CI reruns every stage every time | `dvc pull` was skipped, so no previous outputs or run-cache entries exist | Pull before `dvc repro` |
| Credentials appear in `git diff` | `dvc remote modify` was run without `--local` | Redo it with `--local` and rotate the key |
| Workspace differs from the branch after `git checkout` | `dvc checkout` was not run | Run `dvc checkout`, or use `dvc install` for the hook |
What to take away
- Treat
dvc pushas part of "publish my change", and run it beforegit push. - Keep the remote URL in
.dvc/configand secrets in.dvc/config.localor the provider's credential chain. - Review pointer, lock, parameter, and metrics diffs in pull requests. Resolve conflicts by choosing a version or by rerunning
dvc repro, never by hand-editing hashes. - In CI, pull before you reproduce, fetch full history for metric comparisons, and push only from trusted branches.
Exercise
A teammate opens a pull request that changes data/raw.csv.dvc. Your CI job fails at dvc pull with a "missing cache" error for the new hash, but the pointer file looks valid. What most likely happened, and what two commands should the author run to fix and verify it?
Show answer
The author committed and pushed the pointer file to Git but never uploaded the new data to the DVC remote, so the hash in the pointer does not exist there. The author should run dvc push to upload the data, then dvc status --cloud to confirm that the cache and the remote match. Re-running the CI job should then pass.
Exercise
Two branches both reran the train stage, and merging them gives a conflict in dvc.lock. Explain how to resolve it, and why you should not edit the hash lines yourself.
Show answer
Resolve the conflict markers by taking either side (or any valid version of the file), then run dvc repro. DVC recomputes the input and output hashes from the actual workspace and rewrites dvc.lock. Hand-edited hashes can describe a combination of inputs and outputs that never existed together, so DVC could treat a stale stage as up to date. Commit the regenerated dvc.lock.
Exercise
Your CI workflow runs dvc metrics diff origin/main and fails with an unknown revision error. Name the cause and the fix. Then say why the dvc push step in the example is limited to the main branch.
Show answer
The checkout is shallow, so origin/main is not available locally. Set fetch-depth: 0 on actions/checkout. The push step is limited to main so that unreviewed pull requests cannot write to the shared remote, and so that jobs from forks, which have no access to secrets, do not fail on credentials.