Deploy

Capistrano

Example config for deploy on dev

role :app, %w[mp3pool@54.36.199.27] # ssh user and ip of remote server
role :web, %w[mp3pool@54.36.199.27]
role :db,  %w[mp3pool@54.36.199.27]

set :tmp_dir, '/home/mp3pool/tmp-mp3pool'
set :nginx_server_name, 'mp3pool-api.themindstudios.com' # not required but nice to mention
set :rails_env, 'dev'

set :deploy_to, '/var/www/mp3pool-api' # directory where project located on remote server
set :rvm_ruby_version, 'ruby-3.0.1@mp3pool' # set this from .ruby-version and .ruby-gemset
set :rvm_custom_path, '/opt/rvm'

set :puma_service_unit_name, 'mp3pool-api' # do not forget to create service in /etc/systemd/system

set :branch, :dev # git branch to deploy from

set :sidekiq_service_unit_name, 'mp3pool-api-sidekiq' # same as for puma
set :sidekiq_service_unit_user, :system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Example config for puma service

[Unit]
Description=mp3pool-api
After=network.target

# Uncomment for socket activation (see below)
# Requires=puma.socket

[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple

# Preferably configure a non-privileged user
# User=mp3pool

# The path to the puma application root
# Also replace the "<WD>" place holders below with this path.

# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
# The command to start Puma. This variant uses a binstub generated via
# `bundle binstubs puma --path ./sbin` in the WorkingDirectory
# (replace "<WD>" below)
WorkingDirectory=/var/www/mp3pool-api/current
User=mp3pool
Group=mp3pool
ExecStart=/opt/rvm/gems/ruby-3.0.1@mp3pool/wrappers/bundle exec puma -C /var/www/mp3pool-api/shared/puma.rb
# Variant: Use config file with `bind` directives instead:
# ExecStart=<WD>/sbin/puma -C config.rb
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub

Restart=always

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

Example config for sidekiq service

[Unit]
Description=mp3pool-api-sidekiq
After=network.target

[Service]
Type=simple

WorkingDirectory=/var/www/mp3pool-api/current
User=mp3pool
Group=mp3pool

ExecStart=/opt/rvm/gems/ruby-3.0.1@mp3pool/wrappers/bundle exec sidekiq -e dev -C /var/www/mp3pool-api/current/config/sidekiq.yml

# SyslogIdentifier=mp3pool-api-sidekiq
StandardOutput=append:/var/www/mp3pool-api/shared/log/sidekiq.log
StandardError=append:/var/www/mp3pool-api/shared/log/sidekiq.log

Restart=always

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Gitlab CI

Example CI config for deployment

image: ruby:2.4
stages:
  - deploy

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - vendor/bundle
    - node_modules

variables:
  BUNDLE_PATH: vendor/bundle

before_script:
  # Install node and some other deps
  - curl -sL https://deb.nodesource.com/setup_12.x | bash -
  - apt-get update -yq
  - apt-get install -y apt-transport-https build-essential cmake nodejs unzip

  # Install yarn
  - wget -q -O - https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
  - echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
  - apt-get update -yq
  - apt-get install -y yarn

  # Project setup
  # Check if the dependencies are ok, if not install what is missing
  - ruby -v
  - which ruby
  - gem install bundler --no-document
  - bundle check || bundle install -j $(nproc)
  - yarn install --check-files

deploy:
  environment: production
  stage: deploy
  script:
    - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
    - eval $(ssh-agent -s)
    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo -e "$SSH_PRIVATE_KEY")
    - bundle exec cap production deploy
  only:
    - master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44