-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
138 lines (125 loc) · 5.57 KB
/
action.yml
File metadata and controls
138 lines (125 loc) · 5.57 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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 in Composer"
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;
- id: read-system-php-version
name: "Read System PHP Version"
shell: bash
run: |
php_version=$(php -v | head -n 1 | cut -d ' ' -f 2 | cut -d '.' -f 1,2)
echo "No composer.json detected, falling back to system PHP version $php_version"
echo "php_version=$php_version" >> $GITHUB_OUTPUT
- id: decide-php-version
name: "Decide PHP Version"
shell: bash
run: |
echo "Requested PHP Version: ${{ steps.read-php-version.outputs.php_version }}"
echo "System PHP Version: ${{ steps.read-system-php-version.outputs.php_version }}"
if [ "${{ steps.read-php-version.outputs.php_version }}" != "" ] && [ "${{ steps.read-php-version.outputs.php_version }}" != "${{ steps.read-system-php-version.outputs.php_version }}" ]; then
echo "Switching PHP version to ${{ steps.read-php-version.outputs.php_version }}"
echo "keep_system_php=false" >> $GITHUB_OUTPUT
else
echo "Keeping system PHP version ${{ steps.read-system-php-version.outputs.php_version }}"
echo "keep_system_php=true" >> $GITHUB_OUTPUT
fi
- id: detect-if-apt-present
name: "Detect if apt is present"
shell: bash
run: |
if command -v apt-get > /dev/null 2>&1; then
echo "apt-get exists"
echo "apt_exists=true" >> $GITHUB_OUTPUT
else
echo "apt-get does not exist"
echo "apt_exists=false" >> $GITHUB_OUTPUT
fi
- uses: shivammathur/setup-php@v2
if: ${{ steps.decide-php-version.outputs.keep_system_php == 'false' && steps.detect-if-apt-present.outputs.apt_exists == 'true' }}
with:
php-version: ${{ steps.read-php-version.outputs.php_version }}
tools: ${{ inputs.php_tools }}
- id: composer-cache-find
name: "Find Composer Cache"
working-directory: ${{ inputs.working_directory }}
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 }}
- id: conditionally-composer-install
working-directory: ${{ inputs.working_directory }}
if: ${{ steps.read-php-version.outputs.has_composer }}
shell: bash
run: composer install --ignore-platform-reqs --prefer-dist --no-scripts
- 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