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

  • Get IP address of Docker with Ansible
  • Automate Deployment of Docker
  • Docker and sensitive information used at run-time
  • Mount Host Directory to Wildfly deployment directory
  • Docker deployment options
  • Deploy a Docker image without using a repository
  • What features do Docker for Mac/Windows provide over Docker Toolbox on my development box?
  • Updating a config file through docker-compose
  • Postgresql drivers not found when dockerizing TeamCity
  • Is there any CI service that will pull from dockerhub?
  • Too many docker mounts
  • Getting error while runnig neo4j-mazerunner project
  • 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.