applejuiceteaching
applejuiceteaching

Reputation: 1493

Always run rake task when uploading to production

I am familiar with Rails but this is my first time uploading to production. I am able to successfully upload my app to AWS and deploy it. However, every time I do that, I have to ssh into my server and run the necessary rake tasks to clean up my models and fully prep my website. Is there a file like production.rb where you can write a script to be run on every production upload. For instance run all tests and rake tests ? Is there a simple example of a script someone. This is the example of my rake file.

Note: I am using AWS Beanstalk, super easy to deploy, just want to run some post production ready scripts.

This is the rake file I want to run commands of post deployment.

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

Upvotes: 0

Views: 941

Answers (1)

karthik v
karthik v

Reputation: 1172

For post deployment, you can try the following way.

Create a file in .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

What this config does is:

  • create the “post” directory if it doesn’t already exist (it won’t by default) – ignore any errors (such as if the directory already
    existed)

  • deploy the shell script with the appropriate permissions into the right directory

For more details look at the following references: Blog-Article & Stackoverflow-Question

Upvotes: 1

Related Questions