.env variables staging and prod

I am inserting some analytics tags into my wp_head using an analytics.php file in /site/web/app/themes/roots/app/analytics.php and including it in my functions .php collect(['setup', 'filters', 'analytics'])
I am loading the tracking code IDs from my .env file like so

 add_action( 'wp_head', function() {
    $tag_code = '';
    if(getenv('GTAG')) {
        $tag_code .= "
        <!-- Google tag (gtag.js) -->
        <script async src='https://www.googletagmanager.com/gtag/js?id=".getenv('GTAG')."'></script>
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());
          gtag('config', );
    if(getenv('GTM_ID')) {
        $tag_code .= "
        <!-- Google Tag Manager -->
        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
        <!-- End Google Tag Manager -->

    if(getenv('FB_PIXEL_CODE')) {
        $tag_code.= "
        <!-- Facebook Pixel Code -->
        s.parentNode.insertBefore(t,s)}(window, document,'script',
        fbq('init', ".getenv('FB_PIXEL_CODE').");
        fbq('track', 'PageView');
        <img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?id=".getenv('FB_PIXEL_CODE')."&ev=PageView&noscript=1' />
        <!-- End Facebook Pixel Code -->
    echo $tag_code;

my .env file I have added


This is working on development with the code outputting in the head as expected but when I deploy to staging the scripts are not being outputted which makes me think that the env variables are not being copied across.

Any thoughts?
Am I doing something wrong?

That all looks good to me.

I know you may already have checked, but is .env actually present on staging? Typically, env / dotfiles aren’t deployed, as they relate to a single environment, and you’re deploying a new environment.

Bedrock excludes .env (and others) from the repo via .gitignore by default.

That’s what I thought, so how do we get around that?
Should I scp an env file to the current directory with just those env vars?

Yes, exactly. If one already exists, add to the end of it.

So I think the best way to go about adding environment specific env variables is to add them to your environment group_vars in trellis (assuming you are using rellis which I am)