Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Adds template for Gitlab CI #445

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions inst/templates/gitlab_ci/dot.gitlab-ci.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Use appropriate R version for runner
FROM rstudio/r-base:4.2.2-focal
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This template will only work if the gitlab-runner has a docker executor.

I was hesitant to provide a dockerfile template to create an image that will set-up the app system dependencies. However, after some tests, I realized that installing the dependencies took a lot of time.

For rhino-showcase, system dependencies took ~1.5mins to install, nodejs took ~1.5mins, but cypress took ~12.5mins. The fastest I managed to get the CI to complete was ~12mins with all dependencies installed in the pipeline.

I also could not figure out how to extract the R Version from renv.lock then use the appropriate R image in the pipeline, so I just decided to let the users specify their appropriate R image in this template.


# Install other app system dependencies here
RUN apt-get update && apt-get install --yes libcurl4-openssl-dev
Comment on lines +4 to +5
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users should update this based on the dependencies of their app.


# Install cypress dependencies
RUN apt-get update && apt-get install --yes \
libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 \
libxtst6 xauth xvfb \
&& rm -rf /var/lib/apt/lists/*

# Install nodejs
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash \
&& apt-get install nodejs -yq \
&& rm -rf /var/lib/apt/lists/*

# Build: docker build -f .gitlab-ci.Dockerfile -t your/image:latest .
# Push: docker push your/image:latest
Comment on lines +18 to +19
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are instructions on how to deploy their app environment images.

36 changes: 36 additions & 0 deletions inst/templates/gitlab_ci/dot.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
variables:
# For caching renv library
RENV_PATHS_CACHE: ${CI_PROJECT_DIR}/cache
RENV_PATHS_LIBRARY: ${CI_PROJECT_DIR}/renv/library
# Cache .rhino
DOT_RHINO_PATH: ${CI_PROJECT_DIR}/.rhino
# Necessary for cypress to run.
# https://docs.cypress.io/guides/references/error-messages#A-Cached-Cypress-Binary-Could-not-be-found
# https://docs.cypress.io/guides/continuous-integration/introduction#Caching
CYPRESS_CACHE_FOLDER: ${CI_PROJECT_DIR}/.cache/Cypress
Comment on lines +1 to +10
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cache paths help speed up the pipeline after the first run. Without this, the pipeline will take >20mins to complete each run.

Cypress requires caching the binary. Without this, cypress throws an error.



default:
# Use deployed image with .gitlab-ci.Dockerfile
image: your/image:latest
Comment on lines +14 to +15
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users should use the image they built and pushed using the .gitlab-ci.Dockerfile template.

cache:
# This key needs to be constant so all jobs in all branches will share the same cache
key: rhino-ci
paths:
- ${RENV_PATHS_CACHE}
- ${RENV_PATHS_LIBRARY}
- ${DOT_RHINO_PATH}
- ${CYPRESS_CACHE_FOLDER}
Comment on lines +16 to +23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just specifies all the paths that need to be cached. The key needs to be constant for all branches to have access to the cache. Users can change this depending on how they want to share caches.


Runs linters and tests:
before_script:
# Restores renv when cache is not available.
- R -e "options(renv.config.cache.symlinks = FALSE); renv::restore(clean = TRUE);"
script:
- R -e "rhino::lint_r()"
- R -e "rhino::lint_sass()"
- R -e "rhino::lint_js()"
- R -e "rhino::build_sass()"
- R -e "rhino::build_js()"
- R -e "rhino::test_r()"
- R -e "rhino::test_e2e()"
Comment on lines +25 to +36
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for a single job pipeline instead of a multi-job pipeline.

While a multi-job pipeline looks neater, the fastest I was able to achieve with it using the docker image was about ~10mins.

For a single job pipeline, the CI runs ~2-3 mins for rhino-showcase. The pipeline page is not as informative as the multi-job pipeline because it only shows one job. If the pipeline throws an error, it is not apparent which step failed, but reading the logs actually shows which step failed or encountered an error, so this might not be that much of a downside.

I also performed tests to ensure that the pipeline catches errors in lint r, lint sass, lint js, build sass, and r unit tests. I could not test build js because I can't find a way to make the lint js pass and build js fail. For cypress, I encountered fails when I was doing tests with cypress cache.