Run Rake tasks on a server with Capistrano and Rake

I ran into the issue the other day of getting my Jenkins to be able to
run rake tasks on a remote server. So I decided to write a rake task
that allowed it to offload the connection to the remote box onto
Capistrano. This nifty little Rake task will allow you to run Rake tasks
in a production environment on a remote server using the Dynamic Duo of
Capistrano 3 and Rake.

desc "Easily Run rake task on a remote server"
  task :server_side_rake do
    on roles(:app), in: :sequence, wait: 5 do
      within release_path do
      with rails_env: :production do
      execute :rake, ENV['task'], "RAILS_ENV=production"
      end
    end
  end
end

Then you can run your one off rake task with the following command:

cap production server_side_rake task=one_time_rake_task --trace

Easy as that!

Easy Blogging with Rake and Jekyll

Fresh out of College, I read everything on how to make yourself more marketable. Several places suggested, write a blog to get your name out there. However, as a graduate, I was not looking to drop money on a hosted blog or pay for a server to run my own. Also there is something awkward about dot wordpress dot com sites, that just makes people run away from them.

In walks GitHub pages. A useful tool for you to make a simple site for a project, but as you can work on yourself, You could argue that you are your biggest project. So while building this blog I decided to use Jekyll since GitHub will allow me to host it for free on their servers.

In order to make the process easier I created a Rake file too easily create new Posts.

# Ask for title
def ask message
  print message
  STDIN.gets.chomp
end
title = ask('Title: ')

#Create new a post
desc "Default 'rake' command creates a new post"
task :default do
  filename = "#{Time.now.strftime('%Y-%m-%d')}-#{title.gsub(/\s/, '_').downcase}.markdown"
  path = File.join("_posts", filename)
  if File.exist? path; raise RuntimeError.new("File exists #{path}"); end
  File.open(path, 'w') do |file|
    file.write <<-EOS
---
layout: post
title: #{title}
date: #{Time.now.strftime('%Y-%m-%d %k:%M:%S')}
---

Content goes here
    EOS
  end

  # open the file in vim
  sh "vim #{path}"

end

Then to create a new blog post you just run:

rake