Photo by Clément H on Unsplash

Building and Testing Rails With GitLab CI

Taylor Scott

--

If you haven’t already, check out Basic Docker Image for Rails since we will building our project with Docker.

Setup GitLab CI

GitLab has very good documentation which you can reference when going through this section.

Within your application directory:

touch .gitlab-ci.yml

Then open up the file and copy/pasta the following

image: ruby:2.6.3-stretchstages:
- build
- test
variables:
BUILD_IMAGE: ${CI_REGISTRY_IMAGE}/${CI_COMMIT_REF_SLUG}:${CI_COMMIT_SHA}
build-container:
stage: build
image: docker:latest
services:
- docker:dind
script:
- echo "Building Docker Image $BUILD_IMAGE"
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com
- docker build -t $BUILD_IMAGE .
- docker push $BUILD_IMAGE
rspec:
stage: test
image: ${BUILD_IMAGE}
services:
- postgres:latest
variables:
POSTGRES_HOST: postgres
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres
RAILS_ENV: test
before_script:
- gem install bundler
- bundle install
script:
- bundle exec rspec --color --tty

rubocop:
stage: test
image: ${BUILD_IMAGE}
before_script:
- gem install bundler
- bundle install
script:
- bundle exec rubocop

This setup is based on a basic rails application with rspec and rubocop setup.

There are 3 jobs we’ve specified. Thebuild-container job will build our docker image and push it to the gitlab registry. The rspec job runs our tests and rubocop lints our code.

Altogether the CI will look like this:

The pipeline created for GitLab CI

NEXT UP: Deploying to Elastic Beanstalk

--

--