Skip to content

Commit

Permalink
Merge pull request #2409 from skaut/2396-novy-docker
Browse files Browse the repository at this point in the history
2396-novy-docker
  • Loading branch information
juradee authored Nov 15, 2023
2 parents 9962bf0 + f9b0acc commit d9a7f29
Show file tree
Hide file tree
Showing 53 changed files with 1,165 additions and 371 deletions.
21 changes: 8 additions & 13 deletions .docs/instalace.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ Aplikace vyžaduje:
- Composer
- Yarn

Všechny potřebné nástroje jsou instalovány v příslušných kontejnerech
Na hostujícím stroji musí být ve výchozím stavu volné porty 80 a 3306. Port 3306 je možné použít pro propojená s IDE

## Docker
Pro lokální vývoj je připraven Docker container a konfigurace pro **docker-compose**.
Pro lokální vývoj je připraven Docker container a konfigurace pro **docker compose**.
Všechny potřebné příkazy jsou definované v Make file

```bash
docker volume create --name hskauting_mysql
docker compose up -d # Spustí container v detached modu
make up # Spustí container v detached modu
```

V kontejneru je možné spustit bash pomocným skriptem:
```bash
docker/ssh
make enter
```

## Nastavení hosts
Expand All @@ -30,12 +33,4 @@ Stačí přidat tento řádek do souboru `/etc/hosts`:
```

## Příprava projektu
V kontejneru stačí spustit příkaz `phing init`.

**Poznámka**: Při commitování se automaticky opravuje coding standard v PHP - to však vyžaduje lokálně nainstalované PHP.
Pokud nemáte mimo kontejner instalované PHP alespoň ve verzi jako používá Hospodaření,
je možné automatickou opravu coding standardu vypnout nastavením proměnné `HUSKY_SKIP_INSTALL` na `true` při instalaci
`yarn` závislostí. Tedy např.:

- `export HUSKY_SKIP_INSTALL=true; phing init`
- `HUSKY_SKIP_INSTALL=true yarn install`
Stačí spustit příkaz `make init`.
19 changes: 17 additions & 2 deletions .docs/nastroje.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,37 @@ Pro testování používáme [Codeception](http://codeception.com/).

Testy lze spustit příkazem v kontejneru:
```bash
phing tests # Jednotkové + Integrační testy + Akceptační testy
phing tests-unit # Pouze jednotkové testy
phing tests-integration # Pouze integrační testy
phing tests # Jednotkové + Integrační testy
phing tests-acceptance # Akceptační testy
```

nebo pomocí make přímo na hostujícím stroji:
```bash
make tests-all # Jednotkové + Integrační testy + Akceptační testy
make tests-unit # Pouze jednotkové testy
make tests-integration # Pouze integrační testy
make tests-acceptance # Akceptační testy
```


## Coding standard
Coding standard vychází z [Doctrine Coding Standardu](https://github.com/doctrine/coding-standard).
Zda je projekt v souladu s CS lze ověřit pomocí příkazu v kontejneru:

```bash
phing coding-standard
phing coding-standard-ci
```

Automaticky lze nechat opravit pomocí:

```bash
./vendor/bin/phpcbf app
```

Nebo opět přímo z hostujícího stroje pomocí make:

```bash
make coding-standard
```
11 changes: 10 additions & 1 deletion .docs/tipy-pro-testovani.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ Jednotlivé testy:
vendor/bin/codecept run <cesta k souboru>
```

Všechny daného typu:
Všechny daného typu lze spustit v kontejneru:
```bash
phing tests
phing tests-acceptance
phing tests-integration
phing tests-unit
phing tests-with-coverage
```

Pomocí make na hostitelském stroji:
```bash
make tests-all
make tests-acceptance
make tests-integration
make tests-unit
make tests-with-coverage
```
5 changes: 5 additions & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_HOST=mysql
DB_USER=hskauting
DB_PASSWORD=hskauting
DB_NAME=hskauting
DEVELOPMENT_MACHINE=true
5 changes: 5 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_HOST=mysql-test
DB_USER=hskauting
DB_PASSWORD=hskauting
DB_NAME=hskauting
DB_TEST=true
52 changes: 17 additions & 35 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ jobs:
restore-keys: |
${{ runner.os }}-composer-
- run: composer install
- name: Remove development docker-compose override
run: rm docker-compose.override.yml
- name: Fix permissions
run: chown 1000:1000 . -R && chmod 777 . -R
- name: Create tarball
Expand Down Expand Up @@ -59,10 +57,6 @@ jobs:
runs-on: ubuntu-22.04
needs: workdir
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- name: Download workdir
uses: actions/download-artifact@v1
with:
Expand All @@ -71,8 +65,7 @@ jobs:
- name: Extract workdir
run: tar -xzpf workdir.tar.gz
- name: Start application containers
run: docker-compose up -d mysql-test
- run: docker-compose run -T -u www-data app phing tests-integration
run: docker compose -f docker/docker-compose.yml run -T php-test phing tests-integration

collect-code-coverage:
name: "Collect code coverage"
Expand All @@ -87,10 +80,8 @@ jobs:
path: .
- name: Extract workdir
run: tar -xzpf workdir.tar.gz
- name: Start application containers
run: docker-compose up -d app mysql-test
- name: Run tests with coverage
run: docker-compose exec -T -u www-data app phing tests-with-coverage
run: docker compose -f docker/docker-compose.yml run -T php-test phing tests-with-coverage
- name: Upload code coverage
uses: codecov/codecov-action@v3
with:
Expand All @@ -110,30 +101,15 @@ jobs:
path: .
- name: Extract workdir
run: tar -xzpf workdir.tar.gz
- name: Start application containers
run: docker-compose up -d
- name: Append to .env file
run: echo -e "\nHEADLESS='--headless'" >> .env.test
- name: Init app
run: docker compose -f docker/docker-compose.yml run -T php-test phing app-init
- name: Start selenium container
run: docker compose -f docker/docker-compose.yml up -d selenium nginx
- run: mv app/config/config.ci.local.neon app/config/config.local.neon
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
# Copy & paste from https://github.com/actions/cache/blob/master/examples.md#node---yarn
- name: Get yarn cache
id: yarn-cache
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: npm install -g yarn
- name: Build frontend assets
run: yarn install && yarn build
- name: Prepare database
run: docker-compose exec -T -u www-data app bin/console migrations:migrate --no-interaction
- run: docker-compose exec -T -u www-data app phing tests-acceptance
- name: Run acceptance tests
run: docker compose -f docker/docker-compose.yml run -T php-test phing tests-acceptance
- uses: actions/upload-artifact@v3
if: failure()
with:
Expand All @@ -143,6 +119,12 @@ jobs:
static-analysis:
name: "Run PHPStan analysis"
runs-on: ubuntu-22.04
env:
DB_HOST: mysql-test
DB_USER: hskauting
DB_PASSWORD: hskauting
DB_NAME: hskauting
DB_TEST: true
container:
image: skaut/lebeda:8.1
needs: workdir
Expand Down Expand Up @@ -172,7 +154,7 @@ jobs:
path: .
- name: Extract workdir
run: tar -xzpf workdir.tar.gz
- run: phing coding-standard
- run: phing coding-standard-ci

latte-lint:
name: "Lint Latte templates"
Expand Down
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CONTAINER_PHP=hskauting.app
CONTAINER_PHP_TEST=hskauting.app-test
CONTAINER_DB=hskauting.mysql
CONTAINER_DB_TEST=hskauting.mysql-test

COMPOSE_FILE=-f docker/docker-compose.yml

CONSOLE?=

up: down
docker compose ${COMPOSE_FILE} up -d --force-recreate

down:
docker compose ${COMPOSE_FILE} down --remove-orphans

enter:
@docker exec -it ${CONTAINER_PHP} bash

init:
docker exec -it $(CONTAINER_PHP) composer install
docker exec -it $(CONTAINER_PHP) /app/vendor/bin/phing app-init

tests-all:
docker exec -it ${CONTAINER_PHP_TEST} /app/vendor/bin/phing tests

tests-unit:
docker exec -it ${CONTAINER_PHP_TEST} /app/vendor/bin/phing tests-unit

tests-integration:
docker exec -it ${CONTAINER_PHP_TEST} /app/vendor/bin/phing tests-integration

tests-acceptance:
docker exec -it ${CONTAINER_PHP_TEST} /app/vendor/bin/phing tests-acceptance

static-analysis:
docker exec -it ${CONTAINER_PHP} /app/vendor/bin/phing static-analysis

coding-standard:
docker exec -it ${CONTAINER_PHP} /app/vendor/bin/phing coding-standard
2 changes: 2 additions & 0 deletions app/AccountancyModule/Components/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
abstract class BaseControl extends Control
{
public const MAX_FILE_SIZE_VALUE = 15 * 1024 * 1024;

abstract public function render(): void;

public function getPresenter(): BasePresenter|null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function createComponentUploadForm(): BaseForm
BaseForm::MIME_TYPE,
'Neplatný formát skenu, povolené formáty jsou ' . implode(', ', array_keys(IScanStorage::ALLOWED_MIME_TYPES)) . '.',
IScanStorage::ALLOWED_MIME_TYPES,
)->addRule(BaseForm::MAX_FILE_SIZE, 'Maximální povolená velikost souboru je 15 MB', 15 * 1024 * 1024);
)->addRule(BaseForm::MAX_FILE_SIZE, 'Maximální povolená velikost souboru je 15 MB', BaseControl::MAX_FILE_SIZE_VALUE);

$form->addSubmit('submit', 'Nahrát');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function createComponentUploadForm(): BaseForm
BaseForm::MIME_TYPE,
'Neplatný formát skenu, povolené formáty jsou ' . implode(', ', array_keys(IScanStorage::ALLOWED_MIME_TYPES)) . '.',
IScanStorage::ALLOWED_MIME_TYPES,
)->addRule(BaseForm::MAX_FILE_SIZE, 'Maximální povolená velikost souboru je 15 MB', 15 * 1024 * 1024);
)->addRule(BaseForm::MAX_FILE_SIZE, 'Maximální povolená velikost souboru je 15 MB', BaseControl::MAX_FILE_SIZE_VALUE);

$form->addSubmit('submit', 'Ok');

Expand Down
64 changes: 64 additions & 0 deletions app/Console/MigrationsDropCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Console;

use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function getenv;
use function sprintf;

class MigrationsDropCommand extends Command
{
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
/** @var string|null $defaultName The default command name */
protected static $defaultName = 'migrations:drop-all-tables-views';

public function __construct(private EntityManagerInterface $em)
{
parent::__construct();
}

protected function configure(): void
{
$this->setName(self::$defaultName)
->setDescription('Drops all tables from the database');
}

/** @throws Exception */
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (getenv('DB_TEST') !== 'true') {
$output->writeln('Cannot run on non testing environment');

return Command::FAILURE;
}

$conn = $this->em->getConnection();
$schemaManager = $conn->getSchemaManager();

// Drop tables
$tables = $schemaManager->listTables();
$conn->executeStatement('SET foreign_key_checks = 0');
foreach ($tables as $table) {
$tableName = $table->getName();
$conn->executeStatement(sprintf('DROP TABLE %s', $tableName));
$output->writeln(sprintf('Dropped table %s', $tableName));
}

// Drop views
$views = $schemaManager->listViews();
foreach ($views as $view) {
$viewName = $view->getName();
$conn->executeStatement(sprintf('DROP VIEW %s', $viewName));
$output->writeln(sprintf('Dropped view %s', $viewName));
}

return Command::SUCCESS;
}
}
9 changes: 7 additions & 2 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Nette\Bootstrap\Configurator;

require __DIR__ . '/../vendor/autoload.php';

setlocale(LC_COLLATE, 'cs_CZ.utf8');
Expand All @@ -11,16 +13,19 @@

putenv('TMPDIR=' . $tempDir);

$configurator = new Nette\Configurator();
$env = getenv();

$configurator = new Configurator();
$configurator->setDebugMode(getenv('DEVELOPMENT_MACHINE') === 'true' ?: '89.177.225.213');
$configurator->enableDebugger($logDir);
$configurator->enableTracy($logDir);
$configurator->setTempDirectory($tempDir);

$configurator->createRobotLoader()
->addDirectory(__DIR__)
->register(true);

$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addDynamicParameters(['env' => $env]);
$configurator->addConfig(__DIR__ . '/config/config.local.neon');

$configurator->addParameters(['logDir' => $logDir]);
Expand Down
3 changes: 3 additions & 0 deletions app/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extensions:
skautis: Skautis\Nette\SkautisExtension
messenger: Fmasa\Messenger\DI\MessengerExtension
console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
console.cache: Contributte\Console\Extra\DI\CacheConsoleExtension(%consoleMode%)

search:
command handlers:
Expand Down Expand Up @@ -110,6 +111,8 @@ services:
- Model\Common\Services\MessengerCommandBus(@messenger.commandBus.bus)
- Model\Common\Services\MessengerQueryBus(@messenger.queryBus.bus)
- Model\Common\Services\MessengerEventBus(@messenger.eventBus.bus)
- Console\MigrationsDropCommand


skautisCache:
factory: Nette\Caching\Cache(namespace: 'skautis')
Expand Down
Loading

0 comments on commit d9a7f29

Please sign in to comment.