Continuous integration: Automate everything!

Roland Franssen
Jan 2, 2018 · 5 min read

The folder structure

./
├── bin/
│ └── ...
├── src/
│ ├── Package/
│ │ ├── composer.json
│ │ └── ...
│ └── OtherPackage/
├── composer.json
└── ...
src/Component/Some
src/Bundle/Some(Bundle)
src/Some
src/SomeBundle

The goal

Create a set of tools to ease local development, as well as CI build scripts.

The checks

Our tools

#!/usr/bin/env bash

if [[ $# < 1 ]] ; then
echo "Usage: $0 <cmd>"
exit 1
fi

CMD="${@}"
RETURN=0

for PACKAGE in $(find src/*/composer.json -type f) ; do
DIR=$(dirname ${PACKAGE})
pushd ${DIR} &> /dev/null
echo "Running in \"${DIR}\""
bash -xc "${CMD}" 2>&1
LAST=$? ; [[ ! $LAST = 0 ]] && RETURN=${LAST}
popd &> /dev/null
done

exit ${RETURN}

Tests

bin/each composer update
bin/each vendor/bin/simple-phpunit
#!/usr/bin/env bash

bin/each composer "${@}"

#!/usr/bin/env bash

bin/each vendor/bin/simple-phpunit "${@}"

Code style

vendor/bin/php-cs-fixer fix --dry-run --verbose --diff src/
#!/usr/bin/env bash

BIN="vendor/bin/php-cs-fixer"
CMD="${BIN} fix --dry-run --verbose --diff ${@:-src/}"

if [[ ! -f $BIN ]] ; then
composer update --prefer-dist --no-interaction --no-suggest
fi

${CMD}

Static analysis

BIN="vendor/bin/phpstan"
CMD="${BIN} analyse --configuration phpstan.neon --level 7 ${@:-src/}"

Configuring Travis

php:
- 7.1

matrix:
include:
- php: 7.1
env:
- DEPS=low
- php: 7.1
env:
- DEPS=high
- CC=true
- CS=true
- SA=true

install:
- if [[ ! $DEPS ]] ; then bin/composer update ; fi
- if [[ $DEPS = high ]] ; then bin/composer update --prefer-stable ; fi
- if [[ $DEPS = low ]] ; then bin/composer update --prefer-stable --prefer-lowest ; fi
- if [[ $CS = true ]] || [[ $SA = true ]] ; then composer update ; fi

script:
- if [[ ! $CC ]] ; then bin/phpunit; fi
- if [[ $CC = true ]] ; then bin/phpunit --coverage-clover=coverage.xml ; fi
- if [[ $CS = true ]] ; then bin/cs ; fi
- if [[ $SA = true ]] ; then bin/sa ; fi

after_success:
- if [[ $CC = true ]] ; then .travis/codecov ; fi

Final words

Roland Franssen

Written by

Symfony Developer & Contributor, living and working in the Netherlands 🌷 / Purist / Opinionated / Into UX & DX / @https://msgphp.github.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade