Skip to content

Instantly share code, notes, and snippets.

@lucaspiller
Last active Apr 24, 2021
Embed
What would you like to do?
Rails 5.2 + Docker + Gitlab Continuous Deployment
image: ruby:2.6
services:
- postgres:11-alpine
variables:
POSTGRES_DB: myorg_test
CONTAINER_IMAGE: registry.gitlab.com/myorg/myapp
stages:
- test
- build
cache:
paths:
- .bundle
- vendor/ruby
test:
stage: test
script:
- apt-get update -qy
- apt-get install -y nodejs
- bundle install --path .bundle
- cp .env.ci .env
- cp config/database.ci.yml config/database.yml
- bundle exec rake db:create db:schema:load RAILS_ENV=test || true
- bundle exec rake db:migrate RAILS_ENV=test
- bundle exec rspec
- bundle exec cucumber
build:
stage: build
image: docker:stable
services:
- docker:dind
before_script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
script:
- docker pull $CONTAINER_IMAGE:master || true
- docker build --cache-from $CONTAINER_IMAGE:master --tag $CONTAINER_IMAGE:$CI_COMMIT_REF_NAME .
- docker push $CONTAINER_IMAGE:$CI_COMMIT_REF_NAME
#!/bin/sh
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec puma -C config/puma.rb
FROM ruby:2.6-alpine AS base
RUN apk add --no-cache --update build-base \
git \
postgresql-dev \
nodejs \
tzdata
RUN mkdir /app
WORKDIR /app
# Add the Gemfile and bundle first, so changes to the app don't invalidate the
# cache
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install --without development test --deployment
#
# Development stage
#
FROM base AS development
ENV RAILS_ENV development
RUN bundle install
ADD . /app
CMD bundle exec rails s
#
# Production stage
#
FROM base AS production
# Add the rest of the app
ADD . /app
ENV RAILS_ENV production
ENV NODE_ENV production
# Build assets - n.b. the DATABASE_URL doesn't need to be valid, it's just to
# stop Rails complaining and saying that it isn't set
RUN bundle exec rake assets:precompile DATABASE_URL=postgresql://localhost/
EXPOSE 9292
CMD bin/prod-start
@lucaspiller

This comment has been minimized.

Copy link
Owner Author

@lucaspiller lucaspiller commented Jan 21, 2019

These are the scripts I use for continuous deployment of WatchSumo. I host the repository on GitLab where I take advantage of their CI and container registry (wink wink Microsoft). On the servers (provisioned with Terraform and Ansible) I am just running a basic Docker setup (no Docker Compose or similar orchestration tools). For CD I use Watchtower which monitors the registry, and automatically pulls and recreates the app container when a change is pushed. The FROM ... AS syntax in the Dockerfile is for multi-stage builds, so I can use the same image to develop locally in Docker Compose.

@adrienpoly

This comment has been minimized.

Copy link

@adrienpoly adrienpoly commented Aug 9, 2019

Hello
what does your database.ci.yml looks like?
Thanks

@lucaspiller

This comment has been minimized.

Copy link
Owner Author

@lucaspiller lucaspiller commented Aug 20, 2019

@adrienpoly Nothing unusual:

test:
  min_messages: WARNING
  adapter: postgresql
  encoding: unicode
  database: watchsumo_test
  host: postgres
  username: postgres
  pool: 5

You could do the same with the DATABASE_URL environment variable.

@adrienpoly

This comment has been minimized.

Copy link

@adrienpoly adrienpoly commented Aug 20, 2019

thanks

@mikesutton

This comment has been minimized.

Copy link

@mikesutton mikesutton commented Aug 20, 2019

Thanks Lucas - curious though, does this take the hit of asset precompile on each production build?

@JetsonDavis

This comment has been minimized.

Copy link

@JetsonDavis JetsonDavis commented Apr 24, 2021

What is in .env.ci please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment