Push from the same file with different parameter in Git

1

In Laravel there is the file .env .

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

Let's suppose I have 3 connections:

  • Localhost
  • Homologation
  • Production

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

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

When I want to go up in production I change the value to production and I make push again.

All this for connection to file database.php . It was the best solution I found.

'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')),
],

Can you automate this via git or otherwise?

NOTE: I created 2 repositories in GitHub, one of Homologation and one of Production to upload the project.

    
asked by anonymous 31.01.2017 / 12:31

1 answer

1

Laravel uses the DotEnv library to set up the environment.

It is recommended to have a file named .env with the connection data of your machine.

This file should not be versioned , as each machine will have its own.

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

For example this Laravel file would be on 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 is on the server your file .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 that's it!

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

In this way the only file that changes from environment to environment are your .env files. 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 should not be used in production. In production, the current 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 .env file on production servers.

The solution is to edit your ~/.bashrc file and set your variables there, so DotEnv does nothing and your application has all the services settings in a safe place without disturbing your deploy process.

    
31.01.2017 / 13:06