Configure Deployment via GitLab

To automatically deploy via gitlab we need to configure

{project_name}
| .gitlab-ci.yml
└─── deploy
   |  deploy.php

./.gitlab-ci.yml

image: php:7.2-stretch

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan -H staging.ext.dev >> ~/.ssh/known_hosts
  - apt-get update -yqq
  - apt-get install -yqq git libmcrypt-dev libpq-dev libcurl4-gnutls-dev libicu-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev rsync zip
  - docker-php-ext-install zip
  - curl -sS getcomposer.org/installer | php
  - php composer.phar install

deploy_staging:
  only:
    - staging
  script:
    - php deploy/deployer.phar --file=deploy/deploy.php deploy staging

./deploy/deploy.php

Make sure to replace [domain] with the actual domain of the TYPO3 website.

<?php
namespace Deployer;

require 'recipe/common.php';
require 'recipes/rsync.php';

set('keep_releases', 5);

set('rsync', [
    'exclude' => [
        'public/fileadmin',
        'public/typo3temp',
        'docker',
        'deploy',
        '.git',
        '.gitignore',
        '.gitlab-ci.yml',
        'docker-compose.yml'
    ],
    'exclude-file' => false,
    'include' => [],
    'include-file' => false,
    'filter' => [],
    'filter-file' => false,
    'filter-perdir' => false,
    'flags' => 'rz',
    'options' => [
        'delete',
        'links'
    ],
    'timeout' => 3600,
]);

set('rsync_src', 'src/typo3');

set('bin/php', 'php72.bin.cli -d allow_url_fopen=On');


set('shared_files', [
    'public/typo3conf/AdditionalConfiguration.php'
]);

set('shared_dirs', [
    'public/fileadmin',
    'public/typo3temp',
    'var'
]);

host('staging.ext.dev')
    ->stage('staging')
    ->user('root')
    ->set('http_user', 'www-data')
    ->set('writable_mode', 'chown')
    ->set('rsync_src', '.')
    ->set('rsync_dest','{{release_path}}')
    ->set('deploy_path', '/var/www/deployments/[domain].staging.ext.dev');


// Tasks...

task('deploy', [
    'deploy:prepare',
    'deploy:lock',				// lock, prevent synchrone deployments in same project
    'deploy:release',
    'rsync:warmup',				// rsync reuse previous release
    'rsync',					// rsync new code
    'deploy:shared',
    'deploy:writable',
    'deploy:clear_paths',
    'deploy:symlink',
    'deploy:unlock',
    'cleanup',
    'success'
]);

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');