From b9cd043c92b2b3381bd2df92b0e1e1c7a0334718 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Wed, 14 Feb 2024 12:26:19 -0500 Subject: [PATCH] Add `prune` param to `setup-r-dependencies` to prune extra R packages only when `cache="always"` --- setup-r-dependencies/README.md | 4 ++- setup-r-dependencies/action.yaml | 43 +++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/setup-r-dependencies/README.md b/setup-r-dependencies/README.md index 7d4a377ea..b09ca3b41 100644 --- a/setup-r-dependencies/README.md +++ b/setup-r-dependencies/README.md @@ -14,7 +14,7 @@ This action install dependencies for the current R environment based on the DESC Inputs available - `cache` - default `true`. Whether packages should be cached across runs or - not. + not. If `"always"` is provided, the package cache will be saved even if the Action fails. - `cache-version` - default `1`. If you need to invalidate the existing cache pass any other number and a new cache will be used. Ignored if `cache: false`. Note that you can also [delete caches @@ -40,6 +40,8 @@ Inputs available you need quoting. Defaults to `FALSE`. - `working-directory` - default `'.'`. If the DESCRIPTION file is not in the root directory of your repository. +- `prune` - default `true`. Used when `cache="always"`. Whether to prune the R + library of packages not found within the lock file used for installation. Basic: ```yaml diff --git a/setup-r-dependencies/action.yaml b/setup-r-dependencies/action.yaml index 0db13733c..3ad0b934e 100644 --- a/setup-r-dependencies/action.yaml +++ b/setup-r-dependencies/action.yaml @@ -3,7 +3,7 @@ description: 'Action to setup installation tools and install R dependencies' author: 'Jim Hester' inputs: cache: - description: 'A boolean value indicating whether packages should be cached (on success) from one to the other. If `"always"` is provided, the package cache will be saved even if the Action fails.' + description: 'Whether packages should be cached across runs or not. If `"always"` is provided, the package cache will be saved even if the Action fails.' required: true default: true cache-version: @@ -29,6 +29,9 @@ inputs: upgrade: description: 'Whether to install the latest available versions of the dependencies. Must be an R expression. See the README for details if you need quoting.' default: 'FALSE' + prune: + description: 'Whether to prune the R library of packages not found within the lock file used for installation.' + default: true runs: using: "composite" steps: @@ -145,13 +148,47 @@ runs: restore-keys: ${{ steps.cache-args.outputs.restore-keys }} - name: Install dependencies + id: install-pkgs run: | # Install/Update packages cat("::group::Install/update packages\n") Sys.setenv("PKGCACHE_HTTP_VERSION" = "2") library(pak, lib.loc = Sys.getenv("R_LIB_FOR_PAK")) - pak::lockfile_install(".github/pkg.lock") - ## Clean up lock file + pkgs <- pak::lockfile_install(".github/pkg.lock") + # Store the installed packages + packages <- paste0(pkgs$package, collapse = "|") + cat("packages=", packages, "\n", file = Sys.getenv("GITHUB_OUTPUT"), sep = "", append = TRUE) + cat("::endgroup::\n") + shell: Rscript {0} + working-directory: ${{ inputs.working-directory }} + + - name: Prune unused R packages + if: inputs.cache == 'always' && inputs.prune == 'true' + run: | + # Prune packages + cat("::group::Prune unnecessary packages\n") + installed_df <- as.data.frame(installed.packages(), stringsAsFactors = FALSE) + installed_packages <- installed_df$Package + core_pkgs <- installed_packages[!is.na(installed_df$Priority)] + pak_pkgs <- c( + "pak", + strsplit("${{ steps.install-pkgs.outputs.packages }}", "|", fixed = TRUE)[[1]] + ) + to_remove_pkgs <- setdiff(installed_packages, c(core_pkgs, pak_pkgs)) + if (length(to_remove_pkgs) > 0) { + cat("Removing unnecessary packages: ", paste0(to_remove_pkgs, collapse = ", "), "\n") + remove.packages(to_remove_pkgs) + } else { + cat("No unnecessary packages to remove\n") + } + cat("::endgroup::\n") + shell: Rscript {0} + working-directory: ${{ inputs.working-directory }} + + - name: Clean up lock file + run: | + # Clean up lock file + cat("::group::Clean up lock file\n") unlink(".github/pkg.lock") cat("::endgroup::\n") shell: Rscript {0}