-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.yml
206 lines (194 loc) · 7.93 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
name: Generate Matrix
description: GitHub Action to create a dynamic Silverstripe CI matrix
inputs:
composer_install:
type: boolean
required: false
default: false
# extra jobs must be multi-line string, as there's no support for type: array for inputs
extra_jobs:
type: string
required: false
default: ''
dynamic_matrix:
type: boolean
default: true
# simple matrix will only run a single job with the lowest supported PHP and mysql versions instead of a full matrix
simple_matrix:
type: boolean
default: false
endtoend:
type: boolean
default: true
# this option is intended only for community modules
# modules on silverstripe account will ignore this value and always run codecov
phpcoverage:
type: boolean
default: false
# this option is specifically for turning off codecov on modules on the silverstripe account
# use this if there are something like segfaults during a codecov run
phpcoverage_force_off:
type: boolean
default: false
phplinting:
type: boolean
default: true
phpunit:
type: boolean
default: true
phpunit_skip_suites:
type: string
required: false
default: ''
js:
type: boolean
default: true
doclinting:
type: boolean
default: true
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
outputs:
matrix:
description: JSON matrix
value: ${{ steps.php-script.outputs.matrix }}
runs:
using: composite
steps:
- name: Set fetch depth
id: set-fetch-depth
env:
GITHUB_REPOSITORY: ${{ github.repository }}
shell: bash
run: |
# Default fetch-depth of 1 will only fetch the latest commit
# This is fine for non-pull-request events where standard major.minor naming conventions
# are used so we do not need to attempt to get the name of the parent branch
FETCH_DEPTH=1
# For pull-requests, set fetch-depth to one more than the number of commits
# so we can later make a best attempt at getting the name of the parent branch
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
PR_NUMBER=${{ github.event.pull_request.number }}
# https://docs.github.com/en/rest/pulls/pulls#list-commits-on-a-pull-request
RESP_CODE=$(curl -w %{http_code} -s -o __pr_commits.json \
-X GET https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/commits \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}" \
)
if [[ $RESP_CODE == "200" ]]; then
NUM_COMMITS=$(cat __pr_commits.json | jq '. | length')
FETCH_DEPTH=$(( NUM_COMMITS + 1 ))
rm __pr_commits.json
else
echo "Failed to fetch commits for pull-request - HTTP response code was $RESP_CODE"
exit 1
fi
fi
echo "FETCH_DEPTH is $FETCH_DEPTH"
echo "fetch-depth=$FETCH_DEPTH" >> "$GITHUB_OUTPUT"
- name: Checkout code
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
fetch-depth: ${{ steps.set-fetch-depth.outputs.fetch-depth }}
- name: Install PHP
uses: shivammathur/setup-php@c541c155eee45413f5b09a52248675b1a2575231 # v2.31.1
with:
php-version: '8.1'
extensions: yaml
tools: composer:v2
# This is shared between runs, not just jobs. It means the first time the repo runs the job it'll
# need to download requirements for the first time, after that it will be plenty quick
# https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows
- name: Enable shared composer cache
uses: actions/cache@3624ceb22c1c5a301c8db4169662070a689d9ea8 # @v4.1.1
with:
path: ~/.cache/composer
key: shared-composer-cache
# Install composer dependencies for this action itself
- name: Composer
shell: bash
run: |
cd ${{ github.action_path }}
composer install
cd -
- name: Create __inputs.yml
shell: bash
# Add string inputs to memory instead of using string substituion in shell script
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
env:
EXTRA_JOBS: ${{ inputs.extra_jobs }}
GITHUB_REPOSITORY: ${{ github.repository }}
# github.base_ref is the target branch on a pull-request
# github.ref_name is the name of the branch on push, and the tag on tag
GITHUB_MY_REF: ${{ github.base_ref && github.base_ref || github.ref_name }}
run: |
# Escape double quotes '"' => '\"'
EXTRA_JOBS=${EXTRA_JOBS//\"/\\\"}
GITHUB_MY_REF=${GITHUB_MY_REF//\"/\\\"}
if [ -f __inputs.yml ]; then
rm __inputs.yml
fi
# e.g. push event to branch called myaccount-patch-1
# use git history to make a best attempt at getting the parent branch
# note: { grep * || :; } is to prevent exit code of 1 on zero-match from halting workflow
PARENT_BRANCH=$(git show-branch -a 2>/dev/null | { grep '\*' || :; } | { grep -v `git rev-parse --abbrev-ref HEAD` || :; } | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//')
# create __inputs.yml
touch __inputs.yml
echo "composer_install: ${{ inputs.composer_install }}" >> __inputs.yml
echo "endtoend: ${{ inputs.endtoend }}" >> __inputs.yml
echo "js: ${{ inputs.js }}" >> __inputs.yml
echo "phpcoverage: ${{ inputs.phpcoverage }}" >> __inputs.yml
echo "phpcoverage_force_off: ${{ inputs.phpcoverage_force_off }}" >> __inputs.yml
echo "phplinting: ${{ inputs.phplinting }}" >> __inputs.yml
echo "phpunit: ${{ inputs.phpunit }}" >> __inputs.yml
echo "doclinting: ${{ inputs.doclinting }}" >> __inputs.yml
echo "phpunit_skip_suites: ${{ inputs.phpunit_skip_suites }}" >> __inputs.yml
echo "dynamic_matrix: ${{ inputs.dynamic_matrix }}" >> __inputs.yml
echo "simple_matrix: ${{ inputs.simple_matrix }}" >> __inputs.yml
echo "github_repository: '$GITHUB_REPOSITORY'" >> __inputs.yml
echo "github_my_ref: '$GITHUB_MY_REF'" >> __inputs.yml
echo "parent_branch: '$PARENT_BRANCH'" >> __inputs.yml
if [[ "$EXTRA_JOBS" != "" ]]; then
echo "extra_jobs:" >> __inputs.yml
echo "$EXTRA_JOBS" >> __inputs.yml
fi
echo "cat __inputs.yml"
cat __inputs.yml
- name: Fetch branches
id: fetch-branches
shell: bash
run: |
# Gets all branches from GitHub API
# https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#list-branches
RESP_CODE=$(curl -w %{http_code} -s -o __installer_branches.json \
-X GET "https://api.github.com/repos/silverstripe/silverstripe-installer/branches?per_page=100" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to read list of branches - HTTP response code was $RESP_CODE"
exit 1
fi
- name: Run php script
id: php-script
shell: bash
run: |
MATRIX_JSON=$(php ${{ github.action_path }}/action.php)
echo "MATRIX_JSON: $MATRIX_JSON"
echo "matrix=${MATRIX_JSON}" >> "$GITHUB_OUTPUT"
- name: Delete temporary files
shell: bash
if: always()
run: |
if [[ -f __installer_branches.json ]]; then
rm __installer_branches.json
fi
if [[ -f __inputs.yml ]]; then
rm __inputs.yml
fi
if [[ -f ${{ github.action_path }}/composer.lock ]]; then
rm ${{ github.action_path }}/composer.lock
fi
if [[ -d ${{ github.action_path }}/vendor ]]; then
rm -rf ${{ github.action_path }}/vendor
fi