-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.sh
executable file
·180 lines (168 loc) · 6.42 KB
/
build.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
#!/bin/bash
set -e
cd "$(dirname "$0")"
# Available variables
# -------------------
declare PS_VERSION; # -- PrestaShop version, defaults to latest
declare PHP_VERSION; # -- PHP version, defaults to recommended version for PrestaShop
declare OS_FLAVOUR; # -- either "alpine" (default) or "debian"
declare SERVER_FLAVOUR; # -- not implemented, either "nginx" (default) or "apache"
declare TARGET_PLATFORM; # -- a comma separated list of target platforms (defaults to "linux/amd64")
declare PLATFORM; # -- alias for $TARGET_PLATFORM
declare TARGET_IMAGE; # -- docker image name, defaults to "prestashop/prestashop-flashlight"
declare PUSH; # -- set it to "true" if you want to push the resulting image
declare ZIP_SOURCE; # -- the zip to unpack in flashlight
declare DRY_RUN; # -- if used, won't really build the image. Useful to check tags compliance
# Static configuration
# --------------------
DEFAULT_OS="alpine";
DEFAULT_SERVER="nginx";
DEFAULT_DOCKER_IMAGE=prestashop/prestashop-flashlight
DEFAULT_PLATFORM=$(docker system info --format '{{.OSType}}/{{.Architecture}}')
GIT_SHA=$(git rev-parse HEAD)
TARGET_PLATFORM="${TARGET_PLATFORM:-${PLATFORM:-$DEFAULT_PLATFORM}}"
error() {
echo -e "\e[1;31m${1:-Unknown error}\e[0m"
exit "${2:-1}"
}
get_latest_prestashop_version() {
curl --silent --show-error --fail --location --request GET \
'https://api.github.com/repos/prestashop/prestashop/releases/latest' | jq -r '.tag_name'
}
get_recommended_php_version() {
local PS_VERSION=$1;
local RECOMMENDED_VERSION=;
REGEXP_LIST=$(jq -r 'keys_unsorted | .[]' <prestashop-versions.json)
while IFS= read -r regExp; do
if [[ $PS_VERSION =~ $regExp ]]; then
RECOMMENDED_VERSION=$(jq -r '."'"${regExp}"'".php.recommended' <prestashop-versions.json)
break;
fi
done <<<"$REGEXP_LIST"
echo "$RECOMMENDED_VERSION";
}
get_recommended_nodejs_version() {
local PS_VERSION=$1;
local RECOMMENDED_VERSION=;
REGEXP_LIST=$(jq -r 'keys_unsorted | .[]' <prestashop-versions.json)
while IFS= read -r regExp; do
if [[ $PS_VERSION =~ $regExp ]]; then
RECOMMENDED_VERSION=$(jq -r '."'"${regExp}"'" | if has("nodejs") then .nodejs.recommended else "0.0.0" end' <prestashop-versions.json)
break;
fi
done <<<"$REGEXP_LIST"
echo "$RECOMMENDED_VERSION";
}
get_php_flavour() {
local OS_FLAVOUR=${1:-};
local SERVER_FLAVOUR=${2:-};
local PHP_VERSION=${3:-};
jq -r '."'"${PHP_VERSION}"'".'"${OS_FLAVOUR}" <php-flavours.json;
}
get_ps_version() {
local PS_VERSION=${1:-};
if [ -z "$PS_VERSION" ] || [ "$PS_VERSION" == "latest" ] ; then
get_latest_prestashop_version;
else
echo "$PS_VERSION";
fi
}
get_php_version() {
local PHP_VERSION=${1:-};
local PS_VERSION=${2:-};
if [ -z "$PHP_VERSION" ] || [ "$PHP_VERSION" == "latest" ] ; then
get_recommended_php_version "$PS_VERSION"
else
echo "$PHP_VERSION";
fi
}
#
# if the build is for the latest image of the default OS with the recommended PHP version, these tags will be like:
# * latest
# * php-8.2
# * 8.1.1
# * 8.1.1-8.2
# * 8.1.1-8.2-alpine
#
get_target_images() {
local PHP_FLAVOUR=${1:-};
local PS_VERSION=${2:-};
local PHP_VERSION=${3:-};
local OS_FLAVOUR=${4:-};
declare RES;
if [ "$PS_VERSION" == "nightly" ]; then
if [ "$OS_FLAVOUR" = "$DEFAULT_OS" ]; then
RES="-t ${DEFAULT_DOCKER_IMAGE}:nightly";
else
RES="-t ${DEFAULT_DOCKER_IMAGE}:nightly-${OS_FLAVOUR}";
fi
else
if [ "$PS_VERSION" = "$(get_latest_prestashop_version)" ] && [ "$OS_FLAVOUR" = "$DEFAULT_OS" ] && [ "$PHP_VERSION" = "$(get_recommended_php_version "$PS_VERSION")" ]; then
RES="-t ${DEFAULT_DOCKER_IMAGE}:latest";
fi
if [ "$OS_FLAVOUR" = "$DEFAULT_OS" ]; then
RES="${RES} -t ${DEFAULT_DOCKER_IMAGE}:${PS_VERSION}-${PHP_VERSION}";
if [ "$PHP_VERSION" = "$(get_recommended_php_version "$PS_VERSION")" ]; then
RES="${RES} -t ${DEFAULT_DOCKER_IMAGE}:${PS_VERSION}";
RES="${RES} -t ${DEFAULT_DOCKER_IMAGE}:php-${PHP_VERSION}";
fi
fi
RES="${RES} -t ${DEFAULT_DOCKER_IMAGE}:${PS_VERSION}-${PHP_FLAVOUR}";
RES="${RES} -t ${DEFAULT_DOCKER_IMAGE}:${PS_VERSION}-${OS_FLAVOUR}";
fi
echo "$RES";
}
# Applying configuration
# ----------------------
PS_VERSION=$(get_ps_version "$PS_VERSION");
PHP_VERSION=$(get_php_version "$PHP_VERSION" "$PS_VERSION");
if [ -z "$PHP_VERSION" ]; then
error "Could not find a recommended PHP version for PS_VERSION: $PS_VERSION" 2
fi
OS_FLAVOUR=${OS_FLAVOUR:-$DEFAULT_OS};
SERVER_FLAVOUR=${SERVER_FLAVOUR:-$DEFAULT_SERVER};
PHP_FLAVOUR=$(get_php_flavour "$OS_FLAVOUR" "$SERVER_FLAVOUR" "$PHP_VERSION");
NODE_VERSION=$(get_recommended_nodejs_version "$PS_VERSION");
if [ "$PHP_FLAVOUR" == "null" ]; then
error "Could not find a PHP flavour for $OS_FLAVOUR + $SERVER_FLAVOUR + $PHP_VERSION" 2;
fi
if [ -z "${TARGET_IMAGE:+x}" ]; then
read -ra TARGET_IMAGES <<<"$(get_target_images "$PHP_FLAVOUR" "$PS_VERSION" "$PHP_VERSION" "$OS_FLAVOUR")"
else
read -ra TARGET_IMAGES <<<"-t $TARGET_IMAGE"
fi
if [ "$PS_VERSION" == "nightly" ]; then
ZIP_SOURCE="https://storage.googleapis.com/prestashop-core-nightly/nightly.zip"
else
ZIP_SOURCE="https://github.com/PrestaShop/PrestaShop/releases/download/${PS_VERSION}/prestashop_${PS_VERSION}.zip"
fi
# Build the docker image
# ----------------------
CACHE_IMAGE=${TARGET_IMAGES[1]}
if [ -n "${DRY_RUN}" ]; then
docker() {
echo docker "$@"
}
fi
docker pull "$CACHE_IMAGE" 2> /dev/null || true
docker buildx build \
--progress=plain \
--file "./docker/${OS_FLAVOUR}.Dockerfile" \
--platform "$TARGET_PLATFORM" \
--cache-from type=registry,ref="$CACHE_IMAGE" \
--cache-to type=inline \
--build-arg PHP_FLAVOUR="$PHP_FLAVOUR" \
--build-arg PS_VERSION="$PS_VERSION" \
--build-arg PHP_VERSION="$PHP_VERSION" \
--build-arg GIT_SHA="$GIT_SHA" \
--build-arg NODE_VERSION="$NODE_VERSION" \
--build-arg ZIP_SOURCE="$ZIP_SOURCE" \
--label org.opencontainers.image.title="PrestaShop Flashlight" \
--label org.opencontainers.image.description="PrestaShop Flashlight testing utility" \
--label org.opencontainers.image.source=https://github.com/PrestaShop/prestashop-flashlight \
--label org.opencontainers.image.url=https://hub.docker.com/r/prestashop/prestashop-flashlight \
--label org.opencontainers.image.licenses=MIT \
--label org.opencontainers.image.created="$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")" \
"${TARGET_IMAGES[@]}" \
"$([ "${PUSH}" == "true" ] && echo "--push" || echo "--load")" \
.