Ir para o conteúdo

Lição 21 · Collaboration and CI

Run DVC in CI/CD Pipelines

Check pipelines and data in CI: install DVC in GitHub Actions with setup-dvc, pull data safely with secrets, verify pushes, reproduce stages and post metrics.

Continuous integration (CI) runs checks automatically when code changes. With DVC, CI can also check data and pipelines: that tracked data was pushed, that the pipeline is consistent, and how metrics moved. This lesson uses GitHub Actions as the example, but the commands work on any CI system.

What to check

Start small and add checks when they prove useful.

Levels of CI checks

Data pushed Nothing missing in storage
Pipeline current No stage left to rerun
Metrics Changes shown on the PR
Train in CI Only if cheap and safe
1 / 2

Two commands matter most:

Terminal
# Read-only: check that data is pushed and see what would run
dvc data status --not-in-remote
dvc repro --dry --allow-missing

The documentation suggests the dry run with --allow-missing to see which stages would run without having the data locally. Make your script inspect the output of both, because a command can finish successfully while still reporting work to do.

A GitHub Actions workflow

The treeverse/setup-dvc action installs DVC. As of December 2026 its README shows major version v3, with an optional version input.

YAML
# Read-only: .github/workflows/dvc-check.yml
name: DVC check
on:
  pull_request:

permissions:
  contents: read
  id-token: write      # only if you use short-lived cloud credentials

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0     # full history for comparisons
      - uses: treeverse/setup-dvc@v3
      # add your storage extra and your cloud provider's login step here
      - run: dvc data status --not-in-remote
      - run: dvc repro --dry --allow-missing
      - run: dvc metrics diff origin/main --md >> "$GITHUB_STEP_SUMMARY"

If you instead install DVC with pip install -r requirements.txt (which pins DVC and its storage extra), skip the setup action. Pick one method so you do not get two versions. Pin versions to keep builds stable, and check the action's README and your CI provider's docs for current versions. See CI/CD with GitHub Actions for the basics.

To actually run stages, the job must first download inputs with dvc pull (or dvc repro --pull).

Credentials in CI

  • Prefer short-lived credentials from your cloud provider (for example, OpenID Connect federation) over long-lived keys.
  • If you must use keys, store them as CI secrets and pass them through environment variables.
  • Give the CI identity read-only access when it only needs to read.
  • Never print secrets, and avoid verbose logs that may include sensitive settings.
  • Be careful with pull requests from forks: code from strangers must not run with access to your data or secrets. Do not weaken your CI provider's protections to make a job pass.

Training in CI

CI can run dvc repro for real, but ask first:

  • Is it fast and affordable enough for every pull request?
  • Is the result stable enough to compare?
  • Who may start expensive jobs?
  • Should the job push results? Writing to storage needs write access and a review step.

A common compromise is checks on every pull request and full training on a schedule or after manual approval. CML is a related open-source tool for posting reports and running ML jobs in CI; check its current maintenance status in its repository before depending on it.

Try the idea

This stand-in is a small CI gate: it reads simulated status results and metric changes and decides whether to pass. In a real job you would fill these values from DVC's JSON output (--json), whose exact format you should check.

Python Stand-in: a pull request gate

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The gate is not clever: it catches clear problems early and leaves judgment to reviewers.

Practical tips

  • Cache package installations, not secrets or sensitive data.
  • Set job timeouts so a stuck transfer does not run forever.
  • Download only what you need with targets, for example dvc pull data/test.csv.dvc.
  • Downloading large data from cloud storage may have transfer costs. Check your provider's price page.
  • Fail with a message that tells the author which command to run.

Common mistake: letting CI automatically commit and push new lock files or data to the main branch without review. Keep a human approval step before results are published or promoted.

Design the job output

A failing job should tell the author what to do. Print a short summary at the end, such as "Data missing from remote: run dvc push" or "Pipeline is stale: run dvc repro and commit dvc.lock". A long raw log forces people to guess, and then they learn to ignore the check.

Keep two jobs separate when you can. A fast job runs on every pull request: it checks configuration, pushed data and pipeline consistency, and runs unit tests for your code. A slow job runs training on a schedule or on request, with a larger machine and a clear timeout. If the slow job fails, it should not silently change anything in the repository.

Test the check itself

A check that never fails may not be checking anything. Once, on a test branch, deliberately commit a changed dependency without rerunning the pipeline, or skip dvc push for a small file. Confirm that the job fails with a helpful message. Then delete the branch. Do this again after you change the workflow file or upgrade DVC.

Remember that a green check proves only what you checked. It says nothing about whether the data is correct or the model is fair. Add data validation and model tests when those risks matter, and keep a person in the review loop.

Keep CI and laptops aligned

A check that passes locally but fails in CI often points to a version difference or a missing credential. Print the output of dvc doctor early in the job, after confirming it reveals nothing sensitive. Then compare versions with your local setup. Pin the same DVC and Python versions in both places, and upgrade them together on a branch.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which check helps catch a forgotten dvc push?

    It reports tracked data that the remote lacks.
  2. 2. What is a safer credential choice for CI?

    Short-lived, least-privilege credentials limit damage.
  3. 3. Why be careful with pull requests from forks?

    Anyone can submit from a fork, so protect secrets and data.
  4. 4. What does dvc repro --dry do in a CI job?

    A dry run helps check pipeline consistency.

Summary

  • Start CI checks with pushed data and pipeline consistency.
  • treeverse/setup-dvc installs DVC in GitHub Actions.
  • Use short-lived, least-privilege credentials, and protect against untrusted pull requests.
  • Show metric differences to reviewers.
  • Keep a human approval step before publishing results.

Next: Troubleshooting DVC

Seu progresso fica salvo neste navegador. Não precisa de conta.