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

  • How packaging and deployment works in Azure ?
  • How can I provision a Docker image using Ansible without installing locally?
  • Deploying a minimal flask app in docker - server connection issues
  • Deploying Elixir App to Google Cloud via Docker
  • Ansible error: KeyError: 'ApiVersion'
  • SaltStack Caches installing package files and reproduce installation later
  • Extra package installation in boot2docker
  • Docker port uncertainty
  • Issues in using git forked package in Docker-compose
  • Docker container link not in env or /etc/hosts
  • Bootstraping Percona Xtradb Cluster in Docker gives the error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
  • Understanding docker - how do containers contain all required dependencies?
  • 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.