How are Environment Variables used with Gatsby? - environment-variables

In the context of GatsbyJS, how are environment variables supposed to be used? I've read and re-read the official docs but I'm still confused.
Specifically:
Does Gatsby automatically read the .env.development file when doing gatsby develop?
Similarly, does it ignore the .env.development file and only read the .env.production when doing gatsby build?
Are you supposed to add .env.development and .env.production to .gitignore?
Are you supposed to manually copy (e.g. via scp) the .env.* files to your servers?

I used .env.development and .env.production files, but didn't need dotenv because I wasn't trying to get these env variables into node.js. I wanted these environment variables in JS files.
Q1) Yes. If you make an .env.development file in the root directory, add this one line of code IMG_URL=https://picsum.photos/200/300?image=1079, and then in your JS add this line of code <img src={'${process.env.IMG_URL}'} alt="" /> in a render(){} function in one of your react components. Then gatsby develop, you'll see the image
Q2) Yes it appears to. I created .env.development and .env.production files, defined a variable in the development one, then deployed to production. The env variable wasn't defined in the .env.production file.
Q3) hmm this probably depends on your repo. If it's a public or private. As well as the contents of the file. If you're using the .env files for API URLs etc.. that's fine, but Secrets shouldn't be in the .env files or the repo.
Q4) I'd say that committing and deploying are generally different. Unless you have a setup with auto deployments on commit. I have added my files to my .gitignore then copy all my local files to my live server. Maybe someone else has a better answer to this one.
Maybe this approach might be better for you alternative config go to oliverbenns comment

Related

Why isn't telegraf reading environmental variables?

My goal is to put my telegraf config into source control. To do so, I have a repo in my user's home directory with the appropriate config file which has already been tested and proven working.
I have added the path to the new config file in the "default" environment variables file:
/etc/default/telegraf
like this:
TELEGRAF_CONFIG_PATH="/home/ubuntu/some_repo/telegraf.conf"
... as well as other required variables such as passwords.
However, when I attempt to run
telegraf --test
It says No config file specified, and could not find one in $TELEGRAF_CONFIG_PATH etc.
Further, if I force it by
telegraf --test --config /home/ubuntu/some_repo/telegraf.conf
Then the process fails because it is missing the other required variables.
Questions:
What am I doing wrong?
Is there not also a way of specifying a config directory too (I would like to break my file down into separate input files)?
Perhaps as an alternative to all of this... is there not a way of specifying additional configuration files to be included from within the default /etc/telegraf/telegraf.conf file? (I've been unable to find any mention of this in documentation).
What am I doing wrong?
See what user:group owns /etc/default/telegraf. This file is better used when running telegraf as a service via systemd. Additionally, if you run env do you see the TELEGRAF_CONFIG_PATH variable? What about your other variables? If not, then you probably need to source the file first.
Is there not also a way of specifying a config directory too (I would like to break my file down into separate input files)?
Yes! Take a look at all the options of telegraf with telegraf --help and you will find:
--config-directory <directory> directory containing additional *.conf files
Perhaps as an alternative to all of this... is there not a way of specifying additional configuration files to be included from within the default /etc/telegraf/telegraf.conf file? (I've been unable to find any mention of this in documentation).
That is not the method I would suggest going down. Check out the config directory option above I mentioned.
Ok, after a LOT of trial and error, I figured everything out. For those facing similar issues, here is your shortcut to the answer:
Firstly, remember that when adding variables to the /etc/default/telegraf file, it must effectively be reloaded. So for example using ubuntu systemctl, that requires a restart.
You can verify that the variables have been loaded successfully using this:
$ sudo strings /proc/<pid>/environ
where <pid> is the "Main PID" from the telegraf status output
Secondly, when testing (eg telegraf --test) then (this is the part that is not necessarily intuitive and isn't documented) you will have to ALSO load the same environmental variables into the current user (eg: SET var=value) such that running
$ env
shows the same results as the previous command.
Hint: This is a good method for loading the current env file directly rather than doing it manually.

Prevent the .env file being copied to a Docker container

i'm newbie in Docker and i'm trying to make a "Starter-Boilerplate" with express and mongodb,
but i don't want the .env file in my docker image/containers, i've tried with .dockerignore file but the .env file still being copied inside the container in all docker-compose builds.
My .dockerignore file:
node_modules
.env
Link of the repo to reproduce the case:
https://github.com/josuerodcat90/expressjs-server-starter/tree/docker
Any idea or suggestion?, thanks in advance mates.
I found a solution for these cases when you need to keep the docker volumes without troubles with your .env file and your docker-compose rules, see below:
First i declare 2 new variables in my .env file:
ENV=DOCKER //this can switch between DOCKER and LOCAL
DOCKER_PORT=3000 //for example
and then i've made a little change in my index.js and database.js files, when im working in Docker the enviroment vars must be different of my Local vars, and then the problem solves.
Thanks to all for your help and interest in my issue, sorry for my bad english haha.
Create a .dockerignore file if you don't already have one and add **/*.env inside it. This will prevent to copy any .env file from any folder.

laradock refer to workspace directory path

I have successfully installed Prince via the docker-compose.yml and I see the path + executable I need inside my workspace container but calling it out a number of ways in my Laravel .env will not find that path. In order to use prince I need to tell it where the PRINCE_EXECUTABLE_PATH is via the .env. What is the correct way to hit a directory in the workspace container from the Laravel app? I have tried to set a variable in the Laradock env and refer to it from laradock_workspace_1 and also just put the actual path in the env -- nothing seems to find it. What is the correct way?

Grails 3 plugins configuration

New to Grails 3- starting to port.
Have tried placing config values in application.groovy and application.yml within plugin conf dir to no avail - trying to read values from within plugin service fails. Adding values to the main application's application.groovy works.
What is the secret here? Previously I could load specific conf files via config.locations simply by naming them which was nice and simple. I've seen some resolutions that include needing to setup env vars with paths to config files which I'd like to avoid. Then they set up file URIs for dev and classpath URIs for other envs that will be war packaged - don't really want to do any of this.
Do we no longer have the ability to place config within a plugin and have that automatically merged with the applications config?
The plugin may provide config settings in grails-app/conf/plugin.yml.

How can I copy a config file within a Dockerfile?

I'm trying to install and configure nginx among other things, but I need to copy an entire config file. I tried simply adding it to the Dockerfile since it's just a few lines, but Dockerfile doesn't seem to have good support for multiline commands. I mean I need to copy my config file as is, I can't pollute it with 'sed', 'cat', or '\' on every line.
Some people suggested placing the config file in a public git repository, and I guess I can do that if it turns out there is no other way. But I don't like it at all because it doesn't make sense. I don't want to have and manage version control repositories for these files (let alone make them public), I just want to copy/paste them. They are very simple!
Any ideas?
You should use the ADD instruction in your Dockerfile to copy the config file into the container.

Resources