-
-
Save guiferrpereira/b825f3004036473ee02edb677822f5d3 to your computer and use it in GitHub Desktop.
Example Gitlab CI Config for a Rails + Nginx application using Docker Compose
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
# See how variables work, and a list of predefined ones: | |
# - https://docs.gitlab.com/ce/ci/variables/ | |
variables: | |
RAILS_IMAGE: registry.gitlab.com/bryanbraun/gridmaster.io/railsapp:$CI_COMMIT_SHA | |
NGINX_IMAGE: registry.gitlab.com/bryanbraun/gridmaster.io/nginx:$CI_COMMIT_SHA | |
DEPLOY_TAG: $CI_COMMIT_SHA | |
cache: | |
paths: | |
- vendor/ruby | |
# See the following multi-stage set up examples: | |
# https://docs.gitlab.com/ce/ci/docker/using_docker_build.html#using-the-gitlab-container-registry | |
# https://gist.github.com/jorge07/63b993e8f934012a5abb6b55cc702c75 | |
stages: | |
- test | |
- build | |
- deploy | |
test_job: | |
stage: test | |
image: ruby:2.3.4 | |
services: | |
- postgres:9.6.1 | |
artifacts: | |
paths: | |
- public/ | |
expire_in: 12 hrs | |
variables: | |
RAILS_ENV: test | |
POSTGRES_DB: gridmaster_test | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: '' | |
before_script: | |
# we install gems and precompile assets now so both the NGINX & RAILS images have a copy | |
# of the static assets for baked into their images. For why, see "Compiling Assets" at: | |
# http://chrisstump.online/2016/03/17/continuous-deployment-docker-rails/ | |
- bundle install --jobs $(nproc) --path vendor | |
- rails assets:precompile | |
script: | |
- ls -al public/assets # for debugging | |
- rails db:test:prepare | |
- rails test | |
# We a "docker-in-docker" image, to run docker on CI like we do in dev. See: | |
# - https://docs.gitlab.com/ce/ci/docker/using_docker_build.html#use-docker-in-docker-executor | |
# - http://stackoverflow.com/a/40387216/1154642 | |
build_job: | |
stage: build | |
image: docker:latest | |
services: | |
- docker:dind | |
before_script: | |
- touch .env # prevents an error where docker-compose tries to move a non-existant file. | |
variables: | |
# We use the overlay driver for improved performance. | |
# See: https://docs.gitlab.com/ce/ci/docker/using_docker_build.html#using-the-overlayfs-driver | |
DOCKER_DRIVER: overlay | |
RAILS_ENV: production | |
script: | |
- ls -al public/assets # for debugging | |
- docker build -t $RAILS_IMAGE . | |
- docker build -f config/containers/Dockerfile-nginx -t $NGINX_IMAGE . | |
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN https://registry.gitlab.com | |
- docker push $NGINX_IMAGE | |
- docker push $RAILS_IMAGE | |
# I'm not pushing postgres images because I simply use the official Postgres image. | |
# The other images contain precompiled assets and should be built every time. | |
only: | |
- master | |
# This job triggers a rake task containing a deploy script. For ideas on deploys scripts, see: | |
# - http://chrisstump.online/2016/03/17/continuous-deployment-docker-rails/ | |
# Gitlab ones: | |
# - https://www.stavros.io/posts/how-deploy-django-docker | |
deploy_job: | |
stage: deploy | |
image: ruby:2.3.4 | |
variables: | |
RAILS_ENV: test | |
environment: | |
name: production | |
url: https://gridmaster.io | |
before_script: | |
# We'll access our production server via SSH keys. See | |
# https://docs.gitlab.com/ee/ci/ssh_keys/README.html#using-ssh-keys for details. | |
# install ssh-agent | |
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' | |
# run ssh-agent | |
- eval $(ssh-agent -s) | |
# add ssh key stored in SSH_PRIVATE_KEY variable to the agent store | |
- ssh-add <(echo "$SSH_PRIVATE_KEY") | |
script: | |
- bundle install --jobs $(nproc) --path vendor | |
- bundle exec rails docker:deploy | |
when: manual | |
only: | |
- master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment