Jack Owels
Jack Owels

Reputation: 169

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

Upvotes: 0

Views: 1141

Answers (1)

Evert
Evert

Reputation: 308

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.

Upvotes: 2

Related Questions