Skip to content

Commit 8d228d3

Browse files
committed
chore(build): add dockerfile to build and test project
1 parent d018296 commit 8d228d3

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

.dockerignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vendor
2+
*.cache
3+
composer.lock
4+
tools/php-cs-fixer/composer.lock
5+
.git
6+
.idea
7+
Dockerfile

Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
ARG PHP_VERSION
2+
FROM php:${PHP_VERSION}-cli-alpine AS dependencies
3+
ARG PHP_CS_FIXER
4+
RUN apk add --update --no-cache zip unzip php-zip
5+
COPY --from=composer /usr/bin/composer /usr/bin/composer
6+
RUN addgroup -S php && adduser -S php -G php \
7+
&& mkdir -p /usr/src/xml-lint \
8+
&& chown php:php -R /usr/src/xml-lint
9+
WORKDIR /usr/src/xml-lint
10+
COPY --chown=php:php . ./
11+
USER php
12+
RUN composer install --prefer-dist -o -a -n --no-progress \
13+
&& \
14+
if [[ -n "${PHP_CS_FIXER}" ]]; then \
15+
composer install --working-dir=tools/php-cs-fixer --prefer-dist -o -a -n --no-progress; \
16+
fi
17+
18+
FROM php:${PHP_VERSION}-cli-alpine AS test
19+
ARG PHP_CS_FIXER
20+
RUN addgroup -S php && adduser -S php -G php \
21+
&& mkdir -p /usr/src/xml-lint \
22+
&& chown php:php -R /usr/src/xml-lint
23+
24+
WORKDIR /usr/src/xml-lint
25+
COPY --from=dependencies --chown=php:php /usr/src/xml-lint ./
26+
USER php
27+
28+
RUN if [[ -n "${PHP_CS_FIXER}" ]]; then \
29+
php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run -v; \
30+
fi
31+
RUN php vendor/bin/phpunit
32+
RUN php vendor/bin/behat
33+
34+
FROM dependencies AS build_production
35+
WORKDIR /usr/src/xml-lint
36+
37+
RUN rm -rf tools/ tests/ \
38+
&& composer install --prefer-dist -o -a -n --no-progress --no-dev
39+
40+
FROM php:${PHP_VERSION}-cli-alpine AS production
41+
WORKDIR /usr/src/xml-lint
42+
COPY --from=build_production /usr/src/xml-lint ./
43+
RUN ln -s /usr/src/xml-lint/bin/xmllint /usr/bin/xml-lint
44+
WORKDIR /usr/src
45+
ENTRYPOINT ["php", "/usr/src/xml-lint/bin/xmllint"]
46+
CMD ["--help"]

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ php vendor/bin/behat
5757
Using docker:
5858

5959
```shell
60-
# Install dependencies
61-
docker run -it --rm -v "$PWD":/usr/src/xml-lint -w /usr/src/xml-lint -v ${COMPOSER_HOME:-$HOME/.composer}:/tmp --user $(id -u):$(id -g) composer install --ignore-platform-reqs --no-scripts
62-
docker run -it --rm -v "$PWD":/usr/src/xml-lint -w /usr/src/xml-lint/tools/php-cs-fixer -v ${COMPOSER_HOME:-$HOME/.composer}:/tmp --user $(id -u):$(id -g) composer install --ignore-platform-reqs --no-scripts
60+
# Example
61+
docker build -t xml-lint:php-8.1 --build-arg=PHP_VERSION="8.1" .
6362

64-
# Run code style check
65-
docker run -it --rm -v "$PWD":/usr/src/xml-lint -w /usr/src/xml-lint php:7.4-cli php tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run -v
63+
# PHP_VERSION: choose between 7.4, 8.0 and 8.1
64+
docker build -t xml-lint:php-7.4 --build-arg=PHP_VERSION="7.4" .
65+
docker build -t xml-lint:php-8.0 --build-arg=PHP_VERSION="8.0" .
66+
docker build -t xml-lint:php-8.1 --build-arg=PHP_VERSION="8.1" .
6667

67-
# Run tests
68-
docker run -it --rm -v "$PWD":/usr/src/xml-lint -w /usr/src/xml-lint --user $(id -u):$(id -g) php:7.4-cli php vendor/bin/phpunit
69-
docker run -it --rm -v "$PWD":/usr/src/xml-lint -w /usr/src/xml-lint --user $(id -u):$(id -g) php:7.4-cli php vendor/bin/behat
68+
# Run with code style check
69+
docker build -t xml-lint:php-7.4 --build-arg=PHP_VERSION="7.4" --build-arg=PHP_CS_FIXER=true .
70+
71+
# Use this image to run xml-lint:
72+
cd tests/functional/_testdata
73+
docker run -it --rm -v "$PWD":/var/src -w /var/src xml-lint:php-7.4 -r -v -- ./
7074
```
7175

7276

0 commit comments

Comments
 (0)