CI: discard the cached R library when the image's R changes

The cache holds rlib/, which holds compiled code. A newer R in the image
does not rebuild that code but refuses to load it, so the job died in its
before_script with

  rlib/vctrs/libs/vctrs.so: undefined symbol: SETLENGTH

and never got to the tests. Since a cache key cannot be derived from the
R version of an image that is only pulled later, the version is recorded
in the library and the library discarded when it no longer matches.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Change-Id: I09fc147c114533987c82dd558c77ac36f00904a0
diff --git a/ci/install-rkorapclient-ci.yml b/ci/install-rkorapclient-ci.yml
index 9ef0b82..cf08683 100644
--- a/ci/install-rkorapclient-ci.yml
+++ b/ci/install-rkorapclient-ci.yml
@@ -27,6 +27,13 @@
 
     - start_section install_linux_packages "Installing missing Linux packages"
     - mkdir -pv $APT_CACHE_DIR ccache $R_LIBS_USER /usr/local/sbin ~/.ccache ~/.R
+    # The cached library holds compiled code, which a newer R refuses to load -
+    # with "undefined symbol", not by rebuilding it. As a cache key cannot be
+    # derived from the image's R version, the library is discarded by hand when
+    # that version changes.
+    - R_VERSION=$(R -s -e 'cat(R.version$major, R.version$minor, sep = ".")')
+    - if [ "$(cat $R_LIBS_USER/.r-version 2>/dev/null)" != "$R_VERSION" ]; then echo "R $R_VERSION differs from the one the cached library was built for - rebuilding it"; rm -rf $R_LIBS_USER; mkdir -p $R_LIBS_USER; fi
+    - echo "$R_VERSION" > $R_LIBS_USER/.r-version
     - cp ci/Makevars ~/.R/Makevars
     - cp ci/ccache.conf ${CI_PROJECT_DIR}/ccache/ccache.conf
     - cp ci/ccache.conf ~/.ccache/ccache.conf