Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

markuskoehler's avatar

.env for Unit Testing

Might be a very dumb question, but I cannot see any solutions as of now.

I configured a Jenkins server to checkout my Laravel5 project from Git repo. I see by default there is .env excluded in .gitignore, but .env.example will be under source control...So of course after Jenkins check the project out, there is no .env file and PhpUnit execution fails, because there is also no encryption key etc... So what is the best method to automatically get a valid .env into the Jenkins workspace?

0 likes
3 replies
ifpingram's avatar

@markuskoehler If it is <5.2 see this post by Matt Stauffer. If it is 5.2 as per the docs:

"If the APP_ENV environment variable is set before bootstrapping the application, Laravel will attempt to load a file that matches the environment. For example, if the PHPUnit configuration file sets the APP_ENV variable to testing, Laravel will attempt to load an .env.testing environment file. If the file does not exist, the default .env file is loaded."

Then you just need to commit the testing env file with your project. Make sure that if you are doing any tests with any sort of production collaborator (an external service etc.), that you either use very locked down / rate limited accounts, or pass these variables via Jenkins config itself.

Good luck!

3 likes
markuskoehler's avatar

But isn't it bad practice to commit .env files, even if only for test env, because of confidential data in the .env file, like username/password? How can one assure that confidential connection data don't get abused when under source control?

ifpingram's avatar

@markuskoehler correct, and I did address that already ;)

"that you either use very locked down / rate limited accounts, or pass these variables via Jenkins config itself."

The latter suggestion of storing the environment variables in Jenkins config, will mean you do not need to commit them to source control.

1 like

Please or to participate in this conversation.