-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild
More file actions
executable file
·152 lines (140 loc) · 5.46 KB
/
build
File metadata and controls
executable file
·152 lines (140 loc) · 5.46 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
# shellcheck source=/dev/null
. generate-layer.sh
set -eo pipefail
echo "---> PHP Buildpack"
# 1. GET ARGS
layers_dir=$1
plan_path=$3
bp_dir=$(
cd "$(dirname "$0")"/..
pwd
)
arch=$(dpkg --print-architecture) # amd64 arm64
# 2. DOWNLOAD PHP
php_layer_dir="${layers_dir}/php"
mkdir -p "${php_layer_dir}"/config
extensions_layer_dir="${layers_dir}/extensions"
mkdir -p "${extensions_layer_dir}"/{docs,ext}
# determine php version provided during detection
php_version=$(yj <"${plan_path}" -t | jq -r '.entries[] | select(.name == "php") | .version')
remote_php_version="not found"
if [[ -f "${php_layer_dir}.toml" ]]; then
remote_php_version=$(yj <"${php_layer_dir}.toml" -t | jq -r .metadata 2>/dev/null || echo 'not found')
fi
if [[ "${php_version}" == "${remote_php_version}" ]]; then
echo "---> Reusing php"
else
echo "---> Downloading and extracting php ${php_version} ${arch}"
php_url="https://buildpacks.drycc.cc/php/${CNB_STACK_ID}/php-${php_version}-${arch}.tar.gz"
wget -q -O - "${php_url}" | tar -xzf - -C "${php_layer_dir}"
rm -f "php-${php_version}-${arch}.tar.gz"
cp "${php_layer_dir}/sbin/php-fpm" "${php_layer_dir}/bin"
"${php_layer_dir}"/bin/php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
"${php_layer_dir}"/bin/php composer-setup.php --install-dir="${php_layer_dir}/bin/"
mv "${php_layer_dir}/bin/composer.phar" "${php_layer_dir}/bin/composer"
cat >"${php_layer_dir}.toml" <<EOL
cache = true
build = true
launch = true
metadata = "${php_version}"
EOL
if [ ! -s "${php_layer_dir}/profile.d/php.sh" ]; then
mkdir "${php_layer_dir}/profile.d"
cat >"${php_layer_dir}/profile.d/php.sh" <<EOL
export PATH="${php_layer_dir}/bin:${php_layer_dir}/sbin:${PATH}"
export PHP_PEAR_INSTALL_DIR="${php_layer_dir}/lib/php"
export PHP_PEAR_PHP_BIN="${php_layer_dir}/bin/php"
export php_layer_dir="${php_layer_dir}"
export php_version="${php_version}"
echo "Config pecl"
pecl config-set php_dir "${php_layer_dir}"
pecl config-set bin_dir "${php_layer_dir}"/bin
pecl config-set ext_dir "${php_layer_dir}"/ext
pecl config-set doc_dir "${extensions_layer_dir}"/docs
EOL
fi
echo "---> Config php-fpm"
cp -f "${bp_dir}/config/php/"* "${php_layer_dir}/config/"
cp -f "${bp_dir}"/lib/* "${php_layer_dir}/bin/"
mv -f "${php_layer_dir}"/bin/Builder.php "${php_layer_dir}"/lib/php/PEAR/Builder.php
fi
# 3. MAKE PHP AVAILABLE TO THIS SCRIPT
# shellcheck source=/dev/null
. "${php_layer_dir}/profile.d/php.sh"
# Compares previous extensions.json checksum to the current extensions.json
local_extensions_checksum="$(sha256sum extensions.json | cut -d ' ' -f 1 || echo 'not found')"
remote_extensions_checksum="not found"
if [[ -f "${extensions_layer_dir}.toml" ]]; then
remote_extensions_checksum=$(yj <"${extensions_layer_dir}.toml" -t | jq -r .metadata 2>/dev/null || echo 'not found')
fi
if [[ -f extensions.json && "${local_extensions_checksum}" == "${remote_extensions_checksum}" ]]; then
echo "---> Reusing extensions.json"
elif [[ -f extensions.json ]]; then
echo "---> Config pecl"
pecl config-set php_dir "${php_layer_dir}"
pecl config-set bin_dir "${php_layer_dir}"/bin
pecl config-set ext_dir "${php_layer_dir}"/ext
pecl config-set doc_dir "${extensions_layer_dir}"/docs
echo "---> Installing extensions.json"
rm "${php_layer_dir}"/ext/* -rf
cp -f "${bp_dir}/config/php/php.ini" "${php_layer_dir}/config/"
# shellcheck disable=SC1090
source "$bp_dir/bin/ext"
ext_install
cat >"${extensions_layer_dir}.toml" <<EOL
cache = true
build = true
launch = true
metadata = "${local_extensions_checksum}"
EOL
fi
# Compares previous composer.json checksum to the current composer.json
vendor_layer_dir="${layers_dir}/vendor"
mkdir -p "${vendor_layer_dir}"
local_composer_checksum=$(sha256sum composer.json | cut -d ' ' -f 1 || echo 'not found')
remote_composer_checksum="not found"
if [[ -f "${vendor_layer_dir}.toml" ]]; then
remote_composer_checksum=$(yj <"${vendor_layer_dir}.toml" -t | jq -r .metadata 2>/dev/null || echo 'not found')
fi
composer config --no-plugins vendor-dir
composer config --no-plugins bin-dir
if [[ -f composer.json && "${local_composer_checksum}" == "${remote_composer_checksum}" ]]; then
echo "---> Reusing composer.json"
cp -r "${vendor_layer_dir}" "./vendor"
else
echo "---> Installing composer.json"
composer install --ignore-platform-reqs
if [[ -d "${vendor_layer_dir}" ]]; then
rm "${vendor_layer_dir:?}/*" -rf
cp -r vendor/. "${vendor_layer_dir}/"
fi
cat >"${vendor_layer_dir}.toml" <<EOL
cache = true
build = true
launch = true
metadata = "${local_composer_checksum}"
EOL
fi
echo "---> Config nginx"
nginx_layer_dir="${layers_dir}/nginx"
mkdir -p "${layers_dir}/nginx/"logs
cp -r /etc/nginx/* "${nginx_layer_dir}"
cp "${bp_dir}/config/nginx/base.conf" "${nginx_layer_dir}/nginx-php.conf.tpl"
cat >"${nginx_layer_dir}.toml" <<EOL
cache = true
build = true
launch = true
EOL
cat >"${php_layer_dir}/bin/php-nginx-fpm" <<SH
#!/usr/bin/env bash
mkdir -p "${layers_dir}/log" /tmp/nginx/logs/
touch "${layers_dir}/log/php-fpm.log"
cat "${nginx_layer_dir}/nginx-php.conf.tpl" > "${nginx_layer_dir}/nginx-php.conf"
sed -i "s|@ROOT@|\$(readlink -f \$1)|g" "${nginx_layer_dir}/nginx-php.conf"
sed -i "s|@PORT@|\$2|g" "${nginx_layer_dir}/nginx-php.conf"
php-fpm -c "${php_layer_dir}/config/php.ini" -y "${php_layer_dir}/config/php-fpm.conf" -p "${php_layer_dir}"
nginx -p "${nginx_layer_dir}" -c "${nginx_layer_dir}/nginx-php.conf"
SH
chmod +x "${php_layer_dir}/bin/php-nginx-fpm"
generate-launch.sh "${layers_dir}"