| 1 |
--- |
| 2 |
title: "Using renv with continuous integration" |
| 3 |
output: rmarkdown::html_vignette |
| 4 |
vignette: > |
| 5 |
%\VignetteIndexEntry{Using renv with continuous integration} |
| 6 |
%\VignetteEngine{knitr::rmarkdown} |
| 7 |
%\VignetteEncoding{UTF-8} |
| 8 |
--- |
| 9 |
|
| 10 |
```{r, include=FALSE} |
| 11 |
knitr::opts_chunk$set( |
| 12 |
collapse = TRUE, |
| 13 |
comment = "#>", |
| 14 |
eval = FALSE |
| 15 |
) |
| 16 |
``` |
| 17 |
|
| 18 |
When building, deploying, or testing an renv-using project with continuous |
| 19 |
integration (CI) systems (e.g. [GitHub Actions][github-actions], |
| 20 |
[GitLab CI][gitlab-ci], and others) you need some way to tell the CI system |
| 21 |
to use renv to restore the same packages that you're using locally. |
| 22 |
|
| 23 |
The general idea is: |
| 24 |
|
| 25 |
1. Call `renv::snapshot()` on your local machine to generate `renv.lock`. |
| 26 |
|
| 27 |
2. Call `renv::restore()` on your CI service to restore the project library |
| 28 |
from `renv.lock`. |
| 29 |
|
| 30 |
3. Cache the project library and global renv cache on the CI service. |
| 31 |
|
| 32 |
Note that this workflow is not generally a good fit for CRAN packages, because |
| 33 |
CRAN itself runs `R CMD check` using the latest version of all dependencies. |
| 34 |
|
| 35 |
## GitHub actions |
| 36 |
|
| 37 |
Here, we describe two common approaches for integrating renv with a [GitHub Actions](https://github.com/features/actions) workflow: |
| 38 |
|
| 39 |
* Use the `r-lib/setup-renv` action. |
| 40 |
* Use GitHub's built-in cache action together with existing renv functionality; |
| 41 |
|
| 42 |
### Using r-lib/actions/setup-renv |
| 43 |
|
| 44 |
The r-lib organization offers some actions for R users, and among them a [`setup-renv`][r-lib-actions-renv] action is provided for projects using renv. To use this action, you can add the following steps to your workflow: |
| 45 |
|
| 46 |
```yaml |
| 47 |
steps: |
| 48 |
- uses: actions/checkout@v5 |
| 49 |
- uses: r-lib/actions/setup-r@v2 |
| 50 |
- uses: r-lib/actions/setup-renv@v2 |
| 51 |
``` |
| 52 |
|
| 53 |
Using these steps will automatically perform the following actions: |
| 54 |
|
| 55 |
* renv will be installed, via `install.packages("renv")`, |
| 56 |
* renv will be configured to use the GitHub cache, |
| 57 |
* If provided via a `with: profile:` key, that renv profile will be activated, |
| 58 |
* The project will be restored via `renv::restore()`. |
| 59 |
|
| 60 |
After this, any steps using R will use the active renv project by default. |
| 61 |
|
| 62 |
### Using the GitHub Actions Cache with renv |
| 63 |
|
| 64 |
When using renv in your own custom GitHub action workflow, there are two main requirements: |
| 65 |
|
| 66 |
1. Cache any packages installed by renv across runs, |
| 67 |
2. Use `renv::restore()` to restore packages using this cache to speed up installation |
| 68 |
|
| 69 |
As an example, these steps might look like: |
| 70 |
|
| 71 |
```yaml |
| 72 |
env: |
| 73 |
RENV_PATHS_ROOT: ~/.cache/R/renv |
| 74 |
|
| 75 |
steps: |
| 76 |
|
| 77 |
- name: Cache packages |
| 78 |
uses: actions/cache@v4 |
| 79 |
with: |
| 80 |
path: ${{ env.RENV_PATHS_ROOT }} |
| 81 |
key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }} |
| 82 |
restore-keys: | |
| 83 |
${{ runner.os }}-renv- |
| 84 |
|
| 85 |
- name: Restore packages |
| 86 |
shell: Rscript {0} |
| 87 |
run: | |
| 88 |
if (!requireNamespace("renv", quietly = TRUE)) install.packages("renv") |
| 89 |
renv::restore() |
| 90 |
``` |
| 91 |
|
| 92 |
See also the [example][github-actions-renv] on GitHub actions. |
| 93 |
|
| 94 |
## Generating a lockfile in CI |
| 95 |
|
| 96 |
In some CI workflows, you may want to generate or update a lockfile without |
| 97 |
installing packages -- for example, to validate that dependencies can be |
| 98 |
resolved, or to produce a lockfile artifact in a lightweight container that |
| 99 |
doesn't have compilation tools. |
| 100 |
|
| 101 |
`renv::plan()` resolves the packages required by a project and writes a |
| 102 |
lockfile, without installing anything: |
| 103 |
|
| 104 |
```r |
| 105 |
# resolve all project dependencies and write renv.lock |
| 106 |
renv::plan() |
| 107 |
|
| 108 |
# or, resolve a specific set of packages |
| 109 |
renv::plan(c("dplyr", "ggplot2")) |
| 110 |
``` |
| 111 |
|
| 112 |
Because `renv::plan()` works from repository metadata rather than installed |
| 113 |
packages, it can run in environments where package installation would be |
| 114 |
impractical. |
| 115 |
|
| 116 |
## Downloading packages in CI |
| 117 |
|
| 118 |
In multi-stage CI pipelines, it can be useful to download packages in one |
| 119 |
step and install them in a later step. `renv::retrieve()` downloads the |
| 120 |
packages required by a project (or a lockfile) without installing them: |
| 121 |
|
| 122 |
```r |
| 123 |
# download all packages required by the lockfile |
| 124 |
renv::retrieve() |
| 125 |
|
| 126 |
# download specific packages |
| 127 |
renv::retrieve(c("dplyr", "ggplot2")) |
| 128 |
``` |
| 129 |
|
| 130 |
Downloaded packages are stored in renv's local cache, so a subsequent |
| 131 |
`renv::restore()` can install from the cache without re-downloading. |
| 132 |
|
| 133 |
## GitLab CI |
| 134 |
|
| 135 |
The following template can be used as a base when using renv with |
| 136 |
[GitLab CI][gitlab-ci]: |
| 137 |
|
| 138 |
```yaml |
| 139 |
variables: |
| 140 |
RENV_PATHS_ROOT: ${CI_PROJECT_DIR}/renv |
| 141 |
|
| 142 |
cache: |
| 143 |
key: ${CI_PROJECT_NAME} |
| 144 |
paths: |
| 145 |
- ${RENV_PATHS_ROOT} |
| 146 |
|
| 147 |
before_script: |
| 148 |
- < ... other pre-deploy steps ... > |
| 149 |
- Rscript -e "if (!requireNamespace('renv', quietly = TRUE)) install.packages('renv')" |
| 150 |
- Rscript -e "renv::restore()" |
| 151 |
``` |
| 152 |
|
| 153 |
|
| 154 |
[gitlab-ci]: https://about.gitlab.com/solutions/continuous-integration/ |
| 155 |
[github-actions]: https://github.com/features/actions |
| 156 |
[github-actions-renv]: https://github.com/actions/cache/blob/main/examples.md#r---renv |
| 157 |
[r-lib-actions-renv]: https://github.com/r-lib/actions/tree/v2-branch/setup-renv |