A Capistrano style super light-weight deployment script for Vue

Braga J
Francium Tech
Published in
2 min readJul 28, 2020

--

Photo by Brian Yurasits on Unsplash

One of the sexy features of capistrano if you had used it with Rails is how super simple it is deploy a Rails project. The DSL (Domain Specific Language) it uses is very straightforward for even a beginner to understand. However, with time, Rails at least in our experience has moved mostly to serve the backend alone with the Front end being taken over by the likes of Vue, React etc.,

In case of Vue, we now end up building the script for in the production machine itself. And if you want to deploy it in cost effective machines (that costs $5 a month in DigitalOcean), it becomes extremely resource intensive. Plus, the deployment does not have to be a pain.

Hence, at Francium we developed a custom script that combines all the goodness of Capistrano, using less resources of remote machine and deploying the file in no time. End of the day, the Vue project although with its might complexities with dependencies is going to spit out nothing but a html and few javascript files. Ideally, resource-wise a $5 machine is more than enough.

require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "sshkit"
end
require "sshkit"
require "sshkit/dsl"

SERVER = "root@mydomain.org"
ARCHIVE_NAME = "current.tar.gz"
REMOTE_DIRECTORY = "/var/www/my-vue-app"
USER = :root

include SSHKit::DSL

def setup
on [SERVER] do |host|
within REMOTE_DIRECTORY do
as USER do
execute :mkdir, :current
execute :mkdir, :backup
end
end
end
end

def remove_old_build
on [SERVER] do |host|
within REMOTE_DIRECTORY do
as USER do
execute :rm, "-rf backup"
end
end
end
end

def deploy
on [SERVER] do |host|
within REMOTE_DIRECTORY do
as USER do
upload! "#{ARCHIVE_NAME}", "."
execute :tar, "xzf #{ARCHIVE_NAME}"
execute :mv, "current/ backup/"
execute :mv, "dist current"
execute :rm, "#{ARCHIVE_NAME}"
end
end
end
end

def start
`rm -rf dist`
puts "cleaning up local build, staring the local build"
`npm run-script build`
puts "local build complete, taking backup"
remove_old_build
puts "backup removal complete, now archiving the new build"
`tar cvzf #{ARCHIVE_NAME} dist/`
deploy
puts "Build done."
`rm #{ARCHIVE_NAME}`
end

puts "starting #{ARGV[0]}"
send(ARGV[0])

Deploying this is as simple as running ruby deploy.rb start

The main advantage if you notice here is that it consumes absolutely none of the server resources for the build and exploits the local machine. Even, the network overhead is reduced by using compression.

Francium Tech is a technology company laser focused on delivering top quality software of scale at extreme speeds. Numbers and Size of the data don’t scare us. If you have any requirements or want a free health check of your systems or architecture, feel free to shoot an email to contact@francium.tech, we will get in touch with you!

--

--