diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..52d3402 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,109 @@ +--- + +### +### Enable sudo (required for docker service) +### +sudo: required + + +### +### Language +### +language: python + + +### +### Add services +### +services: + - docker + + +### +### Build Matrix +### +env: + matrix: + - PHPCS=2 PHP=5.6 + - PHPCS=3 PHP=5.6 + - PHPCS=latest PHP=5.6 + - PHPCS=2 PHP=7.0 + - PHPCS=3 PHP=7.0 + - PHPCS=latest PHP=7.0 + - PHPCS=2 PHP=7.1 + - PHPCS=3 PHP=7.1 + - PHPCS=latest PHP=7.1 + - PHPCS=2 PHP=7.2 + - PHPCS=3 PHP=7.2 + - PHPCS=latest PHP=7.2 + - PHPCS=2 PHP=7.3 + - PHPCS=3 PHP=7.3 + - PHPCS=latest PHP=7.3 + - PHPCS=2 PHP=latest + - PHPCS=3 PHP=latest + - PHPCS=latest PHP=latest + + +### +### Install requirements +### +install: + # Get newer docker version + - while ! sudo apt-get update; do sleep 1; done + - while ! sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce; do sleep 1; done + - docker version + + +### +### Check generation changes, build and test +### +before_script: + - make lint + - while ! make build PHPCS=${PHPCS} PHP=${PHP}; do sleep 1; done + - while ! make test PHPCS=${PHPCS} PHP=${PHP}; do sleep 1; done + - git diff --quiet || { echo "Build Changes"; git diff; git status; false; } + + +### +### Push to Dockerhub +### +script: + # Push to docker hub on success + - if [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then + while ! make login USER="${DOCKER_USERNAME}" PASS="${DOCKER_PASSWORD}"; do sleep 1; done; + if [ -n "${TRAVIS_TAG}" ]; then + if [ "${PHPCS}" == "latest" ] && [ "${PHP}" == "latest" ]; then + while ! make push TAG="latest-${TRAVIS_TAG}"; do sleep 1; done; + else + if [ "${PHP}" == "latest" ]; then + while ! make push TAG="${PHPCS}-${TRAVIS_TAG}"; do sleep 1; done; + else + while ! make push TAG="${PHPCS}-php${PHP}-${TRAVIS_TAG}"; do sleep 1; done; + fi + fi + elif [ "${TRAVIS_BRANCH}" == "master" ]; then + if [ "${PHPCS}" == "latest" ] && [ "${PHP}" == "latest" ]; then + while ! make push TAG=latest; do sleep 1; done; + else + if [ "${PHP}" == "latest" ]; then + while ! make push TAG=${PHPCS}; do sleep 1; done; + else + while ! make push TAG=${PHPCS}-php${PHP}; do sleep 1; done; + fi + fi + elif [[ ${TRAVIS_BRANCH} =~ ^(release-[.0-9]+)$ ]]; then + if [ "${PHPCS}" == "latest" ] && [ "${PHP}" == "latest" ]; then + while ! make push TAG="latest-${TRAVIS_BRANCH}"; do sleep 1; done; + else + if [ "${PHP}" == "latest" ]; then + while ! make push TAG="${PHPCS}-${TRAVIS_BRANCH}"; do sleep 1; done; + else + while ! make push TAG="${PHPCS}-php${PHP}-${TRAVIS_BRANCH}"; do sleep 1; done; + fi + fi + else + echo "Skipping branch ${TRAVIS_BRANCH}"; + fi + else + echo "Skipping push on PR"; + fi diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8bec8ea --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +ARG PHP +FROM php:7.3 as builder + +# Install build dependencies +RUN set -eux \ + && DEBIAN_FRONTEND=noninteractive apt-get update -qq \ + && DEBIAN_FRONTEND=noninteractive apt-get install -qq -y --no-install-recommends --no-install-suggests \ + ca-certificates \ + curl \ + git \ + && git clone https://github.com/squizlabs/PHP_CodeSniffer + +ARG PHPCS +RUN set -eux \ + && cd PHP_CodeSniffer \ + && if [ "${PHPCS}" = "latest" ]; then \ + VERSION="$( git describe --abbrev=0 --tags )"; \ + else \ + VERSION="$( git tag | grep -E "^v?${PHPCS}\.[.0-9]+\$" | sort -V | tail -1 )"; \ + fi \ + && curl -sS -L https://github.com/squizlabs/PHP_CodeSniffer/releases/download/${VERSION}/phpcs.phar -o /phpcs.phar \ + && chmod +x /phpcs.phar \ + && mv /phpcs.phar /usr/bin/phpcs + + +FROM php:${PHP} as production +LABEL \ + maintainer="cytopia " \ + repo="https://github.com/cytopia/docker-phpcs" + +COPY --from=builder /usr/bin/phpcs /usr/bin/phpcs +ENV WORKDIR /data +WORKDIR /data + +ENTRYPOINT ["phpcs"] +CMD ["--version"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..548d48e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 cytopia + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5c87101 --- /dev/null +++ b/Makefile @@ -0,0 +1,163 @@ +ifneq (,) +.error This Makefile requires GNU Make. +endif + +.PHONY: build rebuild lint test _test-phpcs-version _test-php-version _test-run tag pull login push enter + +CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) + +DIR = . +FILE = Dockerfile +IMAGE = cytopia/phpcs +TAG = latest + +PHP = latest +PHPCS = latest + +build: +ifeq ($(PHP),latest) + docker build --build-arg PHP=7-cli-alpine --build-arg PHPCS=$(PHPCS) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) +else + docker build --build-arg PHP=$(PHP)-cli-alpine --build-arg PHPCS=$(PHPCS) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) +endif + +rebuild: pull +ifeq ($(PHP),latest) + docker build --no-cache --build-arg PHP=7-cli-alpine --build-arg PHPCS=$(PHPCS) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) +else + docker build --no-cache --build-arg PHP=$(PHP)-cli-alpine --build-arg PHPCS=$(PHPCS) -t $(IMAGE) -f $(DIR)/$(FILE) $(DIR) +endif + +lint: + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-cr --text --ignore '.git/,.github/,tests/' --path . + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-crlf --text --ignore '.git/,.github/,tests/' --path . + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-single-newline --text --ignore '.git/,.github/,tests/' --path . + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-trailing-space --text --ignore '.git/,.github/,tests/' --path . + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8 --text --ignore '.git/,.github/,tests/' --path . + @docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint file-utf8-bom --text --ignore '.git/,.github/,tests/' --path . + +test: + @$(MAKE) --no-print-directory _test-phpcs-version + @$(MAKE) --no-print-directory _test-php-version + @$(MAKE) --no-print-directory _test-run + +_test-phpcs-version: + @echo "------------------------------------------------------------" + @echo "- Testing correct phpcs version" + @echo "------------------------------------------------------------" + @if [ "$(PHPCS)" = "latest" ]; then \ + echo "Fetching latest version from GitHub"; \ + LATEST="$$( \ + curl -L -sS https://github.com/squizlabs/PHP_CodeSniffer/releases \ + | tac | tac \ + | grep -Eo '/[.0-9]+?\.[.0-9]+/' \ + | grep -Eo '[.0-9]+' \ + | sort -V \ + | tail -1 \ + )"; \ + echo "Testing for latest: $${LATEST}"; \ + if ! docker run --rm $(IMAGE) --version | grep -E "^PHP_CodeSniffer[[:space:]]+version[[:space:]]+v?$${LATEST}"; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + else \ + echo "Testing for tag: $(PHPCS).x.x"; \ + if ! docker run --rm $(IMAGE) --version | grep -E "^PHP_CodeSniffer[[:space:]]+version[[:space:]]+v?$(PHPCS)\.[.0-9]+"; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + fi; \ + echo "Success"; \ + +_test-php-version: + @echo "------------------------------------------------------------" + @echo "- Testing correct PHP version" + @echo "------------------------------------------------------------" + @if [ "$(PHP)" = "latest" ]; then \ + echo "Fetching latest version from GitHub"; \ + LATEST="$$( \ + curl -L -sS https://github.com/php/php-src/releases \ + | tac | tac \ + | grep -Eo '/php-[.0-9]+?\.[.0-9]+"' \ + | grep -Eo '[.0-9]+' \ + | sort -V \ + | tail -1 \ + )"; \ + echo "Testing for latest: $${LATEST}"; \ + if ! docker run --rm --entrypoint=php $(IMAGE) --version | head -1 | grep -E "^PHP[[:space:]]+$${LATEST}[[:space:]]"; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + else \ + echo "Testing for tag: $(PHP).x"; \ + if ! docker run --rm --entrypoint=php $(IMAGE) --version | head -1 | grep -E "^PHP[[:space:]]+$(PHP)\.[.0-9]+[[:space:]]"; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + fi; \ + echo "Success"; \ + +_test-tg-version: + @echo "------------------------------------------------------------" + @echo "- Testing correct Terragrunt version" + @echo "------------------------------------------------------------" + @if [ "$(TG_VERSION)" = "latest" ]; then \ + echo "Fetching latest version from GitHub"; \ + LATEST="$$( \ + curl -L -sS https://github.com/gruntwork-io/terragrunt/releases \ + | tac | tac \ + | grep -Eo '/v[.0-9]+/' \ + | grep -Eo 'v[.0-9]+' \ + | sort -u \ + | sort -V \ + | tail -1 \ + )"; \ + echo "Testing for latest: $${LATEST}"; \ + if ! docker run --rm $(IMAGE) terragrunt --version | grep -E "^terragrunt[[:space:]]*version[[:space:]]*v?$${LATEST}$$"; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + else \ + echo "Testing for tag: $(TG_VERSION)"; \ + if ! docker run --rm $(IMAGE) terragrunt --version | grep -E "^terragrunt[[:space:]]*version[[:space:]]*v?$(TG_VERSION)\.[.0-9]+$$"; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + fi; \ + echo "Success"; \ + +_test-run: + @echo "------------------------------------------------------------" + @echo "- Testing phpcs (success)" + @echo "------------------------------------------------------------" + @if ! docker run --rm -v $(CURRENT_DIR)/tests/ok:/data $(IMAGE) .; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + echo "Success"; + @echo "------------------------------------------------------------" + @echo "- Testing phpcs (failure)" + @echo "------------------------------------------------------------" + @if docker run --rm -v $(CURRENT_DIR)/tests/fail:/data $(IMAGE) .; then \ + echo "Failed"; \ + exit 1; \ + fi; \ + echo "Success"; + +tag: + docker tag $(IMAGE) $(IMAGE):$(TAG) + +pull: + @grep -E '^\s*FROM' Dockerfile \ + | sed -e 's/^FROM//g' -e 's/[[:space:]]*as[[:space:]]*.*$$//g' \ + | xargs -n1 docker pull; + +login: + yes | docker login --username $(USER) --password $(PASS) + +push: + @$(MAKE) tag TAG=$(TAG) + docker push $(IMAGE):$(TAG) + +enter: + docker run --rm --name $(subst /,-,$(IMAGE)) -it --entrypoint=/bin/sh $(ARG) $(IMAGE):$(TAG) diff --git a/tests/fail/fail.php b/tests/fail/fail.php new file mode 100644 index 0000000..615d9b9 --- /dev/null +++ b/tests/fail/fail.php @@ -0,0 +1,6 @@ + + * @copyright 2019 cytopia + * @license MIT, https://opensource.org/licenses/MIT + * @link https://github.com/cytopia/docker-phpcs + */ + +echo 'test';