Push of the same file with different parameter in Git

Asked

Viewed 229 times

1

In Laravel there is the file .env.

In it I define my database connections, email, among other things.

Suppose I have three connections:

  • Localhost
  • Homologation
  • Production

When I want to work on localhost I change the variable APP_ENV. I leave the value of this as local.

When I want to upload the files in the approval repository I leave the value of this variable as homo, and do git push origin master.

When I want to move up in production I change the value to production and do the push again.

All this for file connection database.php. It was the best solution I could find.

'mysql' => [
      'driver'    => 'mysql',
      'host'      => (env('APP_ENV') == 'local') ? env('DB_HOST_LOCAL')      : ((env('APP_ENV') == 'homo') ? env('DB_HOST_HOMO')     : env('DB_HOST')),
      'database'  => (env('APP_ENV') == 'local') ? env('DB_DATABASE_LOCAL')  : ((env('APP_ENV') == 'homo') ? env('DB_DATABASE_HOMO') : env('DB_DATABASE')),
      'username'  => (env('APP_ENV') == 'local') ? env('DB_USERNAME_LOCAL')  : ((env('APP_ENV') == 'homo') ? env('DB_USERNAME_HOMO') : env('DB_USERNAME')),
      'password'  => (env('APP_ENV') == 'local') ? env('DB_PASSWORD_LOCAL')  : ((env('APP_ENV') == 'homo') ? env('DB_PASSWORD_HOMO') : env('DB_PASSWORD')),
],

There’s a way I can automate this git or otherwise ?

NOTE: I created 2 repositories in Github, one of Homologation and one of Production to go up the project.

1 answer

1


Laravel uses the library Dotenv to configure the environment.

The recommended is to have a file called .env with your machine connection data.

That file should not be versioned, because each machine will have its own.

All variables defined in this file will be assigned to the environment variables ($_ENV) PHP by Dotenv automatically.

For example this file from Laravel would be in your local machine:

APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

...

Your config would be:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST'),
    'port'      => env('DB_PORT'),
    'database'  => env('DB_DATABASE'),
    'username'  => env('DB_USERNAME'),
    'password'  => env('DB_PASSWORD'),
],

And if it’s on your file server .env would be something like:

APP_ENV=producao
APP_KEY=
APP_DEBUG=false
APP_LOG_LEVEL=error
APP_URL=http://meusite.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_producao
DB_USERNAME=usuario_producao
DB_PASSWORD=secret_producao

...

And just like that!

It is not necessary to change your configuration files, because thanks to Dotenv all your settings are loaded into the environment variables.

That way the only file that changes from environment to environment are your files .env. Just go to your server and create the file with the correct settings.


EDIT: The author of Dotenv:

phpdotenv is made for Development Environments, and generally should not be used in Production. In Production, the actual Environment variables should be set so that there is no overhead of loading the . env file on each request

That is, it is neither necessary nor advisable to have a file .env on production servers.

The solution is to edit your file ~/.bashrc and set your variables there, so Dotenv does nothing and your application has all service settings in a safe place without disrupting your deployment process.

  • You spoke what I already knew. However, when I give push it replaces in the repository, whatever it is. So I did what I did.

  • Is your repository also the site running the system? If that’s the case you would have to use git_hooks to keep the config file in the same place, or else put your file .env somewhere else and make Laravel carry from there

  • I edited the answer

  • Now yes. Yes, my site is running with the repository files. I do deploy. I’m gonna check it out from bash_profile and see what I can do. I can’t use git_hooks.

  • Here on my server do not have this file. Do you have to create ? And how would Laravel get its variables ? This file is created within the project ?

  • I never used git_hooks also... but this solution of bash_profile is a good because it can be used in other projects too

  • your server is Unix?

  • It’s a Linux, Ubuntu. Here’s a .bashrc

  • That, is the .bashrc.

  • Just have to see if this is the file that will be uploaded by the server user...

  • So, that’s my question. How to make in the Laravel files use the .bashrc instead of .env. There are several files, I may not even know what they are. I only know the database.php

  • The environment variables are from the operating system, not from Laravel. Dotenv arrow your variables to whole OS. You could enter the terminal and type export APP_ENV="producao" that the entire process initiated at that terminal will have access to that information

  • http://askubuntu.com/a/58828 you can edit the /etc/environment

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.