How to avoid git sync of prisma .env files - graphql

When I set up prisma in my project, by following this article, I can set up .env and set database url and password.
After that,DB connection was established and some tables were migrated. But I am wondering that Are there any problem to add .env to .gitignore ?
password is hardcoded and this credentials are not pushed.
If someone has experienced this problems or know other options, will you please let me know.

It is strongly recommended that the env files are added to .gitignore.
Executing npx prisma init should create a .gitignore file and a .env file along with creating prisma folder. The default behaviour is that the env file gets added to the .gitignore file so that the credentials are not accidentally pushed to source control.
Here is a reference for using multiple env variables or using prisma without any env variables.
There should be no issues on adding .env to .gitignore file.

Related

How to rename .env variables in package.json?

What I have
I have multiple projects using Percy for Cypress where I set the PERCY_TOKEN env variable inside the .env file. The token is different for each project. In the CI I set different env variables for each project, but locally I have to do it in the .env file. Because of this, I have to edit the .env file whenever I change between projects.
Goal
I would like to set them in the .env file this way:
PROJECT_A_PERCY_TOKEN=tokenhash1
PROJECT_B_PERCY_TOKEN=tokenhash2
So later I could rename these variables to PERCY_TOKEN, eliminating the need to constantly change the .env file.
What I tried
I'm trying to do this inside the package.json file's scripts property. Unfortunately echo $PROJECT_A_PERCY_TOKEN prints nothing. I know that I could create a shell/python/js script that parses the .env file, then passes the value back or calls npm run directly but I would like to do this without an external script.
Problem
It appears to me that I can't access the env variables inside package.json. Is there a way to rename the variable only using the npm script?
tl;dr
If the package you try to configure has the ability to do configuration via a JavaScript file, you can add the renaming at the beginning of it:
process.env.PERCY_TOKEN = process.env.CYPRESS_PERCY_SALESFORCE_TOKEN;
Explanation
While this isn't the solution I was looking for, it is a workaround for this specific use case. Percy supports JavaScript config files so I migrated my YAML config file, then I logged process.env and the .env file's variables were there, so I just need to copy the correct one. This might work for other packages that support JavaScript config files (or some alternative kind of hook/preloader where custom code can be placed), but if they don't, then the question is still unanswered.

Set environment variables on heroku [duplicate]

This question already has answers here:
How to add .env file or otherwise set environment variables in a Heroku app?
(2 answers)
Closed 1 year ago.
I tried deploying one of my projects on heroku via my github repository. I had a .env file that had my API keys and private tokens. I want to make my repo public but it would mean revealing my .env file to everyone. I tried searching for how to set up .env for heroku but found answers that confused me even more. So is there a way to supply my env variables to herkou while I remove them from my github repository ?
Your .env keys are secret and should remain so. Fear not, you can make your repo public and still keep your keys secret.
Off the back, the .env file is a hidden file. You should ignore this file before pushing it to version control. Add a .gitignore file in your top-level directory and supply all the files you want ignored from version control. You can check here to see what files you may want to ignore.
If you had already committed your keys to github, meaning that there is an available copy of these keys online, i'd recommend that you generate new keys for each variable in your .env file. This would be the easiest way around it. With .env file already listed in .gitignore, then your next commit will omit the file and its content from version control.
How to load .env file
Assuming you have a simple structure as the one shown below where your .env and config.py files are in the top-level directory:
project
| --- .env
| --- config.py
| --- requirements.txt
| --- app/
First, you will need to install python-dotenv in your virtual environment. This package helps with the loading of environment variables.
(venv)$ pip3 install python-dotenv && pip3 freeze > requirements.txt
In your config module, load .env:
# config.py
import os
from dotenv import load_dotenv
load_dotenv('.env')
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY')
Your .env file should have this key:
# .env
SECRET_KEY=<you-will-never-guess>
Set environment variables in Heroku
In your terminal, you can set the values of .env:
(venv)$ heroku config:set SECRET_KEY=<you-will-never-guess>
To help interested users of your application know what keys they would need when they test the app locally, create a text file called .env-template and create your environment variables without passing their actual values:
# .env-template
SECRET_KEY=

add new env file to laravel for new server environment

i'm taking over a project from a client to add functionalities. I quite new in the development of Laravel 5.5
There are already 3 .env files in my project:
.env
.env.example
.env.live
.env.live is for production (APP/ENV=production) and runs on aws, the other 2 are for local use.
Now I have a new AWS EC2 server, database and so on and need a new env file to use these new instances. Let's say I create a new .env file called .env.dev
How how can I switch between the .env.dev and .env.live? Or how do I use this .env.dev?
If you're using Elastic Beanstalk, go to your environment, then click the Configuration menu item. From there click "Modify" in the "Software" section. A page will appear where you can add Environment Properties as key->value pairs.
You can then delete the .env files. These properties will be read by your Laravel app just like it reads the .env files.
You can also use the symbolic links
Just Delete .env from both of the server and create a symbolic link like.
ln -s .env.live .env // for live
ln -s .env.dev .env // for dev server
You can also keep this command inside your deployment script if you are using any CI/CD.
On every server you only need .env file to execute. If you have three files
.env
.env.live
.env.example
.env.other
you can create multiple files with .env.*. But when you will execute your application then your .env file will execute.
Now if you want to add new additional settings to your environment then you just need to add in .env file. and also specify all other .env.* files and change values according to server.
When you will deploy your application on live server. Then you just need to rename your .env.live file to .env and set configurations to that file.
cp .env.live .env
Note: if you already have .env file on your server(as you said your application is already on production server.) then you just need to add your environment settings to that file.

where the .env file goes on the web server in laravel 5.6

I am confused about .env file as when I hosted the laravel application the .env file is missing on the web server. If anybody knows the way how can we find .env file on the server please answer.
Try ls -la to list all files in your web root.
As other have said, it sounds like the file exists but you cannot see it because it is a hidden file (denoted by the . in .env)
For future reference, whilst .env is likely to be excluded in a .gitignore there is often a .env.example which is include that you can copy and modify for your environment.
By default a Laravel app's Git repo will ignore the .env file - it's because you will use this on your development machine to configure the app to work on that environment.
You should have another .env file on your server which provides the configuration required for your app to run in that environment. This file should be maintained between deployments - and most importantly, the credentials inside of it should not be tracked in a repo.
Your .env file is there! It is a hidden file and you can't see it with gui unless if you somehow reveal hidden files! I recommend you to use putty over windows or ssh over linux terminal and edit it if you know how to do that.
By default some files like .env, .htaccess are hidden on the server therefore to see these files we need to set as show hidden files on the server settings.

Laravel - .env vs database.php

I am confused by Laravels .env file VS other settings
I have a .env file which has my sql database settings inside, but I also have a file in config/database.php that has these settings inside.
When I deploy my application to an Elastic Beanstalk instance, which of these files is it using for the database settings?
.env is short for environment and thus that is your environment configurations.
The database.php configuration contains non-critical information.
You obviously won't have your database's username's password in your source control or available in the code.
In order to keep everything safe or to keep information saved that is environment-defined... you keep them in .env file
Laravel will prioritize .env variables.
A little more detailed answer:
The config Files are where you store all configurations. You shouldn't add any username, passwords or other secret informations in them, because they will be in your source control.
All secret informations and all environment dependant informations should be stored in your .env file. With this you can have different configuration values in local/testing/production with just a different .env file.
In your config files you access the information in you .env files, if necessary.
When use what from another answer
use env() only in config files
use App::environment() for checking the environment (APP_ENV in .env).
use config('app.var') for all other env variables, ex. config('app.debug')
create own config files for your own ENV variables. Example:
In your .env:
MY_VALUE=foo
example config app/myconfig.php
return [
'myvalue' => env('MY_VALUE', 'bar'), // 'bar' is default if MY_VALUE is missing in .env
];
Access in your code:
config('myconfig.myvalue') // will result in 'foo'
In your ".env" file you have your settings. in the ".php" files like your "database.php" file this is the default value for the property and normally, the corresponding value in the ".env" file is use here with this syntax : 'database' => env('database', 'default_value'),
The .env file is the first file it will use for configs. In case values are missing inside the .env file Laravel will check the config files. I primairly use those as backups.

Resources