Skip to content

Instantly share code, notes, and snippets.

@bryanbraun
Created July 6, 2017 15:15
Show Gist options
  • Save bryanbraun/6aeaffff70c41b74480695ac103db432 to your computer and use it in GitHub Desktop.
Save bryanbraun/6aeaffff70c41b74480695ac103db432 to your computer and use it in GitHub Desktop.
Example Gitlab CI Config for a Rails + Nginx application using Docker Compose
# 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
@mecampbellsoup
Copy link

This doesn't look like you're using docker-compose at all, just docker... am I missing something?

@bryanbraun
Copy link
Author

Yeah, good question. I use docker-compose for local development, but the test_job doesn't use the docker-compose command or my docker-compose.yml file. It just defines the docker images for running the app in config, as shown here:

image: ruby:2.3.4
  services:
    - postgres:9.6.1

I don't remember if I tried to get docker-compose running and ended up doing this instead because of issues. I think that could be likely.

My other jobs build_job and deploy_job don't attempt to run the application... they just build the images and deploy them.

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