-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
entrypoint.sh
executable file
·269 lines (222 loc) · 9.18 KB
/
entrypoint.sh
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
set -e
set -o pipefail
function help {
echo "$0 <JSON>"
echo ""
echo "Run a QA JOB as specified in a JSON object."
echo "The JSON should include each of the following elements:"
echo " - command: command to run"
echo " - php: the PHP version to use"
echo " - extensions: a list of additional extensions to enable"
echo " - ini: a list of php.ini directives"
echo " - dependencies: the dependency set to run against (lowest, latest, locked)"
echo " - ignore_php_platform_requirement: flag to enable/disable the PHP platform requirement when executing composer \`install\` or \`update\`"
echo " - additional_composer_arguments: a list of composer arguments to be added when \`install\` or \`update\` is called."
echo " - before_script: a list of commands to run before the real command"
echo " - after_script: a list of commands to run after the real command"
echo ""
}
function checkout {
local REF=
local LOCAL_BRANCH=
local LOCAL_BRANCH_NAME=
if [[ ! $GITHUB_EVENT_NAME || ! $GITHUB_REPOSITORY || ! $GITHUB_REF ]];then
return
fi
LOCAL_BRANCH_NAME=$GITHUB_HEAD_REF
case $GITHUB_EVENT_NAME in
pull_request)
REF=$GITHUB_REF
LOCAL_BRANCH=$GITHUB_HEAD_REF
BASE_BRANCH=$GITHUB_BASE_REF
if [[ ! $LOCAL_BRANCH || ! $BASE_BRANCH ]]; then
echo "Missing head or base ref env variables; aborting"
exit 1
fi
LOCAL_BRANCH_NAME=pull/${LOCAL_BRANCH_NAME}
;;
push)
REF=${GITHUB_REF/refs\/heads\//}
LOCAL_BRANCH=${REF}
;;
tag)
REF=${GITHUB_REF/refs\/tags\//}
LOCAL_BRANCH=${REF}
;;
*)
echo "Unable to handle events of type $GITHUB_EVENT_NAME; aborting"
exit 1
esac
if [ -d ".git" ];then
echo "Updating and fetching from canonical repository"
if [[ $(git remote) =~ origin ]];then
git remote remove origin
fi
git remote add origin https://github.com/"${GITHUB_REPOSITORY}"
git fetch origin
else
echo "Cloning repository"
git clone https://github.com/"${GITHUB_REPOSITORY}" .
fi
if [[ "$REF" == "$LOCAL_BRANCH" ]];then
echo "Checking out ref ${REF}"
git checkout "$REF"
else
echo "Checking out branch ${BASE_BRANCH}"
git checkout "${BASE_BRANCH}"
echo "Fetching ref ${REF}"
git fetch origin "${REF}":"${LOCAL_BRANCH_NAME}"
echo "Checking out target ref to ${LOCAL_BRANCH_NAME}"
git checkout "${LOCAL_BRANCH_NAME}"
fi
}
function composer_install_dependencies {
local DEPS=$1
local IGNORE_PHP_PLATFORM_REQUIREMENT=$2
local ADDITIONAL_COMPOSER_ARGUMENTS=$3
local COMPOSER_ARGS="--ansi --no-interaction --no-progress ${ADDITIONAL_COMPOSER_ARGUMENTS}"
if [[ "${IGNORE_PHP_PLATFORM_REQUIREMENT}" == "true" ]];then
COMPOSER_ARGS="${COMPOSER_ARGS} --ignore-platform-req=php"
fi
case $DEPS in
lowest)
echo "Installing lowest supported dependencies via Composer"
# Disable platform.php, if set
composer config --unset platform.php
# shellcheck disable=SC2086
composer update ${COMPOSER_ARGS} --prefer-lowest
;;
latest)
echo "Installing latest supported dependencies via Composer"
# Disable platform.php, if set
composer config --unset platform.php
# shellcheck disable=SC2086
composer update ${COMPOSER_ARGS}
;;
*)
echo "Installing dependencies as specified in lockfile via Composer"
# shellcheck disable=SC2086
composer install ${COMPOSER_ARGS}
;;
esac
composer show
}
if [ $# -ne 1 ]; then
echo "Missing or extra arguments; expects a single JSON string with job information"
echo ""
help
exit 1
fi
JOB=$1
echo "Received job: ${JOB}"
COMMAND=$(echo "${JOB}" | jq -r '.command // ""')
if [[ "${COMMAND}" == "" ]];then
echo "Command is empty; nothing to run"
exit 0
fi
PHP=$(echo "${JOB}" | jq -r '.php // ""')
if [[ "${PHP}" == "" ]];then
echo "Missing PHP version in job"
help
exit 1
fi
RECONFIGURE_PHP_DEFAULT="yes"
# If the default PHP version from the container is requested, we do not reconfigure PHP version
if [[ "${PHP}" == "@default" ]]; then
RECONFIGURE_PHP_DEFAULT="no"
PHP=$(php -nr "echo PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;")
fi
if [[ "${COMMAND}" =~ "^roave-backward-compatibility-check" ]] || [[ "${COMMAND}" =~ "^/usr/bin/env roave-backward-compatibility-check" ]] || [[ "${COMMAND}" =~ "^/usr/local/bin/roave-backward-compatibility-check" ]]; then
echo "NOTE: Due to the execution of \"roave-backward-compatibility-check\" from within this container, the PHP version won't get changed.";
RECONFIGURE_PHP_DEFAULT="no"
fi
declare -a BEFORE_SCRIPT=()
readarray -t BEFORE_SCRIPT < <(echo "${JOB}" | jq -rc '(.before_script // [])[]' )
declare -a AFTER_SCRIPT=()
readarray -t AFTER_SCRIPT < <(echo "${JOB}" | jq -rc '(.after_script // [])[]' )
if [[ "${RECONFIGURE_PHP_DEFAULT}" == "yes" ]]; then
echo "Marking PHP ${PHP} as configured default"
update-alternatives --quiet --set php "/usr/bin/php${PHP}"
update-alternatives --quiet --set php-config "/usr/bin/php-config${PHP}"
update-alternatives --quiet --set phpize "/usr/bin/phpize${PHP}"
update-alternatives --quiet --set phpdbg "/usr/bin/phpdbg${PHP}"
fi
# Marks the working directory as safe for the current user prior to checkout
git config --global --add safe.directory '*'
checkout
# Is there a pre-install script available?
if [ -x ".laminas-ci/pre-install.sh" ];then
echo "Executing pre-install commands from .laminas-ci/pre-install.sh"
./.laminas-ci/pre-install.sh testuser "${PWD}" "${JOB}" "${PHP}"
fi
EXTENSIONS=$(echo "${JOB}" | jq -r ".extensions // [] | join(\" \")")
INI=$(echo "${JOB}" | jq -r '.ini // [] | join("\n")')
DEPS=$(echo "${JOB}" | jq -r '.dependencies // "locked"')
IGNORE_PLATFORM_REQS_ON_8=$(echo "${JOB}" | jq -r 'if has("ignore_platform_reqs_8") | not then "yes" elif .ignore_platform_reqs_8 then "yes" else "no" end')
IGNORE_PHP_PLATFORM_REQUIREMENT=$(echo "${JOB}" | jq -r '.ignore_php_platform_requirement')
ADDITIONAL_COMPOSER_ARGUMENTS=$(echo "${JOB}" | jq -r '.additional_composer_arguments // [] | join("\n")')
# Old matrix generation
if [ "${IGNORE_PHP_PLATFORM_REQUIREMENT}" == "null" ]; then
IGNORE_PHP_PLATFORM_REQUIREMENT="false"
# Provide BC compatibility
if [ "${IGNORE_PLATFORM_REQS_ON_8}" == "yes" ] && [[ "${PHP}" =~ ^8 ]]; then
IGNORE_PHP_PLATFORM_REQUIREMENT="true"
fi
fi
if [[ "${EXTENSIONS}" != "" ]];then
/scripts/extensions.sh "${PHP}" "${EXTENSIONS}"
fi
if [[ "${INI}" != "" ]];then
echo "Installing php.ini settings"
echo "$INI" > "/etc/php/${PHP}/cli/conf.d/99-settings.ini"
echo "$INI" > "/etc/php/${PHP}/phpdbg/conf.d/99-settings.ini"
fi
echo "PHP version: $(php --version)"
echo "Installed extensions:"
/usr/local/bin/php-extensions-with-version.php
# If a token is present, tell Composer about it so we can avoid rate limits
if [[ "${GITHUB_TOKEN}" != "" ]];then
composer config --global github-oauth.github.com "${GITHUB_TOKEN}"
fi
composer_install_dependencies "${DEPS}" "${IGNORE_PHP_PLATFORM_REQUIREMENT}" "${ADDITIONAL_COMPOSER_ARGUMENTS}"
if [[ "${COMMAND}" =~ phpunit ]];then
echo "Setting up PHPUnit problem matcher"
cp /etc/laminas-ci/problem-matcher/phpunit.json "$(pwd)/phpunit.json"
echo "::add-matcher::phpunit.json"
fi
if [[ "${COMMAND}" =~ markdownlint ]];then
echo "Setting up markdownlint problem matcher"
cp /etc/laminas-ci/problem-matcher/markdownlint.json "$(pwd)/markdownlint-matcher.json"
echo "::add-matcher::markdownlint-matcher.json"
if [ ! -f ".markdownlint.json" ];then
echo "Installing markdownlint configuration"
cp /etc/laminas-ci/markdownlint.json .markdownlint.json
fi
fi
chown -R testuser .
# Is there a pre-run script available?
if [ -x ".laminas-ci/pre-run.sh" ];then
echo "Executing pre-run commands from .laminas-ci/pre-run.sh"
./.laminas-ci/pre-run.sh testuser "${PWD}" "${JOB}" "${PHP}"
fi
for BEFORE_SCRIPT_COMMAND in "${BEFORE_SCRIPT[@]}"; do
echo "Running before_script: ${BEFORE_SCRIPT_COMMAND}"
sudo --preserve-env --set-home -u testuser /bin/bash -c "${BEFORE_SCRIPT_COMMAND}"
done
# Disable exit-on-non-zero flag so we can run post-commands
set +e
echo "Running ${COMMAND}"
sudo --preserve-env --set-home -u testuser /bin/bash -c "${COMMAND}"
STATUS=$?
set -e
# Is there a post-run script available?
if [ -x ".laminas-ci/post-run.sh" ];then
echo "Executing post-run commands from .laminas-ci/post-run.sh"
./.laminas-ci/post-run.sh "${STATUS}" testuser "${PWD}" "${JOB}" "${PHP}"
fi
for AFTER_SCRIPT_COMMAND in "${AFTER_SCRIPT[@]}"; do
echo "Running before_script: ${AFTER_SCRIPT_COMMAND}"
sudo --preserve-env --set-home -u testuser /bin/bash -c "${AFTER_SCRIPT_COMMAND}"
done
exit ${STATUS}