Last active
November 5, 2018 11:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Introduction
This script will check if all environment variables defined in
.env.dist
are properly set.Installation
bin/
directory of your Symfony 4 project.symfony/dotenv
is required). It will cause your build to fail if there's a missing environment variable.Usage
✔️ Will check if your environment variables are properly set according to your
.env.dist
.✔️ Will check if your environment variables + vars defined in
.env
file are properly set according to your.env.dist
.✔️ Will not return 1 in case of failure.
✔️ Will output an array of the variables defined in
.env.dist
and their values.