Skip to main content

Deploying Sylius with Deployer

· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

Deployer is a valuable tool that simplifies and automates the deployment process of PHP applications in a traditional non-container environment.

One of the many out-of-the-box recipes offers a default deployment workflow for Symfony application, and since Sylius is built on top of Symfony, the recipe is an excellent start for our task.

To use the Symfony recipe for a Sylius deployment, create a deploy.php file in your project repository with the following contents:

<?php

namespace Deployer;

require 'recipe/symfon.php';

// Configure project name and Git repository
set('application', 'My Sylius website');
set('repository', 'git@example.com:customer/sylius.git');

// import the hosts configuration
import(__DIR__.'/hosts.yml');

In the hosts.yaml file, you define all hosts to deploy the application to:

hosts:
www.example.com:
deploy_path: /var/www/html/
remote_user: sylius-user

This allows you in your deployment pipeline to run a deployment by calling Deployer like this:

./bin/dep deploy --file ./deploy.php -tag "3.2.1"

The command above will run the Sylius deployment and ensure that the Git tag "3.2.1" gets deployed on the server and not the latest state from the master branch.

However, the default Symfony recipe cannot fully deploy a Sylius application. We need to add a task for deploying the Sylius bundles assets:

task('deploy:sylius:assets', function () {
run("cd {{release_or_current_path}} && {{bin/console}} sylius:install:assets --no-interaction");
run("cd {{release_or_current_path}} && {{bin/console}} sylius:theme:assets:install public --no-interaction");
});
after('deploy:vendors', 'deploy:sylius:assets');

We also need to compile the Javascript assets so that the Sylius store is fully functional. For this to work, the following task is enough:

task('deploy:javascript:upload', function () {
run("cd {{release_or_current_path}} && yarn && yarn build:prod");
});
after('deploy:vendors', 'deploy:javascript:upload');

If we run the deployment again, we've deployed a working Sylius store! Great.