Skip to content

Instantly share code, notes, and snippets.

@bpolaszek
Last active November 5, 2018 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpolaszek/559dbc341dec51303fc0dea8162cf735 to your computer and use it in GitHub Desktop.
Save bpolaszek/559dbc341dec51303fc0dea8162cf735 to your computer and use it in GitHub Desktop.
Check that all environment variables defined in your .env.dist are properly set.
#!/usr/bin/env php
<?php
/**
* Checks that all environment variables are set before deploying.
*
* @author Beno!t POLASZEK - 2018
* @link https://gist.github.com/bpolaszek/559dbc341dec51303fc0dea8162cf735#gistcomment-2747882
*/
define('PROJECT_DIR', dirname(__DIR__));
require_once PROJECT_DIR . '/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Dotenv\Exception\PathException;
if (!class_exists(Dotenv::class)) {
print('Error: You probably forgot to `composer req [--dev] symfony/dotenv.` 😉') . PHP_EOL;
exit(1);
}
chdir(PROJECT_DIR);
(new Application('check:environment:variables'))
->register('check:environment:variables')
->addArgument('env-file', InputArgument::IS_ARRAY, '(Optionnal) The .env file(s) to load.')
->addOption('ignore-vars', null, InputOption::VALUE_OPTIONAL, 'Variables to ignore, separated by commas.')
->addOption('no-error', null, InputOption::VALUE_NONE, 'Do not produce error messages.')
->setCode(function (InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$dotEnv = new Dotenv();
$distFile = getcwd() . '/.env.dist';
if (!is_readable($distFile) || is_dir($distFile)) {
throw new PathException($distFile);
}
$definedVars = array_keys($dotEnv->parse(file_get_contents($distFile, '.env.dist')));
$ignoredVars = null === $input->getOption('ignore-vars') ? [] : explode(',', $input->getOption('ignore-vars'));
// Load environment variables
foreach($input->getArgument('env-file') as $envFile) {
$dotEnv->load($envFile);
}
// Compare
$values = [];
foreach ($definedVars as $variable) {
$values[$variable] = getenv($variable);
}
$missingVars = array_keys(
array_filter(
$values,
function ($value) {
return false === $value;
}
)
);
// Display
if ($io->isVerbose()) {
$rows = [];
foreach ($values as $variable => $value) {
if (in_array($variable, $ignoredVars)) {
$rows[] = [$variable, in_array($variable, $missingVars) ? ('<fg=yellow>NOT SET (IGNORED)</>') : $value];
} else {
$rows[] = [$variable, in_array($variable, $missingVars) ? ('<fg=red>MISSING</>') : $value];
}
}
$io->table(['Environment variable', 'Value'], $rows);
}
$missingVars = array_diff($missingVars, $ignoredVars);
if (count($missingVars) > 0) {
if (true !== $input->getOption('no-error')) {
$io->error(sprintf('Missing Environment variables: %s', implode(', ', $missingVars)));
return 1;
}
$io->warning(sprintf('Missing Environment variables: %s', implode(', ', $missingVars)));
return 0;
}
$io->success('All environment variables are set. 👍');
return 0;
})
->getApplication()
->setDefaultCommand('check:environment:variables', true)
->run();
@bpolaszek
Copy link
Author

Introduction

This script will check if all environment variables defined in .env.dist are properly set.

Installation

  • Put it in the bin/ directory of your Symfony 4 project.
  • Insert it in your CI, once vendors are installed (symfony/dotenv is required). It will cause your build to fail if there's a missing environment variable.

Usage

./bin/check-environment-variables

✔️ Will check if your environment variables are properly set according to your .env.dist.


./bin/check-environment-variables .env

✔️ Will check if your environment variables + vars defined in .env file are properly set according to your .env.dist.


./bin/check-environment-variables --no-error

✔️ Will not return 1 in case of failure.


./bin/check-environment-variables -v

✔️ Will output an array of the variables defined in .env.dist and their values.

------------------------------- ----------------------------------- 
  Environment variable            Value                              
 ------------------------------- ----------------------------------- 
  APP_ENV                         dev                                
  APP_SECRET                      23a92e2185563187434706c01d2c7a8f   
  PROXY_BASE_URI                  MISSING
 ------------------------------- ----------------------------------- 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment