Skip to content

Commit

Permalink
chore: availability to chose php version (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Dec 12, 2022
1 parent e4d5fcb commit b8facdc
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 107 deletions.
57 changes: 30 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,40 +211,43 @@ jobs:
- name: Run static analysis
run: bin/tools/phpstan/vendor/phpstan/phpstan/phpstan analyse

docker-stack:
test-docker-stack:
name: CI with docker stack
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2']
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Create cache directory
run: mkdir -p cache-docker
- name: Run test suite with docker
run: |
echo "PHP_VERSION=${{ matrix.php }}" > .env
make validate
- id: cache-docker
uses: actions/cache@v3
with:
path: cache-docker
key: ${{ hashFiles('docker/*') }}
push_docker_images:
name: Push docker images after CI complete on main branch
needs: [tests, composer-validate, build-docs, cs-check, static-analysis, test-docker-stack]
runs-on: ubuntu-latest
# if: ${{ github.event_name == 'push' && github.event.ref == 'refs/heads/1.x' }}
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2']
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Docker build
uses: docker/build-push-action@v3
if: steps.cache-docker.outputs.cache-hit != 'true'
- name: Login to ghcr.io
uses: docker/login-action@v2
with:
context: .
file: ./docker/Dockerfile
outputs: type=docker,dest=cache-docker/php.tar
tags: |
foundry_php:ci
push: false

- name: Load docker context
run: |
docker load < cache-docker/php.tar
registry: ghcr.io/zenstruck
username: token
password: ${{ secrets.PACKAGE_PUSH_TOKEN }}

- name: Run test suite with docker
run: |
make validate
- name: Build and push images
run: ./docker/build.sh push ${{ matrix.php }}
env:
GITHUB_TOKEN: ${{ secrets.PACKAGE_PUSH_TOKEN }}
91 changes: 43 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: $(filter-out vendor bin/tools/phpstan/vendor bin/tools/cs-fixer/vendor,$(MAKECMDGOALS))
.PHONY: $(filter-out vendor bin/tools/phpstan/vendor bin/tools/cs-fixer/vendor %,$(MAKECMDGOALS))

.DEFAULT_GOAL := help

Expand All @@ -11,7 +11,8 @@ USE_FOUNDRY_BUNDLE=1
USE_ORM=1
USE_ODM=1
USE_DAMA_DOCTRINE_TEST_BUNDLE=1
SYMFONY_REQUIRE=6.0.*
SYMFONY_REQUIRE=5.4.*
PHP_VERSION=8.0

# Override test context variables with `.env` file
ifneq (,$(wildcard .env))
Expand All @@ -27,99 +28,93 @@ endif

# Define docker executable
ifeq ($(shell docker --help | grep "compose"),)
DOCKER_COMPOSE_BIN=docker-compose
DOCKER_COMPOSE=COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose
else
DOCKER_COMPOSE_BIN=docker compose
DOCKER_COMPOSE=COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose
endif

# Create special context for CI
INTERACTIVE:=$(shell [ -t 0 ] && echo 1)
ifdef INTERACTIVE
CI='false'
DOCKER_COMPOSE=$(DOCKER_COMPOSE_BIN)
DC_EXEC=$(DOCKER_COMPOSE) exec -e USE_FOUNDRY_BUNDLE=${USE_FOUNDRY_BUNDLE} -e DATABASE_URL=${MYSQL_URL} -e MONGO_URL=${MONGO_URL}
else
CI='true'
DOCKER_COMPOSE=$(DOCKER_COMPOSE_BIN) -f docker-compose.yaml -f docker-compose.ci.yaml
# CI needs to be ran in no-tty mode
DC_EXEC=$(DOCKER_COMPOSE) exec -e USE_FOUNDRY_BUNDLE=${USE_FOUNDRY_BUNDLE} -e DATABASE_URL=${MYSQL_URL} -e MONGO_URL=${MONGO_URL} -T
endif

DOCKER_PHP=${DC_EXEC} php
PHP=php${PHP_VERSION}
DOCKER_PHP=${DC_EXEC} ${PHP}

# From inside the containers, docker host ip is different in linux and macos
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
XDEBUG_HOST=172.17.0.1
endif
ifeq ($(UNAME_S),Darwin)
XDEBUG_HOST=host.docker.internal
endif
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)

help:
@fgrep -h "###" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

validate: fixcs sca test database-validate-mapping ### Run fixcs, sca, full test suite and validate migrations

test: docker-start vendor ### Run PHPUnit tests suite
@$(eval filter ?= '.')
@${DC_EXEC} -e USE_ORM=${USE_ORM} -e USE_ODM=${USE_ODM} php vendor/bin/simple-phpunit --configuration ${PHPUNIT_CONFIG_FILE} --filter=$(filter)
test: vendor ### Run PHPUnit tests suite
@${DC_EXEC} -e USE_ORM=${USE_ORM} -e USE_ODM=${USE_ODM} ${PHP} vendor/bin/simple-phpunit --configuration ${PHPUNIT_CONFIG_FILE} $(ARGS)

fixcs: docker-start bin/tools/cs-fixer/vendor ### Run PHP-CS-Fixer
@${DOCKER_PHP} bin/tools/cs-fixer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer --no-interaction --diff -v fix
fixcs: bin/tools/cs-fixer/vendor ### Run PHP-CS-Fixer
@${DC_EXEC} -e PHP_CS_FIXER_IGNORE_ENV=1 ${PHP} php -d 'xdebug.mode=off' bin/tools/cs-fixer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer --no-interaction --diff -v fix

bin/tools/cs-fixer/vendor: vendor bin/tools/cs-fixer/composer.json bin/tools/cs-fixer/composer.lock
@${DOCKER_PHP} composer bin cs-fixer install
bin/tools/cs-fixer/vendor: bin/tools/cs-fixer/composer.json bin/tools/cs-fixer/composer.lock
@$(MAKE) --no-print-directory docker-start
@$(MAKE) --no-print-directory vendor
@$(MAKE) --no-print-directory composer bin cs-fixer install

sca: docker-start bin/tools/phpstan/vendor ### Run static analysis
@${DOCKER_PHP} bin/tools/phpstan/vendor/phpstan/phpstan/phpstan analyse
sca: bin/tools/phpstan/vendor ### Run static analysis
@${DOCKER_PHP} php -d 'xdebug.mode=off' bin/tools/phpstan/vendor/phpstan/phpstan/phpstan analyse

bin/tools/phpstan/vendor: vendor bin/tools/phpstan/composer.json $(wildcard bin/tools/phpstan/composer.lock)
@${DOCKER_PHP} composer bin phpstan install
bin/tools/phpstan/vendor: bin/tools/phpstan/composer.json bin/tools/phpstan/composer.lock
@$(MAKE) --no-print-directory docker-start
@$(MAKE) --no-print-directory vendor
@$(MAKE) --no-print-directory composer bin phpstan install

database-generate-migration: docker-start vendor database-drop-schema ### Generate new migration based on mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
database-generate-migration: vendor database-drop-schema ### Generate new migration based on mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
@${DOCKER_PHP} vendor/bin/doctrine-migrations migrations:migrate --no-interaction --allow-no-migration # first, let's load into db existing migrations
@${DOCKER_PHP} vendor/bin/doctrine-migrations migrations:diff --no-interaction
@${DOCKER_PHP} vendor/bin/doctrine-migrations migrations:migrate --no-interaction # load the new migration
@${DOCKER_PHP} bin/doctrine orm:validate-schema

database-validate-mapping: docker-start vendor database-drop-schema ### Validate mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
database-validate-mapping: vendor database-drop-schema ### Validate mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
@${DOCKER_PHP} vendor/bin/doctrine-migrations migrations:migrate --no-interaction --allow-no-migration
@${DOCKER_PHP} bin/doctrine orm:validate-schema

database-drop-schema: docker-start vendor ### Drop database schema
database-drop-schema: vendor ### Drop database schema
@${DOCKER_PHP} bin/doctrine orm:schema-tool:drop --force
@${DOCKER_PHP} vendor/bin/doctrine-migrations migrations:sync-metadata-storage # prevents the next command to fail if migrations table does not exist
@${DOCKER_PHP} bin/doctrine dbal:run-sql "TRUNCATE doctrine_migration_versions" --quiet

vendor: composer.json $(wildcard composer.lock) $(wildcard .env)
@${DC_EXEC} -e SYMFONY_REQUIRE=${SYMFONY_REQUIRE} php composer update --prefer-dist
vendor: composer.json $(wildcard composer.lock) $(wildcard .env)
@$(MAKE) --no-print-directory docker-start
@${DC_EXEC} -e SYMFONY_REQUIRE=${SYMFONY_REQUIRE} ${PHP} php -d 'xdebug.mode=off' /usr/bin/composer update --prefer-dist

UID = $(shell id -u)
DOCKER_IS_UP = $(shell $(DOCKER_COMPOSE) ps | grep "Up (healthy)")
DOCKER_IS_UP = $(shell $(DOCKER_COMPOSE) ps | grep "${PHP}" | grep "Up")

docker-start: ### Build and run containers
ifeq ($(DOCKER_IS_UP),)
ifeq ($(CI),'false')
@$(DOCKER_COMPOSE) build --build-arg UID="${UID}" --build-arg XDEBUG_HOST="${XDEBUG_HOST}"
@$(DOCKER_COMPOSE) up --detach --remove-orphans
else
@$(DOCKER_COMPOSE) up --detach --no-build
endif
@$(DOCKER_COMPOSE) ps
endif
@if [ -z "${DOCKER_IS_UP}" ]; then \
./docker/build.sh load "${PHP_VERSION}" ; \
$(DOCKER_COMPOSE) up --detach --no-build --remove-orphans mysql mongo "${PHP}" ; \
echo "" ; \
$(DOCKER_COMPOSE) ps ; \
echo "" ; \
${DOCKER_PHP} php -v ; \
echo "" ; \
fi

docker-stop: ### Stop containers
@$(DOCKER_COMPOSE) stop

docker-purge: docker-stop ### Purge containers
@$(DOCKER_COMPOSE) down --volumes

ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)

composer: ### Run composer command
@${DC_EXEC} php composer $(ARGS)
@${DC_EXEC} ${PHP} php -d 'xdebug.mode=off' /usr/bin/composer $(ARGS)

clear: ### Start from a fresh install (needed if vendors have already been installed with another php version)
rm -rf composer.lock bin/tools/phpstan/vendor/ bin/tools/cs-fixer/vendor/ vendor/

%: # black hole to prevent extra args warning
@.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ composer Run composer command
clear Start from a fresh install (needed if vendors have already been installed with another php version)
```
You can run each `make test` target with a special argument `filter`:
Use double-dash to pass any PHPUnit options or arguments with `make`:
```shell
$ make test filter=FactoryTest
$ make test -- --filter=FactoryTest
$ make test -- --stop-on-failure
```
which will use PHPUnit's `--filter` option.
Same syntax is available for composer:
```shell
$ make composer -- info symfony/*
```
#### Run tests in different environments
Expand All @@ -72,7 +76,8 @@ You can create a `.env` file to change the context in which tests will execute:
USE_ORM=1
USE_ODM=1
USE_DAMA_DOCTRINE_TEST_BUNDLE=1
SYMFONY_REQUIRE=6.0.*
SYMFONY_REQUIRE=5.4.* # allowed values: 5.4.* | 6.0.* | 6.1.* | 6.2.*
PHP_VERSION=8.0 # allowed values: 8.0 | 8.1 | 8.2
```
### Change docker's ports
Expand All @@ -88,7 +93,7 @@ MONGO_PORT=27018
You can execute any command into the php container using docker compose:
```shell
$ docker-compose exec php [you commmand] # or "docker compose" depending on your docker compose version
$ docker-compose exec php [your commmand] # or "docker compose" depending on your compose version
```
### Using xdebug with PhpStorm
Expand Down
5 changes: 0 additions & 5 deletions docker-compose.ci.yaml

This file was deleted.

18 changes: 13 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
version: '3.9'

services:
php:
container_name: foundry_php
build:
context: .
dockerfile: ./docker/Dockerfile
php8.0: &php
container_name: foundry_php8.0
image: ghcr.io/zenstruck/foundry/php:8.0
depends_on:
mysql:
condition: service_healthy
Expand All @@ -17,6 +15,16 @@ services:
environment:
PHP_IDE_CONFIG: "serverName=FOUNDRY"

php8.1:
<<: *php
container_name: foundry_php8.1
image: ghcr.io/zenstruck/foundry/php:8.1

php8.2:
<<: *php
container_name: foundry_php8.2
image: ghcr.io/zenstruck/foundry/php:8.2

mysql:
container_name: foundry_mysql
image: mysql
Expand Down
38 changes: 23 additions & 15 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
FROM php:8.0-cli
ARG PHP_VERSION

COPY --from=composer:2.4 /usr/bin/composer /usr/bin/composer
FROM php:${PHP_VERSION}-cli

COPY docker/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
ARG XDEBUG_HOST
COPY --from=composer:2.4 /usr/bin/composer /usr/bin/composer

RUN apt-get update && apt-get install -y \
RUN set -eux ; \
apt-get update ; \
apt-get install --no-install-recommends -y \
Expand All @@ -26,28 +24,38 @@ RUN set -eux ; \
pkg-config \
libssl-dev \
; \
sed -i "s/%xdebug_host%/${XDEBUG_HOST}/g" /usr/local/etc/php/conf.d/xdebug.ini; \
docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql; \
docker-php-ext-install pdo pdo_mysql pdo_pgsql pgsql; \
pecl install -f mongodb-1.10.0; \
pecl install -f mongodb-1.15; \
pecl install xdebug ; \
docker-php-ext-enable mongodb xdebug ; \
pecl clear-cache ; \
apt-get remove -y $PHPIZE_DEPS zlib1g-dev libcurl4-openssl-dev pkg-config libssl-dev ; \
rm -rf /var/lib/apt/lists/* ; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false ; \
composer global require --no-progress --no-scripts --no-plugins symfony/flex ; \
composer global config --no-plugins allow-plugins.symfony/flex true

LABEL org.opencontainers.image.source="https://github.com/zenstruck/foundry"

# We want these last commands to be at the bottom of the file because they depend on the host
# And we want to benefit of the cache layering as much as possible

COPY docker/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini

# Set user rights
ARG USER=docker

# this may be overridden to match host's machine user
ARG UID=1001
RUN addgroup --system --gid ${UID} ${USER} \
&& adduser --system --disabled-password --uid ${UID} --ingroup ${USER} ${USER} \
&& mkdir -p /app/var \
&& chown -R ${USER}:${USER} /app
USER ${USER}
ARG XDEBUG_HOST="172.17.0.1"

RUN composer global require --no-progress --no-scripts --no-plugins symfony/flex ; \
composer global config --no-plugins allow-plugins.symfony/flex true
RUN addgroup --system --gid ${UID} ${USER} ; \
adduser --system --disabled-password --uid ${UID} --ingroup ${USER} ${USER} ; \
mkdir -p /app/var ; \
chown -R ${USER}:${USER} /app ; \
sed -i "s/%xdebug_host%/${XDEBUG_HOST}/g" /usr/local/etc/php/conf.d/xdebug.ini

USER ${USER}

CMD tail -f /dev/null
Loading

0 comments on commit b8facdc

Please sign in to comment.