如何在AWS CodeDeploy上为Ruby on Rails编写appspec.yml [英] How to write appspec.yml for Ruby on Rails on AWS CodeDeploy

查看:88
本文介绍了如何在AWS CodeDeploy上为Ruby on Rails编写appspec.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Rails应用程序从Heroku迁移到AWS.我完成了AWS CodePipeline的工作,直到最后一刻,为CodeDeploy编写了appspec.yml.

I'm trying to migrate my Rails app from Heroku to AWS. I got my AWS CodePipeline working up until the last bit, writing the appspec.yml for CodeDeploy.

这需要我编写Ubuntu命令来停止我的应用程序,可能做一些迁移工作,然后再次启动该应用程序.

This requires me to write Ubuntu commands to stop my application, possibly do some migration stuff, and then start the application again.

这不是我的强项,所以我在这方面有些挣扎.我以为我将从执行"rails服务器"作为"AfterInstall"操作开始.但是后来我发现了一整箱新问题,即EC2实例上没有安装Rails和Bundler等.当我尝试一次只解决一个小型问题时,我想知道我是否应该实际编写此文件从头开始.

This is not my strong suit so I am struggling a bit with this part. I thought I'd start with executing "rails server" as "AfterInstall" action. But then I openend up a whole box of new problems, i.e. there is no Rails and Bundler installed on the EC2 instance etc.. While I was trying to just solve one mini problem at a time I wondered whether I should actually be writing this file from scratch.

所以这是我的问题:这与使用CodeDeploy部署的所有Rails应用程序是否有点相同?难道不是已经存在了吗,希望由比我了解他或她到底在做什么的人做的吗?我在Google上找不到很多东西,所以我希望有人可以为我提供一些启示.恐怕即使我能正常工作,它最终也不会是一个好,健壮且可投入生产的appspec.yml ...

So here's my question: isn't this somewhat identical for all Rails apps that are deployed with CodeDeploy? Shouldn't that be available already, hopefully done by someone with a lot more clue than me about what exactly he or she is doing? I didn't find much through Google, so I'm hoping somebody could shed some light onto this for me. I'm afraid even if I get it working it won't be a good and robust and production-ready appspec.yml in the end anyways...

已经非常感谢!

推荐答案

听起来您正在尝试直接在EC2实例上运行Rails服务器,我可能会将NGINX与Passenger一起使用,因为在部署后重新启动Rails应用程序是就像运行安装后脚本来触摸tmp/restart.txt一样简单,这将导致乘客重新启动应用程序.

It sounds like you are trying to run rails server directly on the EC2 instance, I would probably use NGINX with Passenger because then restarting your Rails app after deploy would be as simple as running an after install script to touch tmp/restart.txt which would cause Passenger to restart the app.

我仔细搜索了一下,发现下面的帖子也使用了NGINX和Passenger,它看起来非常像我将要采用的那种方法,并且我可以看到它们在安装脚本之前和之后也包括在内

I searched around a bit and found the following post where they are using NGINX and Passenger as well, it looks very much like the kind of approach that I would take and I can see that they include before and after install scripts as well.

http://sfviapgh. com/blog/2016/2/18/how-to-deploy-rails-with-aws-codedeploy

以防万一该职位后来不可用,这是关键点:

Just in case the post is later unavailable here is the key points:

  1. 使用生产服务器所需的一切设置EC2实例.在我们的例子中,这是Ruby,Passenger和Nginx.您不想提前通过git将应用程序克隆到服务器,但是需要知道您的应用程序在服务器上的驻留路径(例如www/var/...).确保您知道每个进程将使用哪些用户(克隆代码,重新启动进程).
  2. 将AWS CodeDeploy代理安装到服务器上.
  3. 将EC2实例移动到Production App Group AMI.
  4. 在我们的代码库中,我们在/script文件夹中添加了以下bash脚本.我们的完整脚本要复杂一些(从安全的s3存储桶中克隆环境变量),但这会使您踏入一个坚实的起点. CodeDeploy当前仅挂接到GitHub.幸运的是,GitHub是我们用来管理代码库的工具.
  5. 在引用这些脚本的应用程序的根目录中设置必需的AWS CodeDeploy appspec.yml.

application/appspec.yml

version: 0.0
os: linux
files:
  - source: /
    destination: <the directory your code will live>
permissions:
  - object: <the directory your code will live>
    owner: <user who will deploy your code>
    group: <group that user lives in>
  AfterInstall:
    - location: script/AfterInstall.sh
      runas: <user who will deploy your code>
  ApplicationStart:
    - location: script/ApplicationStart.sh
      runas: <root user>

应用程序/脚本/AfterInstall.sh

#!/bin/bash
cd /var/www/<app location>
RAILS_ENV=production bundle install --path vendor/bundle
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake assets:clobber
RAILS_ENV=production bundle exec rake assets:precompile

application/script/ApplicationStart.sh

#!/bin/sh
sudo service nginx restart

我希望对您的部署有所帮助.

I hope that helps you a bit further with the deployment.

这篇关于如何在AWS CodeDeploy上为Ruby on Rails编写appspec.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆