title: Deploy angular app using capistrano
body: <p>I don't know if it's the best way but I would like to use capistrano (through gitlab-ci) to deploy my angular 5 app on a remote server.</p>
  
  <p>I have a problem with npm install and ng build. The node_modules seems to be empty for capistrano. Here the config file.</p>
  
  <h1>deploy.rb</h1>
  
  <pre><code>set :application, 'bo'
  set :repo_url, 'git@...'
  set :keep_releases, 5
  
  set :deploy_via, "remote_cache_with_project_root"
  set :deploy_to, "/var/www/bo/"
  set :current_directory, "bo"
  set :npm_flags, '--production' # default
  
  after 'deploy:published','test:npm_install'
  after 'deploy:published','test:build'
  
  ...
  </code></pre>
  
  <h1>tasks</h1>
  
  <h1>npm_install.cap</h1>
  
  <pre><code>namespace :test do
      desc 'build bo'
      task :npm_install do 
          on roles(:app), in: :sequence, wait: 5 do
              execute :npm, "install"
          end
      end
  end
  
  ...
  </code></pre>
  
  <h1>build.cap</h1>
  
  <pre><code>namespace :test do
      desc 'build bo'
      task :build do 
          on roles(:app), in: :sequence, wait: 5 do
              execute "ng build --aot --env=prod --sourcemaps=false --named-chunks"
          end
      end
  end
  
  .... 
  </code></pre>
  
  <p>Errors I can see in gitlab-ci :</p>
  
  <pre><code>cap aborted!
  SSHKit::Runner::ExecuteError: Exception while executing as bo@test.intra: ng build --aot --env=prod --sourcemaps=false --named-chunks exit status: 1
  ng build --aot --env=prod --sourcemaps=false --named-chunks stdout: Nothing written
  ng build --aot --env=prod --sourcemaps=false --named-chunks stderr: node_modules appears empty, you may need to run `npm install`
  /home/gitlab-runner/builds/7599a2f4/0/bo/deploy/tasks/build.cap:4:in `block (2 levels) in &lt;top (required)&gt;'
  
  Caused by:
  SSHKit::Command::Failed: ng build --aot --env=prod --sourcemaps=false --named-chunks exit status: 1
  ng build --aot --env=prod --sourcemaps=false --named-chunks stdout: Nothing written
  ng build --aot --env=prod --sourcemaps=false --named-chunks stderr: node_modules appears empty, you may need to run `npm install`
  /home/gitlab-runner/builds/7599a2f4/0/bo/deploy/tasks/build.cap:5:in `block (3 levels) in &lt;top (required)&gt;'
  /home/gitlab-runner/builds/7599a2f4/0/bo/deploy/tasks/build.cap:4:in `block (2 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>Thank you very much</p>