-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
94 lines (91 loc) · 3.85 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 that was installed, influenced by composer.json"
value: ${{ steps.emit-php-detected.outputs.php_version }}
runs:
using: "composite"
steps:
- uses: actions/checkout@v4
- id: read-php-version
name: "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
echo -n "composer.json 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 "and detected PHP version $php_version from composer.json!"
echo "php_version=$php_version" >> $GITHUB_OUTPUT
echo "has_composer=true" >> $GITHUB_OUTPUT
exit 0
else
echo " ... But does not specify a PHP version!"
echo "Please go set a PHP version in the require key of your composer.json"
exit 1;
fi
fi
echo "No PHP version detected"
exit 0;
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ steps.read-php-version.outputs.php_version }}
tools: ${{ inputs.php_tools }}
- id: composer-cache-find
name: "Find Composer Cache"
if: ${{ steps.read-php-version.outputs.has_composer }}
shell: bash
run: |
{
echo "dir=$(composer config cache-files-dir)"
echo "key=${{ runner.os }}-${{ inputs.working_directory }}-composer-${{ hashFiles('**/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: ${{ always() && steps.read-php-version.outputs.has_composer }}
uses: actions/cache/save@v4
with:
path: ${{ steps.composer-cache-find.outputs.dir }}
key: ${{ steps.composer-cache-restore.outputs.cache-primary-key }}
- id: emit-php-detected
name: "Emit PHP Detected"
shell: bash
run: |
PHP_VERSION=$(php -v | head -n 1 | cut -d ' ' -f 2 | cut -d '.' -f 1,2)
echo "PHP Version: $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
# Emit installed php version by major release
echo "PHP_VERSION=$PHP_VERSION" >> $GITHUB_ENV
echo "php_version=$PHP_VERSION" >> $GITHUB_OUTPUT