Skip to content

Commit 7a3b2bc

Browse files
committed
Build docker image with code coverage support
1 parent 29881a0 commit 7a3b2bc

File tree

2 files changed

+77
-26
lines changed

2 files changed

+77
-26
lines changed

.github/workflows/test.Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ARG PHP_VERSION=latest
2+
FROM php:${PHP_VERSION}-cli-alpine
3+
4+
WORKDIR /workdir
5+
6+
# install composer
7+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
8+
ENV COMPOSER_ALLOW_SUPERUSER=1
9+
ENV COMPOSER_HTACCESS_PROTECT=0
10+
ENV COMPOSER_CACHE_DIR=/.composer
11+
12+
# install PHP extension pcov
13+
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
14+
&& mkdir -p /usr/src/php/ext/pcov && curl -fsSL https://pecl.php.net/get/pcov | tar xvz -C /usr/src/php/ext/pcov --strip 1 \
15+
&& docker-php-ext-install pcov \
16+
&& docker-php-ext-enable pcov \
17+
&& rm -Rf /usr/src/php/ext/pcov \
18+
&& apk del --no-cache .build-deps

.github/workflows/test.yml

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,99 @@ jobs:
1414
matrix:
1515
include:
1616
- PHP_VERSION: 7.1
17+
CODE_COVERAGE: false
18+
RUN_PHPSTAN: false
19+
RUN_PSALM: false
20+
RUN_BENCHMARK: false
1721
- PHP_VERSION: 7.2
22+
CODE_COVERAGE: true
23+
RUN_PHPSTAN: false
24+
RUN_PSALM: false
25+
RUN_BENCHMARK: false
1826
- PHP_VERSION: 7.3
27+
CODE_COVERAGE: true
28+
RUN_PHPSTAN: false
29+
RUN_PSALM: false
30+
RUN_BENCHMARK: false
1931
- PHP_VERSION: 7.4
32+
CODE_COVERAGE: true
2033
RUN_PHPSTAN: true
2134
RUN_PSALM: true
2235
RUN_BENCHMARK: true
2336
- PHP_VERSION: 8.0-rc
37+
CODE_COVERAGE: true
2438
RUN_PHPSTAN: true
2539
RUN_PSALM: true
2640
RUN_BENCHMARK: true
2741
COMPOSER_EXTRA_ARGS: --ignore-platform-reqs
28-
env:
29-
PHP_IMAGE: php:${{ matrix.PHP_VERSION }}-cli-alpine
42+
3043
steps:
3144
- uses: actions/checkout@v2
3245

33-
- name: Cache Composer Phar
34-
id: cache-composer-phar
46+
- name: Cache Docker Image
47+
id: cache-docker-image
3548
uses: actions/cache@v2
3649
with:
37-
path: composer.phar
38-
key: cache-composer-phar
50+
path: /tmp/docker-image.tar
51+
key: cache-docker-image-test:${{ matrix.PHP_VERSION }}
3952

40-
- name: Install Composer
41-
if: steps.cache-composer-phar.outputs.cache-hit != 'true'
42-
run: curl -sS https://getcomposer.org/installer | php -- --install-dir=. --filename=composer.phar
53+
- name: Load Docker Image
54+
if: steps.cache-docker-image.outputs.cache-hit == 'true'
55+
run: docker load --input /tmp/docker-image.tar
4356

44-
- name: Get Composer Setup
45-
id: composer-setup
46-
run: |
47-
echo "::set-output name=version::$(composer --version)"
48-
echo "::set-output name=cache-files-dir::$(composer config cache-files-dir)"
57+
- name: Build Docker Image
58+
if: steps.cache-docker-image.outputs.cache-hit != 'true'
59+
run: docker build -f .github/workflows/test.Dockerfile -t 'test:${{ matrix.PHP_VERSION }}' --build-arg 'PHP_VERSION=${{ matrix.PHP_VERSION }}' .
4960

5061
- name: Cache Composer Cache Files
5162
uses: actions/cache@v2
5263
with:
53-
path: ${{ steps.composer-setup.outputs.cache-files-dir }}
54-
key: cache-composer-cache-files-dir-${{ steps.composer-setup.outputs.version }}-${{ matrix.PHP_VERSION }}
64+
path: /tmp/composer-cache-files
65+
key: cache-composer-cache-files-${{ matrix.PHP_VERSION }}
5566
restore-keys: |
56-
cache-composer-cache-files-dir-${{ steps.composer-setup.outputs.version }}-
57-
cache-composer-cache-files-dir-
67+
cache-composer-cache-files-
5868
5969
- name: Install Composer Dependencies
6070
run: |
61-
if [ "${{ matrix.RUN_PHPSTAN }}" != "true" ]; then ./composer.phar remove --dev phpstan/phpstan --no-update --no-interaction; fi
62-
if [ "${{ matrix.RUN_PSALM }}" != "true" ]; then ./composer.phar remove --dev vimeo/psalm --no-update --no-interaction; fi
63-
if [ "${{ matrix.RUN_BENCHMARK }}" != "true" ]; then ./composer.phar remove --dev phpbench/phpbench --no-update --no-interaction; fi
64-
docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" -w '/workdir' -v '${{ steps.composer-setup.outputs.cache-files-dir }}:/.composer' -e 'COMPOSER_CACHE_DIR=/.composer' $PHP_IMAGE php ./composer.phar install --no-interaction --no-progress --prefer-dist ${{ matrix.COMPOSER_EXTRA_ARGS }}
71+
if [ "${{ matrix.RUN_PHPSTAN }}" != "true" ]; then composer remove --dev phpstan/phpstan --no-update --no-interaction; fi
72+
if [ "${{ matrix.RUN_PSALM }}" != "true" ]; then composer remove --dev vimeo/psalm --no-update --no-interaction; fi
73+
if [ "${{ matrix.RUN_BENCHMARK }}" != "true" ]; then composer remove --dev phpbench/phpbench --no-update --no-interaction; fi
74+
docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" -v '/tmp/composer-cache-files:/.composer' 'test:${{ matrix.PHP_VERSION }}' composer install --no-interaction --no-progress --prefer-dist ${{ matrix.COMPOSER_EXTRA_ARGS }}
6575
6676
- name: Run Unit Test
67-
run: docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" -w '/workdir' $PHP_IMAGE php -d 'zend.assertions=1' ./vendor/bin/phpunit
77+
run: |
78+
if [ "${{ matrix.CODE_COVERAGE }}" == "true" ]; then
79+
docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" 'test:${{ matrix.PHP_VERSION }}' php -d 'zend.assertions=1' -d 'pcov.enabled=1' ./vendor/bin/phpunit --coverage-clover=.clover.xml --coverage-html=./coverage-html
80+
else
81+
docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" 'test:${{ matrix.PHP_VERSION }}' php -d 'zend.assertions=1' ./vendor/bin/phpunit
82+
fi
83+
84+
- name: Archive Code Coverage
85+
uses: actions/upload-artifact@v2
86+
if: ${{ matrix.CODE_COVERAGE }}
87+
with:
88+
name: code-coverage-${{ matrix.PHP_VERSION }}
89+
path: ./coverage-html
90+
91+
- name: Upload Codecov Report
92+
uses: codecov/codecov-action@v1
93+
if: ${{ matrix.CODE_COVERAGE }}
94+
with:
95+
token: ${{ secrets.CODECOV_TOKEN }}
96+
file: .clover.xml
6897

6998
- name: Run PHPStan
7099
if: ${{ matrix.RUN_PHPSTAN }}
71-
run: docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" -w '/workdir' $PHP_IMAGE php ./vendor/bin/phpstan analyse --level max src/ tests/
100+
run: docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" 'test:${{ matrix.PHP_VERSION }}' php ./vendor/bin/phpstan analyse --level max src/ tests/
72101

73102
- name: Run psalm
74103
if: ${{ matrix.RUN_PSALM }}
75-
run: mkdir -p "$HOME/.cache/psalm" && docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" -w '/workdir' -v "$HOME/.cache/psalm:/.cache/psalm" $PHP_IMAGE php ./vendor/bin/psalm
104+
run: mkdir -p "$HOME/.cache/psalm" && docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" -v "$HOME/.cache/psalm:/.cache/psalm" 'test:${{ matrix.PHP_VERSION }}' php ./vendor/bin/psalm
76105

77106
- name: Run benchmark
78107
if: ${{ matrix.RUN_BENCHMARK }}
79-
run: docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" -w '/workdir' $PHP_IMAGE php -d 'zend.assertions=-1' ./vendor/bin/phpbench run --no-interaction --revs=1 --retry-threshold=100 --progress=travis
108+
run: docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/workdir" 'test:${{ matrix.PHP_VERSION }}' php -d 'zend.assertions=-1' ./vendor/bin/phpbench run --no-interaction --revs=1 --retry-threshold=100 --progress=travis
109+
110+
- name: Export Docker Image
111+
if: steps.cache-docker-image.outputs.cache-hit != 'true'
112+
run: docker save --output /tmp/docker-image.tar 'test:${{ matrix.PHP_VERSION }}'

0 commit comments

Comments
 (0)