Update php.yml #125
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PHP Composer | |
on: | |
push: | |
branches: ["main", "develop"] | |
pull_request: | |
branches: ["main", "develop"] | |
permissions: | |
contents: write | |
pull-requests: write | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.event.pull_request.head.ref }} | |
fetch-depth: 0 | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Setup PHP | |
uses: shivammathur/[email protected] | |
with: | |
php-version: '8.2' | |
- name: Validate composer.json and composer.lock | |
run: composer validate --strict | |
- name: Cache Composer packages | |
id: composer-cache | |
uses: actions/cache@v3 | |
with: | |
path: vendor | |
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | |
restore-keys: | | |
${{ runner.os }}-php- | |
- name: Check for composer.json changes | |
id: check-composer | |
if: github.event_name == 'pull_request' | |
run: | | |
git diff --name-only origin/${{ github.base_ref }} HEAD | grep -q "composer.json" && echo "changed=true" >> $GITHUB_OUTPUT || echo "changed=false" >> $GITHUB_OUTPUT | |
- name: Install dependencies (Push to main/develop) | |
if: github.event_name == 'push' | |
run: | | |
if [ -f composer.lock ]; then | |
composer install --prefer-dist --no-progress | |
else | |
composer update --prefer-dist --no-progress | |
fi | |
- name: Update dependencies if composer.json changed (PR) | |
if: github.event_name == 'pull_request' && steps.check-composer.outputs.changed == 'true' | |
run: | | |
composer update --prefer-dist --no-progress | |
- name: Commit and push if composer.lock changed (PR) | |
if: github.event_name == 'pull_request' && steps.check-composer.outputs.changed == 'true' | |
run: | | |
if git diff --exit-code composer.lock; then | |
echo "No changes to composer.lock needed" | |
else | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
git add composer.lock vendor/ | |
git commit -m "Update composer.lock and vendor directory to match composer.json changes" | |
git push | |
fi |