Github-Actions-Setup-PHP/action.yml

81 lines
3.3 KiB
YAML

name: "Setup PHP Project"
description: "Sets up a PHP project with GitHub Actions, including detecting PHP version and running composer install."
branding:
color: "orange"
icon: "arrow-up-circle"
inputs:
working_directory:
description: "The directory where the project is located."
required: false
default: "."
php_tools:
description: "The PHP tools to install. Comma seperated list from shivammathur/setup-php"
required: false
default: ""
outputs:
php_version:
description: "The PHP version detected from composer.json"
value: ${{ steps.read-php-version.outputs.php_version }}
runs:
using: "composite"
steps:
- uses: actions/checkout@v4
- id: read-php-version
shell: bash
working-directory: ${{ inputs.working_directory }}
run: |
# if composer.json exists, set the php_version output
if [ -f composer.json ]; then
# And that the require php key exists
if jq -e '.require["php"]' composer.json > /dev/null; then
php_version=$(jq -r '.require["php"]' composer.json | sed -E 's/[^0-9.]//g')
echo "Detected PHP version $php_version from composer.json"
echo "php_version=$php_version" >> $GITHUB_OUTPUT
echo "has_composer=true" >> $GITHUB_OUTPUT
exit 0
fi
fi
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ steps.read-php-version.outputs.php_version }}
tools: ${{ inputs.php_tools }}
- id: composer-cache-find
shell: bash
run: |
{
echo "dir=$(composer config cache-files-dir)"
echo "key=${{ runner.os }}-${{ inputs.working_directory }}-composer-${{ hashFiles('${{ inputs.working_directory }}/composer.lock') }}"
echo "restore-key=${{ runner.os }}-${{ inputs.working_directory }}-composer-"
} >> $GITHUB_OUTPUT
- id: composer-cache-restore
if: ${{ steps.read-php-version.outputs.has_composer }}
uses: actions/cache/restore@v4
with:
path: ${{ steps.composer-cache-find.outputs.dir }}
key: ${{ steps.composer-cache-find.outputs.key
restore-keys: ${{ steps.composer-cache-find.outputs.restore-key }}
- working-directory: ${{ inputs.working_directory }}
if: ${{ steps.read-php-version.outputs.has_composer }}
shell: bash
run: composer install --ignore-platform-reqs --prefer-dist
- id: composer-cache-save
if: ${{ steps.read-php-version.outputs.has_composer }}
uses: actions/cache/save@v4
with:
path: ${{ steps.composer-cache-find.outputs.dir }}
key: ${{ steps.composer-cache-find.outputs.key }}
- shell: bash
run: |
echo "PHP Version: ${{ steps.read-php-version.outputs.php_version }}"
echo "PHP Tools: ${{ inputs.php_tools }}"
if [ ${{ steps.read-php-version.outputs.has_composer }} == "true" ]; then
echo "Composer Cache Dir: ${{ steps.composer-cache-find.outputs.dir }}"
echo "Composer Cache Key: ${{ steps.composer-cache-find.outputs.key }}"
echo "Composer Cache Restore Key: ${{ steps.composer-cache-find.outputs.restore-key }}"
fi
{
echo "PHP_VERSION=${{ steps.read-php-version.outputs.php_version }}"
} >> $GITHUB_ENV