简体   繁体   English

如何使用GitLab CI + Docker + Ansible部署Rails应用程序?

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

I have a hosted GitLab with some Ruby on Rails project on it. 我有一个托管的GitLab,上面有一些Ruby on Rails项目。 I have a CI script, that builds a Docker image with the project and push it to the GitLab registry. 我有一个CI脚本,可使用该项目构建Docker映像并将其推送到GitLab注册表。 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. 我当时想用Ansible做到这一点,但不知道该怎么做。 Maybe somebody can recommend what to look for or what to read? 也许有人可以推荐寻找或阅读的内容? Thanx 谢谢

I'd recommend to not use Ansible in the CI. 我建议不要在CI中使用Ansible。 That usually complicates things since the container running your CI would need to have Ansible installed for this to work. 这通常会使事情复杂化,因为运行CI的容器需要安装Ansible才能正常工作。 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. 另一个缺点是Ansible会“吃掉”输出,如果有错误,则以无格式打印。
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. 如果您的意图不是在CI中使用Ansible,而是仅通过Ansible自己进行部署,我也建议您不要这样做。 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. 这将使每个想要在登台服务器上进行更新的人都与您联系,或者他们需要访问登台服务器和ansible剧本。 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. 最好将您的代码放在不同的步骤中,从而在计算机中执行ssh,拉取映像,停止上一个并启动新的代码。 You can even define a 'staging' environment in Gitlab and it will inform you when the last update was done. 您甚至可以在Gitlab中定义一个“暂存”环境,它将在最后一次更新完成时通知您。 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. 您可以将私有ssh密钥放入Gitlab秘密变量中,并将其放入〜/ .ssh / id_rsa作为第一步部署的第一步。 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. 您应该称呼他的用户“ gitlab-staging”或类似名称,以便您可以将主机上的访问日志追溯到此配置。

Hope this helps, if you have any questions let me know. 希望对您有所帮助,如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM