Skip to content

Commit

Permalink
Optimize composer installs (#41)
Browse files Browse the repository at this point in the history
Co-authored-by: david_smith <david_smith@sweetwater.com>
  • Loading branch information
zero-to-prod and david_smith authored Jan 4, 2025
1 parent f21b7be commit 2caf321
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ jobs:
- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: vendor
path: ./vendor-${{ matrix.php-version }}
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-

- name: Install dependencies
run: docker compose run --rm -v ${{ github.workspace }}/test-output:/app/test-output ${{ matrix.php-version }}composer composer install --prefer-dist
- name: Install dependencies (if needed)
run: |
if [ ! -d ./vendor-${{ matrix.php-version }} ] || [ -z "$(ls -A ./vendor-${{ matrix.php-version }})" ]; then
echo "Dependencies not found or vendor directory is empty. Installing dependencies..."
docker compose run --rm -v ${{ github.workspace }}/vendor-${{ matrix.php-version }}:/app/vendor ${{ matrix.php-version }}composer composer install --prefer-dist
else
echo "Dependencies already installed for ${{ matrix.php-version }}. Skipping composer install."
fi
- name: Run tests
run: docker compose run --rm -v ${{ github.workspace }}/test-output:/app/test-output ${{ matrix.php-version }} vendor/bin/phpunit
run: docker compose run --rm -v ${{ github.workspace }}/vendor-${{ matrix.php-version }}:/app/vendor ${{ matrix.php-version }} vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
/.phpunit.cache/
/vendor-php81/
/vendor-php82/
/vendor-php83/
13 changes: 11 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
target: base
volumes:
- ./:/app
- ./vendor-php81:/app/vendor

php81debug:
build:
Expand All @@ -13,20 +14,23 @@ services:
volumes:
- ./:/app
- ./docker/php81:/usr/local/etc/php
- ./vendor-php81:/app/vendor

php81composer:
build:
context: docker/php81
target: composer
volumes:
- ./:/app
- ./vendor-php81:/app/vendor

php82:
build:
context: docker/php82
target: base
volumes:
- ./:/app
- ./vendor-php82:/app/vendor

php82debug:
build:
Expand All @@ -35,32 +39,37 @@ services:
volumes:
- ./:/app
- ./docker/php82:/usr/local/etc/php
- ./vendor-php82:/app/vendor

php82composer:
build:
context: docker/php82
target: composer
volumes:
- ./:/app
- ./vendor-php82:/app/vendor

php83:
build:
context: docker/php83
target: base
volumes:
- ./:/app
- ./vendor-php83:/app/vendor

php83debug:
build:
context: docker/php83
target: debug
volumes:
- ./:/app
- ./docker/php82:/usr/local/etc/php
- ./docker/php83:/usr/local/etc/php
- ./vendor-php83:/app/vendor

php83composer:
build:
context: docker/php83
target: composer
volumes:
- ./:/app
- ./:/app
- ./vendor-php83:/app/vendor
17 changes: 7 additions & 10 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
# run-tests.sh
#!/bin/bash
set -e

php_versions=("php83" "php82" "php81")

for version in "${php_versions[@]}"; do
if [ -f composer.lock ]; then
echo "removing composer.lock"
rm composer.lock
fi
vendor_dir="./vendor-${version}"

docker compose run --rm "$version"composer composer install
if [ ! -d "$vendor_dir" ] || [ -z "$(ls -A "$vendor_dir")" ]; then
echo "Dependencies not found or vendor directory is empty for $version. Installing dependencies..."
docker compose run --rm "${version}composer" composer install --no-cache
fi

echo "Running tests on $version..."
if ! docker compose run --rm "$version" vendor/bin/phpunit; then
echo "Tests failed on $version."
exit 1
fi
done

echo "All tests passed!"
done

0 comments on commit 2caf321

Please sign in to comment.