How do I connect .env file to config.yml? - yaml

I'm currently building a Shopify store and would like to use env variables in Themekit's config.yml file. What I'm confused about is how to connect the .env file to the yml file, since I don't think you can just require dotenv. I have my .env file, and the code below in the config.yml. Thanks!
password: ${DEV_PASSWD}
theme_id: ${DEV_THEMEID}
store: ${DEV_SHOP}

You can't include .env file inside a YAML one. However, you can interpolate variables into your config.yml file using the ${} notation.
To help you interpolate variables, there are special files that can be used to automatically to load environment variables for Theme Kit. The following table lists the file paths for each operating system:
macOs: ${HOME}/Library/Application Support/Shopify/Themekit/variables
Linux/BSD: ${XDG_CONFIG_HOME}/Shopify/Themekit/variables
Windows: %APPDATA%\Shopify\Themekit\variables
Even more, you can use the --vars flag in any command to provide a path to a file for loading variables. The variables file has the same format as most .env type files. But note, the .env file is not interpolated by YAML itself and it cannot be connected using standard YAML include directives. All magic is provided exclusively by shopify and its --vars flag.

Related

How to use environment variable(string) in a .conf file

I have a .conf file that has a label and variable that I'm trying to move to a .env file. How can I accomplish this?
This is what I have in my .conf file
[[inputs.snmp]] #Label
agents = ["1.1.1.1:111","2.2.2.2:111","2.3.3.3:111"] #Variable
version = 2 #Variable
I'm trying to have something like this in the .env file
VAR_FOR_CONF_FILE='[[inputs.snmp]]\n agents= ["1.1.1.1:111","2.2.2.2:111","2.3.3.3:111"]\n version=2'
I was hoping I could use $VAR_FOR_CONF_FILE in my .conf file instead of [[inputs.snmp]]agents = ["1.1.1.1:111","2.2.2.2:111","2.3.3.3:111"]
but I keep getting this error Error parsing data: line 7: invalid TOML syntax for $VAR_FOR_CONF_FILE (Im not sure if I'm getting this error because I have the syntax wrong in my .env file or I'm declaring $VAR_FOR_CONF_FILE incorrectly in my .conf file)
Am I doing it correctly (or is it even possible to do what I'm trying to accomplish)?
(Note: I'm trying to accomplish this so I can simply use $VAR_FOR_CONF_FILE instead of hard coding things in the .conf file)
I'm trying to use VAR_FOR_CONF_FILE in the .conf file
If the .conf file parsing program doesn't support variable substitution, there's no way around modifying the .conf file, but this can be automated:
sed -i "s/\<VAR_FOR_CONF_FILE\>/$VAR_FOR_CONF_FILE/" my.conf

How to make Laravel read .env file again?

I'm changing Laravel .env variable after a process, and checking that variable everytime, if it's true code doing one thing, if it's false another thing. But after changing the .env variable I have to restart with php artisan serve, I don't want to do this. Why Laravel does not read the new env variable, it changes on the .env file itself.
You shouldn't have to manipulate environment variables during runtime. If you're trying to manipulate an environment variable, you should probably instead utilize a config value. These configuration files are placed in the config/ directory of your project.
Then during runtime, you can use the following built-in config helper to get/set your config variable.
$value = config('app.timezone');
// To set configuration values at runtime, pass an array to the config helper
config(['app.timezone' => 'America/Chicago']);
$newValue = config('app.timezone');
You can read more about this process here (you can change the version to your version):
https://laravel.com/docs/5.5/configuration#accessing-configuration-values

Is there any way to use dotenv with Bitbucket Pipelines?

As the title says, is there any way to use dotenv with Bitbucket Pipelines for CI purposes, while still adding the (perhaps multiple) (.stage).env to .gitignore?
I know Pipeline supports environment variables, and that they can be referenced in bitbucket-pipelines.yml, but I can't figure out how to use dotenv files instead, and vary which file to use based on i.e. branch patterns.
For example, I'd like commits to develop to use .test.env variables, while commits to master instead uses the variables from .prod.env.
Perhaps I'm going down the wrong path? Although other websites use examples of multiple .env files, the library authors discourage that approach. I'm using Zeit Now for hosting, so I can't just SSH a .env file onto the server.
Any advice is very welcome :-)
Create a base64 string out of your .env file. Then copy this string into your environment variables of your pipeline, see here: https://confluence.atlassian.com/bitbucket/environment-variables-794502608.html
For example, your content is now defined in APP_ENV, then you can use this line in your pipeline configuration file:
echo $APP_ENV | base64 --decode --ignore-garbage > ./www/.env
Now it is save because nobody knows your secrets in this file except your pipeline container itself.
This method could be used for all .env-files, also staging files. :)
Rename the files inside your develop pipelines:
mv .test.env .env
or in your master pipelines:
mv .prod.env .env

set env variables in systemd according to content of the file

Is it possible to set env variables in systemd units according to content of the file. Something like ENV=`cat somefile`?
I need to set path to the executable file according to content of somefile. I didn't find any info about variables in systemd ,exept env variables so I try to use them, but with no luck
I've tried to use Environment = 'ENV=`cat somefile`' but with no luck. It just set the value of the variable to `cat somefile`. I've tried to use cat and /bin/cat but result is the same.
There is a special directive exactly for this purpose: EnvironmentFile. You should put it into your [Service] section like this:
[Service]
EnvironmentFile=/etc/default/somefile
If the file isn't there the service won't start, so to be able to start it despite the presence of the file you can add leading - sign in front of the file name, like:
[Service]
EnvironmentFile=-/etc/default/somefile
Be aware, that although this file looks like shell script it's not and behaviour of variables declaration sligtly differs, in particular, you can't reference other variables in the declaration of the variable.
Please, read EnvironmentFile and Environment options description for more details. Command lines could be helpfl too.

Laravel 5 doesn't read values from dot ENV files

I don't know if this question is relevant or not. LARAVEL 5 is still in developmental phase. I have pulled LARAVEL 5 after watching one of the Laracast video about new features in LARAVEL 5. I couldn't resist to wait for its formal release.
I named the local environment dot file as .env.local.php. But for some reason I am unable to get the the values from this dot file when using $_ENV['KEY'].
I am quite sure that I have configured the environment correctly. When doing $app->environment() shows the correct environment. Has it been changed in LARAVEL 5 the way we get the values from dot files or am I missing something ?
By default in environment.php file you have something like that:
if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');
}
so only .env file is being read (notice .env not .env.php - so you should rename your file - or you can add as 2nd parameter file name .env.php if you want). Any other environment files (.local.env) are not being read by default - you will need to load them manually.
If you don't have such code by default, you should probably update/install Laravel 5 again (changes appear very often)
Now, I don't know what method you use, but you can put in your .env file also your environment name in for example APP_ENV variable, create .local.env file with content you want and then you could use in environment.php file:
if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');
if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
echo "loading";
Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
}
}
If you don't want to do it this way, you can probably change the other and load env file you want based on $env assuming you use PC based environment detection.
If it's unclear you can also look at What's the correct way to set ENV variables in Laravel 5?

Resources