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