0

我熟悉Rails,但这是我第一次上传到生产环境。我能够成功将我的应用上传到AWS并进行部署。但是,每次我这样做,我都必须进入我的服务器并运行必要的rake任务来清理我的模型并完全准备好我的网站。是否有像production.rb这样的文件,您可以在其中编写脚本以在每个生产上传中运行。例如运行所有测试和耙测试?有一个简单的脚本例子吗?这是我的rake文件的例子。上传到生产时始终运行rake任务

注意:我正在使用AWS Beanstalk,超级简单部署,只是想运行一些后期制作就绪脚本。

这是我想运行部署后命令的rake文件。

require "#{Rails.root}/app/helpers/application_helper" 
include ApplicationHelper 

namespace :db do 
    desc "Generate a new blog post markdown" 
    task new_article: :environment do 
    cp 'lib/assets/articles/template.md', "lib/assets/articles/NEW_ARTICLE#{Time.now.strftime("%s")}.md" 
    puts 'new article created!' 
    end 

    task populate: :environment do 
    Article.destroy_all 

    if User.count == 0 
     User.create!(name: "AJ", email: "[email protected]") 
    end 

    puts Dir.pwd 
    a = File.join("lib", "assets", "articles", "*.md") 
    Dir.glob(a).reject { |name| /.*(template|NEW_ARTICLE).*/ =~ name }.each do |file| 
     File.open(file, "r") do |f| 
     contents = f.read 
     mkdown = Metadown.render(contents) 
     md = mkdown.metadata 

     unrendered_content = contents.sub(/^---(\n|.)*---/, '') 
     #puts unrendered_content 


     article = Article.create!(title: md["title"], 
         content: markdown(unrendered_content), 
         header_image: md["header_image"], 
         published: md["published"], 
         useful_links: md["useful_links"], 
         people_mentioned: md["people_mentioned"], 
         written_at_date: md["written_at_date"], 
         timestamp: md["timestamp"], 
         embedded_link: md["embedded_link"], 
         user: User.first) 


     article.add_tag(md["tags"]) 
     puts article.useful_links 
     puts article.people_mentioned 
     puts article.header_image 
     puts article.tags 

     end 
    end 

    puts "Article Count: #{Article.count}" 
    end 



end 

回答

1

对于部署后,您可以尝试以下方法。

.ebextensions创建文件/ 01_build.config

commands: 
    create_post_dir:   
     command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post" 
     ignoreErrors: true 
files: 
    "/opt/elasticbeanstalk/hooks/appdeploy/post/99_build_app.sh": 
    mode: "000755" 
    owner: root 
    group: root 
    content: | 
     #!/usr/bin/env bash 
     cd /var/app/current/app/ 
     Your-Post-Deploy-Command1 
     Your-Post-Deploy-Command2 
     Your-Post-Deploy-Command3 

什么这个配置的作用是:

  • 打造的“邮报”的目录,如果它不存在(它不会通过 默认) - 忽略任何错误(例如,如果目录已存在

  • 部署具有相应权限的shell脚本到正确的目录

欲了解更多详细信息,看看下面的参考资料:Blog-Article & Stackoverflow-Question