Using renv with continuous integration

When building, deploying, or testing an renv-using project with continuous integration (CI) systems (e.g. GitHub Actions, GitLab CI, and others) you need some way to tell the CI system to use renv to restore the same packages that you’re using locally.

The general idea is:

  1. Call renv::snapshot() on your local machine to generate renv.lock.

  2. Call renv::restore() on your CI service to restore the project library from renv.lock.

  3. Cache the project library and global renv cache on the CI service.

Note that this workflow is not generally a good fit for CRAN packages, because CRAN itself runs R CMD check using the latest version of all dependencies.

GitHub actions

Here, we describe two common approaches for integrating renv with a GitHub Actions workflow:

Using r-lib/actions/setup-renv

The r-lib organization offers some actions for R users, and among them a setup-renv action is provided for projects using renv. To use this action, you can add the following steps to your workflow:

- uses: actions/checkout@v3
- uses: r-lib/actions/setup-r@v2
- uses: r-lib/actions/setup-renv@v2

Using these steps will automatically perform the following actions:

After this, any steps using R will use the active renv project by default.

Using the GitHub Actions Cache with renv

When using renv in your own custom GitHub action workflow, there are two main requirements:

  1. Cache any packages installed by renv across runs,
  2. Use renv::restore() to restore packages using this cache to speed up installation

As an example, these steps might look like:

  RENV_PATHS_ROOT: ~/.cache/R/renv


- name: Cache packages
  uses: actions/cache@v1
    path: ${{ env.RENV_PATHS_ROOT }}
    key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }}
    restore-keys: |
      ${{ runner.os }}-renv-

- name: Restore packages
  shell: Rscript {0}
  run: |
    if (!requireNamespace("renv", quietly = TRUE)) install.packages("renv")

See also the example on GitHub actions.

GitLab CI

The following template can be used as a base when using renv with GitLab CI:


  key: ${CI_JOB_NAME}

  - < ... other pre-deploy steps ... >
  - Rscript -e "if (!requireNamespace('renv', quietly = TRUE)) install.packages('renv')"
  - Rscript -e "renv::restore()"