-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction.yml
145 lines (137 loc) · 5.76 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
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
js:
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@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
with:
fetch-depth: ${{ steps.set-fetch-depth.outputs.fetch-depth }}
- name: Install PHP
uses: shivammathur/setup-php@1a18b2267f80291a81ca1d33e7c851fe09e7dfc4 # v2.22.0
with:
php-version: '8.1'
extensions: yaml
- 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 "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: 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"