Last active
April 14, 2022 09:07
-
-
Save lucaspiller/d5bf6ea1c06b4dbaad98f97372319324 to your computer and use it in GitHub Desktop.
Rails 5.2 + Docker + Gitlab Continuous Deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
bundle exec rake db:create | |
bundle exec rake db:migrate | |
bundle exec puma -C config/puma.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Hello
what does your database.ci.yml
looks like?
Thanks
@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.
thanks
Thanks Lucas - curious though, does this take the hit of asset precompile on each production build?
What is in .env.ci please?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 theDockerfile
is for multi-stage builds, so I can use the same image to develop locally in Docker Compose.