Home

Awesome

Gitlab CI/CD pipeline with aws integration for magento 2 deployment

Feature highlights

Important configuration information

# Magento log rotation
45 20 * * * /usr/sbin/logrotate /etc/logrotate.d/magento.cfsaved -s /var/lib/logrotate/magento.status

Contents overview

  1. Project sequence diagram
  2. Pre-Requistices
  3. Configure repository and user for deployment
  4. Configure gitlab server
  5. Configure golden server
  6. Usage of pipeline and extra information

1. Project sequence diagram

Deployment Pipeline

2. Pre-Requistics

3. Configure repository and user for deployment

Existing magento repository

cp .gitlab-ci.yml.dis $path_to_your_project/.gitlab-ci.yml
cp .gitignore.dis $path_to_your_project/.gitignore
cp .rsyncignore.dis $path_to_your_project/.rsyncignore
cp -r .ec2.dis $path_to_your_project/.ec2

Repository for cloud

User for deployment

4. Configure gitlab server

Configure gitlab-runner user

# Passwordless sudo commands
gitlab-runner ALL=NOPASSWD: /usr/sbin/openvpn, /usr/bin/pkill

Configure ssh

Host production_golden
    User ubuntu
    HostName $host_ip
    IdentityFile ~/.ssh/$key_name.pem

Host staging_golden
    User ubuntu
    HostName $host_ip
    IdentityFile ~/.ssh/$key_name.pem
ssh $ssh_alias_name

Configure this repository for gitlab

# Aws Openvpn config
cloud_aws="--config /etc/openvpn/client/$vpn_name.ovpn"

# Root path
scripts_root="/home/gitlab-runner/gitlab-aws-magento2-deploy/scripts/gitlab"
cp -a .env.dis .env.prod
cp -a .env.dis .env.stag

5. Configure golden server

Configure mage/build root and ssh

Host $gitlab_domain
   HostName $host_ip
   IdentityFile ~/.ssh/$key_name.pem
rm -rf var/view_preprocessed
mkdir -p symlinks/view_preprocessed
cd var && ln -snf ../symlinks/view_preprocessed .
$tmpMaterializationDir = dirname(__DIR__, 1).'/symlinks/view_preprocessed/pub/static';
$params = $_SERVER;
$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = array_replace_recursive(
    $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] ?? [],
    [
        DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
        DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
        DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
        DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
        DirectoryList::TMP_MATERIALIZATION_DIR => [DirectoryList::PATH => $tmpMaterializationDir]
    ]
);

rm -r .ec2 .git .gitignore .gitlab-ci.yml
sudo rsync -a --exclude-from=".rsyncignore" $build_root/ . --delete
rm -rf var/view_preprocessed
mkdir -p symlinks/view_preprocessed
cd var && ln -snf ../symlinks/view_preprocessed .

Configure this repository for golden

# Root path
mage_root="/var/www/html/$domain_name"
build_root="/var/www/html/build"
scripts_root="/home/ubuntu/gitlab-aws-magento2-deploy/scripts/ec2"

cp -a .env.dis .env.prod
cp -a .env.dis .env.stag

6. Usage of pipeline and extra information

git push origin $branch_name -o ci.variable="FORCE_APPLICATION_BUILD=1"