I am tired of running tests, waiting and then deploying every time. It's really annoying.

I'm not a robot. I do robots. I can automate this process.

Here is my example of continuous deployment #rails app.

I want to run tests and then deploy if all tests passed.

First, I have to run tests.

There are a lot of CIs and most of people use it: Travis CI, Jenkins CI, etc.

In our company we use Gitlab CI. It takes Travis'es config and runs its instructions.

# ...
  - "bundle exec rake spec"
  - "bundle exec rake production:deploy"

Scripts runs in sequence. So if one of the specs fail, deploy script will not be run.

What is in production:deploy?

This script runs deploy. But it's complicated to use shell and ssh on CI, so I can trigger a remote script, which deploys the app.

http = Net::HTTP.new("domain.com", 3666)
res = http.get "/my-secret-token"
raise "Bad Deploy" if res.code_type != Net::HTTPOK

I have a copy of app not only on CI and dev, but also in production!

Why don't use it.

Just create a simple server which catches deploy's requests and then runs deploy:

# config.ru
require "rack"

secret = "my-secret-token"
ci_ip = ""
app = lambda do |env|
  if env["PATH_INFO"] == "/#{secret}" && env["REMOTE_ADDR"] == ci_ip
    `cd /my/app/current && /path/to/cap deploy:migrations`
    [200, { "Content-Type" => "text/plain" }, ["okie-dokie"]]
    [500, { "Content-Type" => "text/plain" }, ["Bad Auth"]]

run app
rackup config.ru -p 3666 # use free port
# use -D for daemonize

That's all!

After these simple steps I don't worry about deploying and don't spend time on meditate on compiling assets.

#ruby #deploy #capistrano #ci