Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Ansistrano Stages & Shared Files

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $9.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

When we deploy, our code is delivered to the server. But there are a few other things we still need to do, like setting up database credentials in parameters.yml.

The Ansistrano Deploy Stages

Scroll up to the top of the Ansistrano documentation and find the Main Workflow link. When we deploy, Ansistrano goes through five stages: Setup, Update Code, Symlink Shared, Symlink, and Clean Up. The reason this is really interesting is that we can add our own custom tasks before or after any of these stages. For example, we could add a hook to run composer install or create parameters.yml.

The most important stages are "Update Code" - that's when our code is pulled down from git and put into the new releases directory - and "Symlink", which is when the current symlink is changed to that new directory. It's at that moment that the site becomes live and traffic starts using the new code.

But look at the third stage: "Symlink Shared". Right now, each release is completely separate from the others. We have 3 releases in 3 entirely isolated directories: nothing is shared. But sometimes... you do want a file or directory to be shared between deployments. For example, a log file: I want to have just one log file that's used across deployments. I don't want each deployment to create a new, empty log file.

In Ansistrano, this is done via the shared/ directory. It's empty right now, but we can configure it to hold certain shared paths. For example, eventually, we will want the var/logs directory to be shared. We'll actually do this later, but I want you to understand how it works now. When you configure var/logs to be shared in Ansistrano, on the next deploy, this directory will be created inside shared/. Then, every release will have a symlink to this shared directory.

That's what the "Symlink Shared" stage does: it creates all the shared symlinks in the new release directory. That's important, because - after this stage - your code should be fully functional.

Creating parameters.yml

Google for "Symfony deployment basics": you should find Symfony's deployment article. It lists the basic things you need to do when deploying a Symfony application, like upload the code, install vendor dependencies and create your app/config/parameters.yml file. Let's handle that next... via an Ansistrano hook!

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

While the fundamentals of Ansistrano haven't changed, this tutorial is built using Symfony 3, which has significant differences versus Symfony 4 and later.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "doctrine/doctrine-bundle": "^1.6", // 1.6.8
        "doctrine/orm": "^2.5", // v2.7.2
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "sensio/distribution-bundle": "^5.0.19", // v5.0.20
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.26
        "symfony/monolog-bundle": "^3.1.0", // v3.1.0
        "symfony/polyfill-apcu": "^1.0", // v1.4.0
        "symfony/swiftmailer-bundle": "^2.3.10", // v2.6.3
        "symfony/symfony": "3.3.*", // v3.3.5
        "twig/twig": "^1.0||^2.0", // v1.34.4
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
        "predis/predis": "^1.1", // v1.1.1
        "composer/package-versions-deprecated": "^1.11" // 1.11.99
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.6
        "symfony/phpunit-bridge": "^3.0", // v3.3.5
        "doctrine/data-fixtures": "^1.1", // 1.3.3
        "hautelook/alice-bundle": "^1.3" // v1.4.1
    }
}

What Ansible libraries does this tutorial use?

# ansible/requirements.yml
-
    src: DavidWittman.redis
    version: 1.2.4
-
    src: ansistrano.deploy
    version: 2.7.0
-
    src: ansistrano.rollback
    version: 2.0.1
userVoice