How to deploy a Rails app with GitLab CI + Docker + Ansible?

I have a hosted GitLab with some Ruby on Rails project on it. I have a CI script, that builds a Docker image with the project and push it to the GitLab registry. Finally, I need to deploy that image to the staging server.

stages:
  - test
  - build
  - deploy

# ...

build_image:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN gitlab.host.com:4567
    - docker build -t gitlab.host.com:4567/group/app:$CI_BUILD_REF_NAME .
    - docker push gitlab.host.com:4567/group/app:$CI_BUILD_REF_NAME
  only:
    - master

I was thinking to do it with Ansible, but can’t figure how to do it. Maybe somebody can recommend what to look for or what to read? Thanx

  • Escaping double curly braces in Ansible
  • How to implement the “One Binary” principle with Docker
  • Why ansible keeps recreating docker containers with state “started”
  • Ansible ignores remote_user switch
  • Docker application deployment
  • Setting up Nginx Proxy in Docker using Ansible
  • Cancelling ulimit bounds
  • New device node created on host does not get reflected in Docker container when using --device flag
  • Docker cannot write to assets folder as web server process in Yii/PHP application locally OS X
  • pecl install mongo command for mongo-1.5.5.tgz produce invalid tgz error
  • `npm start` in docker ends with: Please install a supported C++11 compiler and reinstall the module
  • How to run a jar file using docker image?
  • One Solution collect form web for “How to deploy a Rails app with GitLab CI + Docker + Ansible?”

    I’d recommend to not use Ansible in the CI. That usually complicates things since the container running your CI would need to have Ansible installed for this to work. This is doable however and I have a setup like this. Another downside is that Ansible ‘eats’ the output and if there is an error, prints it in an unformatted way.
    If your intention was not to use Ansible in the CI and to just deploy it yourself with Ansible I have to advise against this as well. This will cause everyone who wants an update on the staging server to contact you, or they would need access to both the staging server and the ansible playbook. They will also ask themselves when the last update took place.

    Better to put your code in a different step that does an ssh into the machine, pulls the image, stops the previous and launches the new. You can even define a ‘staging’ environment in Gitlab and it will inform you when the last update was done. This can be handy for the other developers.
    You can put your private ssh key in the Gitlab secret variable and put it in ~/.ssh/id_rsa as a first deploy step. This will then give you access to your staging machine. You should call his user ‘gitlab-staging’ or something similar so you can track the access logs on the host back to this config.

    Hope this helps, if you have any questions let me know.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.