bootstrap.php how to include environment variables for multi-environment configuration?
So debugging this a bit, it looks like its to do with the namespace declaration on the original template I was using for the migration. Basically when declaring namespace Craft this causes problems with loading the environment variables that were previously being masked by the die() error, as I was including Bootstrap too early.

Uncaught Error: Class 'Craft\Dotenv' not found


In my case, the phpdotnev stuff is going to need to appear before including Bootstrap otherwise Craft isn't going to be able to connect to the DB.

Moving the vendor and environment variables stuff up before loading the bootstrap file from the Craft folder, seems to work. This a test file I have now:

&lt;?php
namespace Craft;

// Avoid errors when running through console
$_SERVER['HTTP_HOST'] = 'example.craft.localdev';
$_SERVER['REQUEST_URI'] = '/index.php';

// Get the root dir so we can run migrations without worrying about path
$root_dir = dirname(dirname(__FILE__));

// We need to load in phpdotenv stuff before initialising bootstrap.php, otherwise you won't be able to connect to the DB
require_once($root_dir . '/vendor/autoload.php');

// If APP_SECRETS is not set, load in .env file
if (!getenv('APP_SECRETS'))
{
    $envFile = '.env';

    if (file_exists($root_dir . '/' . $envFile))
    {
        // PHP dotenv: https://github.com/vlucas/phpdotenv
        $dotenv = new \Dotenv\Dotenv($root_dir);
        $dotenv-&gt;load();
        $dotenv-&gt;required(['APP_SECRETS', 'CRAFT_ENVIRONMENT']);
    }
    else
    {
        die("$envFile does not exist.");
    }
}

// Set the Craft environment with the value set in .env
define('CRAFT_ENVIRONMENT', getenv('CRAFT_ENVIRONMENT'));

// Initialize Craft without letting it take over the whole request
$app = require $root_dir . '/craft/app/bootstrap.php';

// Set Craft to Craft Personal so we don't get any errors about Client/Pro-only components not being available
$app-&gt;getInfo()-&gt;edition = 0;

// CUSTOM CODE FOR MIGRATIONS AND OTHER CRAFT RELATED STUFF GOES BELOW THIS LINE


This then appears to be using the proper environment variables. I tested by editing my db.php file to break one of the variables being pulled from secrets.json to cause the DB connection error and it does appear to be work, as the try and catch statement is responding appropriately.

Not sure if there will be an issue not having the namespace Craft declared or not, but I appear to be able to interact with Craft through $app still. Would this be correct?