Skip to content

Commit

Permalink
Refactor .env file setup
Browse files Browse the repository at this point in the history
Volume-mounting individual files is unreliable due to limitations
with the way Docker handles inode changes. If editing a file creates
a new inode, then those changes will unexpectedly not be propagated
into the container.

See moby/moby#15793 (comment).

To fix this, we will avoid volume-mounting files. We move the `.env`
file to `config/env`, so that we can reuse the `config` directory
volume-mount. We also create a symlink to `.env` for the convenience
of docker-compose commands (since it reads `.env` by default).
  • Loading branch information
hemberger committed Feb 15, 2021
1 parent 487ccf0 commit ff13d72
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 34 deletions.
1 change: 1 addition & 0 deletions .env
4 changes: 1 addition & 3 deletions .gitignore
Expand Up @@ -10,10 +10,8 @@
# /.settings/
/.settings/

# Docker-compose environment variable file
/.env

# /config/
/config/env
/config/config.specific.php
/config/discord/config.specific.php
/config/irc/config.specific.php
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -17,7 +17,7 @@ To run unit tests on your machine:
First, you will need to clone this repository. Then inside the clone, you
will need to create installation-specific copies of the following files:

* `.env.sample` → `.env`
* `config/env.sample` → `config/env`
* `config/config.specific.sample.php` → `config/config.specific.php`

For "Caretaker" (IRC) or "Autopilot" (Discord) functionality:
Expand Down Expand Up @@ -200,7 +200,7 @@ This information applies to IDEA-based IDEs, e.g. `IntelliJ`, `PHPStorm`. For ot
1. Press the "+" button in the top left, and select `From Docker, Vagrant, VM, WSL, Remote...`
1. In the new window, choose `Docker Compose`, and in the `Service` area, select `smr-integration-test`. Press "OK".
1. It will check the configuration by starting up the Docker container, and gathering PHP information. Once that's finished, you should be on a configuration screen for the new interpreter. It should have the PHP information from the Docker container, and also the `Xdebug` information.
1. In the `Environment variables` box on the new intepreter's screen, paste in the values from `/test.env` in the project directory.
1. In the `Environment variables` box on the new intepreter's screen, paste in the values from `/test/env` in the project directory.
1. The rest of the default settings should be fine, so you can press "Apply".
1. On the settings navigation tree, underneath PHP, click the `Composer` item: In the `CLI Interpreter` drop down, select the new interpreter you've created. Press "Apply".
1. On the settings navigation tree, underneath PHP, click the `Test Frameworks` item.
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -29,15 +29,15 @@
"docker-compose up --build -d smr-dev"
],
"start:integration-services": [
"docker-compose --env-file test.env up -d mysql-integration-test",
"docker-compose --env-file test/env up -d mysql-integration-test",
"@rebuild:integration-services"
],
"rebuild:integration-services": [
"docker-compose --env-file test.env build smr-integration-test",
"docker-compose --env-file test.env run --rm flyway-integration-test"
"docker-compose --env-file test/env build smr-integration-test",
"docker-compose --env-file test/env run --rm flyway-integration-test"
],
"test": [
"docker-compose --env-file test.env run --rm smr-integration-test"
"docker-compose --env-file test/env run --rm smr-integration-test"
],
"stop": [
"docker-compose stop",
Expand Down
File renamed without changes.
11 changes: 1 addition & 10 deletions docker-compose.yml
@@ -1,9 +1,3 @@
# You must include a `.env` file with the following content:
# MYSQL_ROOT_PASSWORD=chooseapassword
# MYSQL_PASSWORD=chooseapassword
# MYSQL_HOST=chooseahost
# PHP_DEBUG=1 or 0

version: '3.7'

networks:
Expand Down Expand Up @@ -33,7 +27,6 @@ x-smr-common: &smr-common
volumes:
- ./vol_upload:/smr/src/htdocs/upload
- ./config:/smr/config:ro
- ./.env:/smr/.env:ro

# Base configuration for the SMR command line tools
x-smr-cli: &smr-cli
Expand All @@ -48,7 +41,6 @@ x-smr-cli: &smr-cli
- smtp
volumes:
- ./config:/smr/config:ro
- ./.env:/smr/.env:ro

x-mysql-common: &mysql-common
image: mysql:8.0
Expand Down Expand Up @@ -78,7 +70,6 @@ services:
volumes:
- ./vol_upload:/smr/src/htdocs/upload
- ./config:/smr/config:ro
- ./.env:/smr/.env:ro
# Mount the source code instead of copying it.
- ./src:/smr/src
environment:
Expand Down Expand Up @@ -125,7 +116,7 @@ services:
- XDEBUG_MODE=coverage
volumes:
- ./config/config.specific.sample.php:/smr/config/config.specific.php:ro
- ./test.env:/smr/.env:ro
- ./test/env:/smr/config/env:ro
- ./phpunit.xml:/smr/phpunit.xml
# Mount the source code instead of copying it.
- ./src:/smr/src
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Smr/Container/DiContainer.php
Expand Up @@ -40,7 +40,7 @@ private function getDefinitions(): array {
return MySqlDatabase::mysqliFactory($mysqlProperties);
},
Dotenv::class => function(): Dotenv {
return Dotenv::createArrayBacked(ROOT);
return Dotenv::createArrayBacked(CONFIG, 'env');
},
// Explicitly name all classes that are autowired, so we can take advantage of
// the compiled container feature for a performance boost
Expand Down
14 changes: 0 additions & 14 deletions test.env

This file was deleted.

9 changes: 9 additions & 0 deletions test/env
@@ -0,0 +1,9 @@
# docker-compose environment file to be used with phpunit

MYSQL_ROOT_PASSWORD=smr
MYSQL_USER=smr
MYSQL_PASSWORD=smr
MYSQL_HOST=smr-mysql-test
MYSQL_DATABASE=smr_live
SMR_HOST=smr-game-test
PHP_DEBUG=1

0 comments on commit ff13d72

Please sign in to comment.