Continuous integration with R + testthat + gitlab + docker

Continuous integration with R + testthat + gitlab + docker

Why?

I want to make sure that when I make a change to some complicated code nothing breaks. Moreover, I want to make sure that nothing breaks in a clean install.

Getting gitlab up and running

If you are reading this you probably know that I like gitlab better than bitbucket, github, and that awfaul thing that you are probably using. You can skip this and the next section and just use the hosted version of gitlab which gives you 2000 minutes per month to do this stuff. Or, you have have some fun with docker and run your own instance with the following docker-compose.yaml


web:
  image: 'gitlab/gitlab-ce:latest'
  restart: always
  hostname: 'git.ignacio.website'
  environment:
    GITLAB_OMNIBUS_CONFIG: |
      external_url 'https://git.ignacio.website'
      gitlab_rails['smtp_enable'] = true
      gitlab_rails['smtp_address'] = "smtp.mailgun.org"
      gitlab_rails['smtp_port'] = 587
      gitlab_rails['smtp_authentication'] = "plain"
      gitlab_rails['smtp_enable_starttls_auto'] = true
      gitlab_rails['smtp_user_name'] = "postmaster@email.ignacio.website"
      gitlab_rails['smtp_password'] = "MY_SUPER_SECRET_PASSWORD"
      gitlab_rails['smtp_domain'] = "email.ignacio.website"
      gitlab_rails['gitlab_shell_ssh_port'] = 2224
  ports:
    - '80:80'
    - '443:443'
    - '2224:22'
  volumes:
    - '/nfs/config/gitlab:/etc/gitlab'
    - '/nfs/config/gitlab/logs:/var/log/gitlab'
    - '/nfs/config/gitlab/data:/var/opt/gitlab'

Then just run:

docker-compose pull
docker-compose up -d

To update your gitlab instance you just re run those commands.

Getting Getting a runner up an running

Now you can go to your gitlab instance to find your runner registration token at Admin Area > Overview > Runners.

Once you have that, just run:

docker pull gitlab/gitlab-runner

docker run -d --name imgitlabrunner -v /var/run/docker.sock:/var/run/docker.sock -v /srv/imgitlabrunner/config:/etc/gitlab-runner --rm gitlab/gitlab-runner

docker exec -it imgitlabrunner gitlab-runner register -n --url https://git.ignacio.website/ --registration-token MY_SUPER_SECRET_TOKEN --clone-url https://git.ignacio.website/ --executor docker --docker-image "docker:latest" --docker-privileged

A very simple R package with testthat

Now you only need add the following .gitlab-ci.yml to you package:

image: rocker/r-base
test:
   script:
    - apt-get update
    - apt-get install --yes --no-install-recommends r-cran-testthat r-cran-devtools
    - R -e "devtools::install_deps()"
    - 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

This is telling gitlab to: 1. start with the rocker/r-base container, 2. install testthat and devtools, 3. install all the dependencies that you package needs 4. build your package 5. run the tests that you programmed

You can find my toy example here.

Ignacio Martinez avatar
About Ignacio Martinez
research economist, tech enthusiast
comments powered by Disqus