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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is in .env.ci please?