【问题标题】:Always run rake task when uploading to production上传到生产环境时始终运行 rake 任务
【发布时间】:2017-12-20 17:15:47
【问题描述】:

我熟悉 Rails,但这是我第一次上传到生产环境。我能够成功地将我的应用程序上传到 AWS 并进行部署。但是,每次我这样做时,我都必须 ssh 进入我的服务器并运行必要的 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: "aj@psychowarfare.com")
    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

【问题讨论】:

    标签: ruby-on-rails ruby amazon-web-services amazon-elastic-beanstalk production-environment


    【解决方案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
    

    这个配置的作用是:

    • 如果“post”目录不存在,则创建它(它不会通过 默认)——忽略任何错误(例如,如果目录已经
      存在)

    • 将具有适当权限的 shell 脚本部署到正确的目录中

    更多详情请看以下参考资料:Blog-Article & Stackoverflow-Question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 2012-12-26
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 2016-02-13
      • 2019-10-28
      相关资源
      最近更新 更多