From aa4d0f408468fc7989b0314fa0010b68cefdaa96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Draho=C5=A1?= Date: Sat, 23 Sep 2017 18:10:33 +0200 Subject: [PATCH 1/5] Docker - spike building docker image --- .ci/Dockerfile | 17 +++++++++++++++++ .ci/build-docker-image | 29 +++++++++++++++++++++++++++++ .dockerignore | 15 +++++++++++++++ .gitattributes | 2 ++ .travis.yml | 22 +++++++++++++++++++--- bin/suggested-tools.sh | 4 +++- 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 .ci/Dockerfile create mode 100755 .ci/build-docker-image create mode 100644 .dockerignore diff --git a/.ci/Dockerfile b/.ci/Dockerfile new file mode 100644 index 00000000..5de0dcc0 --- /dev/null +++ b/.ci/Dockerfile @@ -0,0 +1,17 @@ +FROM composer + +RUN apk add --update libxslt-dev && \ + docker-php-ext-install xsl + +COPY ./composer.json ./composer.lock ./bin/suggested-tools.sh /phpqa/ +RUN (cd /phpqa && \ + composer install --no-dev && \ + ./suggested-tools.sh install --update-no-dev && \ + ln -s /phpqa/phpqa /usr/local/bin && \ + ln -s /phpqa/vendor/bin/* /usr/local/bin && \ + ls -lA /usr/local/bin | grep phpqa) + +COPY ./ /phpqa/ +WORKDIR /phpqa + +ENTRYPOINT ["docker-php-entrypoint"] diff --git a/.ci/build-docker-image b/.ci/build-docker-image new file mode 100755 index 00000000..f34bf891 --- /dev/null +++ b/.ci/build-docker-image @@ -0,0 +1,29 @@ +#!/usr/bin/env sh + +APP_DIR="./" +DOCKER_REPO="zdenekdrahos/phpqa" +DOCKER_FILE=".ci/Dockerfile" +DOCKER_TAG="" + +run () { + load_docker_variables + build_docker_image +} + +load_docker_variables () { + DOCKER_TAG="$DOCKER_REPO:${TRAVIS_TAG:-$TRAVIS_COMMIT}" + + echo "Dockefile: $DOCKER_FILE" + echo "Build context: $APP_DIR" + echo "Publish image: $DOCKER_TAG" + echo +} + +build_docker_image () { + set -e + docker login --username $DOCKER_USER --password $DOCKER_TOKEN + time docker build --file $DOCKER_FILE --tag $DOCKER_TAG $APP_DIR + time docker push $DOCKER_TAG +} + +run diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b79ed092 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +.git* +.docker* +.ci +bin/deploy-report.sh +bin/ci.sh +build +nbproject +tests +vendor +*-phpqa.* +.php_cs* +.travis.yml +docker-compose.yml +phpunit.xml +auth.json diff --git a/.gitattributes b/.gitattributes index 1dd98e22..ffcced19 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,7 @@ /tests export-ignore /bin export-ignore +/.ci export-ignore +/.dockerignore export-ignore /.gitattributes export-ignore /.gitignore export-ignore /.travis.yml export-ignore diff --git a/.travis.yml b/.travis.yml index b3efee21..c52cae92 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ -sudo: false -dist: precise +sudo: required +dist: trusty language: php +services: + - docker matrix: include: - php: 5.4 @@ -13,7 +15,7 @@ matrix: install: # Disable JIT compilation in hhvm, as the JIT is useless for short live scripts like tests. - if [[ $TRAVIS_PHP_VERSION = hhvm* ]]; then echo 'hhvm.jit = 0' >> /etc/hhvm/php.ini; fi - - if [ -n "$GITHUB_OAUTH_TOKEN" ]; then composer config github-oauth.github.com ${GITHUB_OAUTH_TOKEN}; fi; + - if [ -n "$GITHUB_OAUTH_TOKEN" ]; then composer config -g github-oauth.github.com ${GITHUB_OAUTH_TOKEN}; fi; - composer install --no-interaction # test app with symfony3 components in latest php - if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.1" ]]; then composer update && composer remove phpunit/phpunit --dev --no-interaction && composer require sebastian/phpcpd:~3.0 phploc/phploc:~4 phpunit/phpunit:~5.7 && bin/suggested-tools.sh install; fi @@ -24,6 +26,20 @@ script: - ./phpqa tools - bin/ci.sh # http://blog.wyrihaximus.net/2015/07/composer-cache-on-travis/ +deploy: + - provider: script + script: .ci/build-docker-image + skip_cleanup: true + on: + tags: true + php: 7.0 + - provider: script + script: ls -la .ci && .ci/build-docker-image + skip_cleanup: true + on: + branch: docker + php: 7.0 + cache: directories: - vendor diff --git a/bin/suggested-tools.sh b/bin/suggested-tools.sh index bb2fd99d..54d6182a 100755 --- a/bin/suggested-tools.sh +++ b/bin/suggested-tools.sh @@ -2,14 +2,16 @@ # Usage: # $ bin/suggested-tools.sh install +# $ bin/suggested-tools.sh install --update-no-dev # $ bin/suggested-tools.sh remove mode="$1" +requireMode="$2" if [ $mode = "install" ] then echo "Installing suggested tools" - composer require jakub-onderka/php-parallel-lint jakub-onderka/php-console-highlighter phpstan/phpstan friendsofphp/php-cs-fixer:~2.2 vimeo/psalm + composer require jakub-onderka/php-parallel-lint jakub-onderka/php-console-highlighter phpstan/phpstan friendsofphp/php-cs-fixer:~2.2 vimeo/psalm $requireMode else echo "Removing suggested tools" composer remove jakub-onderka/php-parallel-lint jakub-onderka/php-console-highlighter phpstan/phpstan friendsofphp/php-cs-fixer vimeo/psalm From fe0b5bd0db8a6cb27e01c00daf736e17fe5be6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Draho=C5=A1?= Date: Sun, 24 Sep 2017 08:49:23 +0200 Subject: [PATCH 2/5] Docker - rename .ci directory, shorter commit in tag --- {.ci => .docker}/Dockerfile | 0 .ci/build-docker-image => .docker/build-image | 6 +++--- .dockerignore | 1 - .gitattributes | 2 +- .travis.yml | 4 ++-- 5 files changed, 6 insertions(+), 7 deletions(-) rename {.ci => .docker}/Dockerfile (100%) rename .ci/build-docker-image => .docker/build-image (80%) diff --git a/.ci/Dockerfile b/.docker/Dockerfile similarity index 100% rename from .ci/Dockerfile rename to .docker/Dockerfile diff --git a/.ci/build-docker-image b/.docker/build-image similarity index 80% rename from .ci/build-docker-image rename to .docker/build-image index f34bf891..fdee53c6 100755 --- a/.ci/build-docker-image +++ b/.docker/build-image @@ -1,8 +1,8 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash APP_DIR="./" DOCKER_REPO="zdenekdrahos/phpqa" -DOCKER_FILE=".ci/Dockerfile" +DOCKER_FILE=".docker/Dockerfile" DOCKER_TAG="" run () { @@ -11,7 +11,7 @@ run () { } load_docker_variables () { - DOCKER_TAG="$DOCKER_REPO:${TRAVIS_TAG:-$TRAVIS_COMMIT}" + DOCKER_TAG="$DOCKER_REPO:${TRAVIS_TAG:-${TRAVIS_COMMIT:0:7}}" echo "Dockefile: $DOCKER_FILE" echo "Build context: $APP_DIR" diff --git a/.dockerignore b/.dockerignore index b79ed092..8bb3b47c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,5 @@ .git* .docker* -.ci bin/deploy-report.sh bin/ci.sh build diff --git a/.gitattributes b/.gitattributes index ffcced19..d50e001c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,6 +1,6 @@ /tests export-ignore /bin export-ignore -/.ci export-ignore +/.docker export-ignore /.dockerignore export-ignore /.gitattributes export-ignore /.gitignore export-ignore diff --git a/.travis.yml b/.travis.yml index c52cae92..9435b001 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,13 +28,13 @@ script: # http://blog.wyrihaximus.net/2015/07/composer-cache-on-travis/ deploy: - provider: script - script: .ci/build-docker-image + script: .docker/build-image skip_cleanup: true on: tags: true php: 7.0 - provider: script - script: ls -la .ci && .ci/build-docker-image + script: ls -la .docker && .docker/build-image skip_cleanup: true on: branch: docker From 2fd7983cb13ecd15fc970a4889681abec8e05d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Draho=C5=A1?= Date: Sun, 24 Sep 2017 09:11:26 +0200 Subject: [PATCH 3/5] Docker - ignore .docker, .dpl directories --- .dockerignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.dockerignore b/.dockerignore index 8bb3b47c..828ee2de 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,7 @@ .git* -.docker* +.docker +.dockerignore +.dpl bin/deploy-report.sh bin/ci.sh build From 167dc390ffb5dc894cb316df03b0adc0b48cc2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Draho=C5=A1?= Date: Sun, 24 Sep 2017 09:31:09 +0200 Subject: [PATCH 4/5] Docker - build image only on tags --- .travis.yml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9435b001..843aa26f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,6 @@ script: - vendor/phpunit/phpunit/phpunit - ./phpqa tools - bin/ci.sh -# http://blog.wyrihaximus.net/2015/07/composer-cache-on-travis/ deploy: - provider: script script: .docker/build-image @@ -33,13 +32,7 @@ deploy: on: tags: true php: 7.0 - - provider: script - script: ls -la .docker && .docker/build-image - skip_cleanup: true - on: - branch: docker - php: 7.0 - +# http://blog.wyrihaximus.net/2015/07/composer-cache-on-travis/ cache: directories: - vendor From 6c84ad61229ce5eec894ab8ad8fee5cdbb1833c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Draho=C5=A1?= Date: Sun, 24 Sep 2017 09:56:23 +0200 Subject: [PATCH 5/5] Docker - add basic documentation --- README.md | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 91c29e86..d19ceb2c 100644 --- a/README.md +++ b/README.md @@ -124,11 +124,25 @@ qa/phpqa ### Docker +Official docker image is https://hub.docker.com/r/zdenekdrahos/phpqa/. +The image can be used at [Gitlab CI](#gitlabci---docker-installation--composer-cache--artifacts). +Beware that the image is as lean as possible. That can be a problem for running PHPUnit tests. +In that case, you might miss PHP extensions for database etc (_you can [install phpqa](https://gitlab.com/costlocker/integrations/blob/213aab7/.ci/get-phpqa-binary#L40) in another [php image](https://gitlab.com/costlocker/integrations/blob/213aab7/.ci/.gitlab-ci.yml#L28)_). + +```bash +docker run --rm -it zdenekdrahos/phpqa:v1.14.0 phpqa tools +# using a tool without phpqa +docker run --rm -it zdenekdrahos/phpqa:v1.14.0 phploc -v +``` + +There are also available images [eko3alpha/docker-phpqa](https://hub.docker.com/r/eko3alpha/docker-phpqa/) and [sparkfabrik/docker-phpqa](https://hub.docker.com/r/sparkfabrik/docker-phpqa/). +`phpqa` is used as [an entrypoint](https://docs.docker.com/engine/reference/builder/#entrypoint) (_I haven't been able to use these images at Gitlab CI_). + ```bash docker run --rm -u $UID -v $PWD:/app eko3alpha/docker-phpqa --report --ignoredDirs vendor,build,migrations,test ``` -For full documentation please visit [eko3alpha/docker-phpqa](https://hub.docker.com/r/eko3alpha/docker-phpqa/). + ## Analyze @@ -379,6 +393,31 @@ test: - cp -r ./var/tests $CIRCLE_ARTIFACTS ``` +### Gitlab.ci - docker installation + composer cache + artifacts + +```yaml +stages: + - test + +test: + stage: test + image: zdenekdrahos/phpqa:v1.14.0 + variables: + BACKEND_QA: "*/backend/var/QA" + BACKEND_CACHE: $CI_PROJECT_DIR/.composercache + cache: + paths: + - $BACKEND_CACHE + script: + - 'export COMPOSER_CACHE_DIR=$BACKEND_CACHE' + - 'composer install --ignore-platform-reqs --no-progress --no-suggest' + - 'phpqa --report --tools phpcs:0,phpunit:0 --buildDir var/QA --analyzedDirs ./ --ignoredDirs var,vendor' + artifacts: + when: always + paths: + - $BACKEND_QA +``` + ## Contributing Contributions from others would be very much appreciated! Send