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.

  - test
  - build
  - deploy

# ...

  stage: build
  image: docker:latest
    - docker:dind
    DOCKER_DRIVER: overlay
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN
    - docker build -t$CI_BUILD_REF_NAME .
    - docker push$CI_BUILD_REF_NAME
    - 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

  • Ansible task timeout max length
  • Deploying with docker push is slow because there are many images
  • How to use Docker in the development/deployment workflow?
  • Ansible, how to add user to group only if user exists
  • docker daemon start using ansible
  • Docker build/push every time - is it practical for continuous deployment?
  • Docker upgrade issue cent os 6.5
  • Docker container drive does not match available hard drive space on host
  • How to set the command history in a Dockerfile
  • Why I can't see my files inside a docker container?
  • File mounted as docker volume becomes corrupt when saved outside docker
  • Get composer (php dependency manager) to run on a docker image build
  • 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.