Skip to content

Handle .env files the right way when deploying

License

Notifications You must be signed in to change notification settings

Setono/deployer-dotenv

Repository files navigation

DotEnv handling with Deployer

Latest Version Software License Build Status

If you use Deployer as your deployment tool and .env files to handle environment variables (i.e. Symfony) this library is for you.

Are you still accessing your server to update environment variables manually after a deployment? We also did that and that's the main reason why we built this library.

Now we have a very specific, but simple, strategy for updating the .env files during deployment:

  • We do not share the .env.local.php, .env.local files as is the default by Deployer. Instead, we have a .env.[stage].local and .env.local.php in each release folder.

  • When deploying we copy the .env.[stage].local file from the previous release (if there was a previous release, else we create it).

  • If you are deploying interactively (i.e. manually) you are presented with a dialog asking if you want to update any environment variables.

  • Finally, we run composer symfony:dump-env [stage] to generate the .env.local.php file for the current release.

Installation

composer require setono/deployer-dotenv

Usage

In your deploy.php file require the recipe:

<?php

namespace Deployer;

require_once 'recipe/setono_dotenv.php';

// ...

This will automatically hook into the default flow of Deployer.

Testing

  1. Set correct permissions on the SSH keys:

    chmod 600 tests/docker/ssh/id_rsa && chmod 644 tests/docker/ssh/id_rsa.pub
  2. Build the Docker image:

    docker build -t setono/deployer-dotenv --no-cache ./tests/docker
  3. Run the Docker container:

    docker run -d -p 2222:22 setono/deployer-dotenv
  4. Run the tests:

    vendor/bin/phpunit