Docker, GitLab CI and Developing R Packages

Docker, GitLab CI and Developing R Packages

When I started developing R packages, I did not care much whether they would build with no errors, no warnings and no notes. I hardly knew about Build & Reload and Check in RStudio. I am not going to lie. I never finished reading Hadley Wickham's R Packages book (I am sorry Hadley)! Then, I learned about including unit tests and data, correctly referencing external packages and such. When my first build happened with no errors, no warnings and no notes, I felt good.

Next step was to make this even fancier using GitLab CI to check and build my packages automatically every time I push a new commit to the development/master branch. I set up a GitLab runner on my machine but failed to correctly create a .gitlab-ci.yml configuration file for the runner. Most blog posts suggested having a .yml file that looked like

image: rocker/rstudio  
    - R -e 'install.packages(c(<list_of_packages_you_need>))'
    - R CMD build . --no-build-vignettes --no-manual
    - PKG_FILE_NAME=$(ls -1t *.tar.gz | head -n 1)
    - R CMD check "${PKG_FILE_NAME}" --no-build-vignettes --no-manual
    - R -e 'devtools::test()' 

But there is an easier way of doing this. Much shorter and cleaner. devtools package provides an efficient way of building and testing packages. Your .gitlab-ci.yml can look as simple as this

  stage: test
  image: rocker/tidyverse
    - R -e 'install.packages(c(<list_of_packages_you_need>))'
    - R -e 'devtools::check()'
    - r-packages

Here I am using the rocker/tidyverse Docker image because it comes devtools installed. This is all you need!