title: Laravel | Deployer and automatic deploy with gitlab ci
body: <p>i want to setup automatic deploy to my production server.</p>
  
  <p>This is my log from gitlab ci -></p>
  
  <pre><code>Running with gitlab-runner 11.9.0-rc2 (227934c0)
    on docker-auto-scale fa6cab46
  Using Docker executor with image lorisleiva/laravel-docker:latest ...
  Pulling docker image lorisleiva/laravel-docker:latest ...
  Using docker image sha256:4bd5ecacba7b0f46950944f376647090071a70a7b1ffa0eacb492719bd476c6b for lorisleiva/laravel-docker:latest ...
  Running on runner-fa6cab46-project-11286864-concurrent-0 via runner-fa6cab46-srm-1552945620-f480ce3e...
  Initialized empty Git repository in /builds/Woblex/web/.git/
  Fetching changes...
  Created fresh repository.
  From https://gitlab.com/Woblex/web
   * [new branch]      master     -&gt; origin/master
  Checking out ce51a64e as master...
  
  Skipping Git submodules setup
  Downloading artifacts for composer (179849020)...
  Downloading artifacts from coordinator... ok        id=179849020 responseStatus=200 OK token=zy8-CGce
  Downloading artifacts for npm (179849021)...
  Downloading artifacts from coordinator... ok        id=179849021 responseStatus=200 OK token=NvUWyzkg
  $ eval $(ssh-agent -s) # collapsed multi-line command
  Agent pid 11
  Identity added: (stdin) (git@gitlab.com)
  $ find . -type f -not -path "./vendor/*" -exec chmod 664 {} \;     # collapsed multi-line command
  $ whoami
  root
  $ php artisan deploy woblex.cz -s upload
  ✈︎ Deploying HEAD on woblex.cz with upload strategy
  ➤ Executing task deploy:prepare
  ✔ Ok
  ➤ Executing task deploy:lock
  ✔ Ok
  ➤ Executing task deploy:release
  ✔ Ok
  ➤ Executing task deploy:update_code
  ➤ Executing task deploy:failed
  ✔ Ok
  ➤ Executing task deploy:unlock
  ✔ Ok
  
  In Client.php line 99:
  
    The command "cd /var/www/dev.woblex.cz &amp;&amp; (/usr/bin/git clone   --recursive  
      git@gitlab.com:Woblex/web.git /var/www/dev.woblex.cz/releases/1 2&gt;&amp;1)" fa  
    iled.                                                                        
  
    Exit Code: 128 (Invalid exit argument)                                       
  
    Host Name: woblex.cz                                                         
  
    ================                                                             
    Warning: Identity file /home/gitlab/.ssh/id_rsa not accessible: No such fil  
    e or directory.                                                              
  
  
  deploy [-p|--parallel] [-l|--limit LIMIT] [--no-hooks] [--log LOG] [--roles ROLES] [--hosts HOSTS] [-o|--option OPTION] [--] [&lt;stage&gt;]
  
  
  In Process.php line 239:
  
    The command "vendor/bin/dep --file=vendor/lorisleiva/laravel-deployer/.buil  
    d/deploy.php deploy 'woblex.cz'" failed.                                     
  
    Exit Code: 128(Invalid exit argument)                                        
  
    Working directory: /builds/Woblex/web                                        
  
    Output:                                                                      
    ================                                                             
    ✈︎ Deploying HEAD on woblex.cz with upload strategy                          
    ➤ Executing task deploy:prepare                                              
    ✔ Ok                                                                         
    ➤ Executing task deploy:lock                                                 
    ✔ Ok                                                                         
    ➤ Executing task deploy:release                                              
    ✔ Ok                                                                         
    ➤ Executing task deploy:update_code                                          
    ➤ Executing task deploy:failed                                               
    ✔ Ok                                                                         
    ➤ Executing task deploy:unlock                                               
    ✔ Ok                                                                         
  
  
    Error Output:                                                                
    ================                                                             
  
    In Client.php line 99:                                                       
  
  
      The command "cd /var/www/dev.woblex.cz &amp;&amp; (/usr/bin/git clone   --recursi  
    ve                                                                           
        git@gitlab.com:Woblex/web.git /var/www/dev.woblex.cz/releases/1 2&gt;&amp;1)"   
    fa                                                                           
      iled.                                                                      
  
  
  
      Exit Code: 128 (Invalid exit argument)                                     
  
  
  
      Host Name: woblex.cz                                                       
  
  
  
      ================                                                           
  
      Warning: Identity file /home/gitlab/.ssh/id_rsa not accessible: No such f  
    il                                                                           
      e or directory.                                                            
  
  
  
  
    deploy [-p|--parallel] [-l|--limit LIMIT] [--no-hooks] [--log LOG] [--roles  
     ROLES] [--hosts HOSTS] [-o|--option OPTION] [--] [&lt;stage&gt;]                  
  
  
  ERROR: Job failed: exit code 1
  </code></pre>
  
  <p>I there is my deploy.php configuration -></p>
  
  <pre><code>&lt;?php
  
  return [
  
      /*
      |--------------------------------------------------------------------------
      | Default deployment strategy
      |--------------------------------------------------------------------------
      |
      | This option defines which deployment strategy to use by default on all
      | of your hosts. Laravel Deployer provides some strategies out-of-box
      | for you to choose from explained in detail in the documentation.
      |
      | Supported: 'basic', 'firstdeploy', 'local', 'pull'.
      |
      */
  
      'default' =&gt; 'basic',
  
      /*
      |--------------------------------------------------------------------------
      | Custom deployment strategies
      |--------------------------------------------------------------------------
      |
      | Here, you can easily set up new custom strategies as a list of tasks.
      | Any key of this array are supported in the `default` option above.
      | Any key matching Laravel Deployer's strategies overrides them.
      |
      */
  
      'strategies' =&gt; [
          //
      ],
  
      /*
      |--------------------------------------------------------------------------
      | Hooks
      |--------------------------------------------------------------------------
      |
      | Hooks let you customize your deployments conveniently by pushing tasks 
      | into strategic places of your deployment flow. Each of the official
      | strategies invoke hooks in different ways to implement their logic.
      |
      */
  
      'hooks' =&gt; [
          // Right before we start deploying.
          'start' =&gt; [
              //
          ],
  
          // Code and composer vendors are ready but nothing is built.
          'build' =&gt; [
              //
          ],
  
          // Deployment is done but not live yet (before symlink)
          'ready' =&gt; [
              'artisan:storage:link',
              'artisan:view:clear',
              'artisan:cache:clear',
              'artisan:config:cache',
              'artisan:migrate',
              'artisan:horizon:terminate',
          ],
  
          // Deployment is done and live
          'done' =&gt; [
              //
          ],
  
          // Deployment succeeded.
          'success' =&gt; [
              //
          ],
  
          // Deployment failed.
          'fail' =&gt; [
              //
          ],
      ],
  
      /*
      |--------------------------------------------------------------------------
      | Deployment options
      |--------------------------------------------------------------------------
      |
      | Options follow a simple key/value structure and are used within tasks
      | to make them more configurable and reusable. You can use options to
      | configure existing tasks or to use within your own custom tasks.
      |
      */
  
      'options' =&gt; [
          'application' =&gt; env('APP_NAME', 'Laravel'),
          'repository' =&gt; 'git@gitlab.com:Woblex/web.git',
          'git_tty' =&gt; false,
      ],
  
      /*
      |--------------------------------------------------------------------------
      | Hosts
      |--------------------------------------------------------------------------
      |
      | Here, you can define any domain or subdomain you want to deploy to.
      | You can provide them with roles and stages to filter them during
      | deployment. Read more about how to configure them in the docs.
      |
      */
  
      'hosts' =&gt; [
          'woblex.cz' =&gt; [
              'deploy_path' =&gt; '/var/www/dev.woblex.cz',
              'user' =&gt; 'gitlab',
              'identityFile' =&gt; '/home/gitlab/.ssh/id_rsa',
          ],
      ],
  
      /*
      |--------------------------------------------------------------------------
      | Localhost
      |--------------------------------------------------------------------------
      |
      | This localhost option give you the ability to deploy directly on your
      | local machine, without needing any SSH connection. You can use the
      | same configurations used by hosts to configure your localhost.
      |
      */
  
      'localhost' =&gt; [
          //
      ],
  
      /*
      |--------------------------------------------------------------------------
      | Include additional Deployer recipes
      |--------------------------------------------------------------------------
      |
      | Here, you can add any third party recipes to provide additional tasks, 
      | options and strategies. Therefore, it also allows you to create and
      | include your own recipes to define more complex deployment flows.
      |
      */
  
      'include' =&gt; [
          //
      ],
  
      /*
      |--------------------------------------------------------------------------
      | Use a custom Deployer file
      |--------------------------------------------------------------------------
      |
      | If you know what you are doing and want to take complete control over
      | Deployer's file, you can provide its path here. Note that, without
      | this configuration file, the root's deployer file will be used.
      |
      */
  
      'custom_deployer_file' =&gt; false,
  
  ];
  </code></pre>
  
  <p>I think that there is a problem with user connected to my ubuntu server from laravel deployer, because i set in config the username to 'gitlab', but if deployer connect to ubuntu server with this user it can see a file /home/gitlab/.ssh/id_rsa. Have anybody someting, that can help me? Thanks a lot!</p>
  
  <p>Edit: Here my gitlab-ci.yml</p>
  
  <pre><code>image: lorisleiva/laravel-docker:latest
  
  .init_ssh: &amp;init_ssh |
    eval $(ssh-agent -s)
    echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - &gt; /dev/null
    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    [[ -f /.dockerenv ]] &amp;&amp; echo -e "Host *\n\tStrictHostKeyChecking no\n\n" &gt; ~/.ssh/config
  
  # Replace the last line with the following lines if you'd rather
  # leave StrictHostKeyChecking enabled (replace yourdomain.com):
  #
  #  ssh-keyscan yourdomain.com &gt;&gt; ~/.ssh/known_hosts
  #  chmod 644 ~/.ssh/known_hosts
  
  .change_file_permissions: &amp;change_file_permissions |
    find . -type f -not -path "./vendor/*" -exec chmod 664 {} \;    
    find . -type d -not -path "./vendor/*" -exec chmod 775 {} \;
  
  composer:
    stage: build
    cache:
      key: ${CI_COMMIT_REF_SLUG}-composer
      paths:
        - vendor/
    script:
        - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
        - cp .env.example .env
        - php artisan key:generate
    artifacts:
      expire_in: 1 month
      paths:
        - vendor/
        - .env
  
  npm:
    stage: build
    cache:
      key: ${CI_COMMIT_REF_SLUG}-npm
      paths:
        - node_modules/
    script:
        - npm install
        - npm run production
    artifacts:
      expire_in: 1 month
      paths:
        - node_modules/
        - public/css/
        - public/js/
  
  #codestyle:
  #  stage: test
  #  dependencies: []
  #  script:
  #    - phpcs --standard=PSR2 --extensions=php --ignore=app/Support/helpers.php app
  
  phpunit:
    stage: test
    dependencies:
      - composer
    script:
      - phpunit --coverage-text --colors=never
  
  staging:
    stage: deploy
    script:
      - *init_ssh
      - *change_file_permissions
      - whoami
      - php artisan deploy woblex.cz -s upload
    environment:
      name: staging
      url: http://www.dev.woblex.cz
    only:
      - master
  
  production:
    stage: deploy
    script:
      - *init_ssh
      - *change_file_permissions
      - php artisan deploy new.woblex.cz -s upload
    environment:
      name: production
      url: https://www.new.woblex.cz
    when: manual
    only:
     - master
  </code></pre>