What is a good workflow for continuous deployment in VPS using Travis CI and Capistrano?

I have a Ruby web application hosted on Digital Ocean VPS. In the current setup, I have a bash script that lives on a VPS that does the following:

  • Stops the application server
  • Cloning the latest git repository code on the local server
  • Performs database migration
  • Application Server Launch
  • Cleaning

Obviously, this approach is not scalable (or even good). Therefore, I am trying to configure the deployment pipeline using Travis CI + Capistrano, which automatically installs, builds, tests and deploys this web application after pressing git.

So far, we have Travis CI that installs, builds, and tests our code each time git is pressed, but we come across the best way to complete the deployment step. Since we have JS and SASS files that need to be created using gulp, we cannot just pull it directly from git to the server and run it. And since Travis CI is already creating these files, we are curious whether it is possible to use the embedded files from Travis CI and send them directly from Travis CI to our servers.

Some options that we examined:


SCP + direct transmission

As an example of a custom FTP deployment on Travis CI, we could SCP create files on our server and invoke a bash script that starts the migration and restarts the application server.

Capistrano + direct transmission

We could install Capistrano on Travis CI and use it to directly transfer files that were built on Travis CI to our servers. Before packing and sending, we will need to clean any files / directories that do not need to be transferred (node_modules, gazebo, etc.). After migrating the package, we can again use Capistrano to retrieve it, start the database migration and restart the application server.

To do this, how do we minimize deployment downtime? And how can we manage bugs and rollbacks?

Capistrano + git

We could pull the Travis CI embedded files into the git tag on github and use Capistrano to pull the git tag on the server, start the database migration, and restart the application server.

For this, it seems that git releases should only be for prod, so how do we manage different environments (dev, stage and prod)?


In any case, we learned quite a lot on the Internet and could not find a solution for our needs that sets the best approach to deployment from Travis CI to an unsupported deployment provider.

Given the above situation, what is the best way to install from Travis CI to VPS?

+5
source share
1 answer

I ended up with the Capistrano task to pack the release from Travis CI and upload it to the appropriate server.

I had to create a new SCM in Capistrano (which I called Travis), which overwrite the tasks for creating the default SCM release.

I sent it to this github thread: https://github.com/capistrano/capistrano/issues/722#issuecomment-54653745

And to complete, the custom code for Capistrano is also below.

 set :scm, :git namespace :travis do desc 'Check that travis is reachable' task :check do exit 1 unless true end desc 'Package to release' task :create_release do run_locally do execute :mkdir, '-p', :'tmp' execute "tar -cz --exclude tests --exclude vendor --exclude .git --exclude node_modules --exclude tmp/#{fetch(:release_timestamp)}.tar.gz -f tmp/#{fetch(:release_timestamp)}.tar.gz ." end on release_roles :all do execute :mkdir, '-p', release_path upload! "tmp/#{fetch(:release_timestamp)}.tar.gz", "#{release_path}/#{fetch(:release_timestamp)}.tar.gz" execute "tar -xvf #{release_path}/#{fetch(:release_timestamp)}.tar.gz --directory #{release_path}" execute "rm #{release_path}/#{fetch(:release_timestamp)}.tar.gz" end run_locally do execute "rm -rf tmp" end end desc 'Determine the revision that will be deployed' task :set_current_revision do run_locally do set :current_revision, capture(:git, "rev-parse --short #{fetch(:branch)}") end end end namespace :deploy do desc 'Use Travis' task :use_travis do set :scm, :travis end before :starting, :use_travis end 
+1
source

Source: https://habr.com/ru/post/1200701/


All Articles