From 2a06251b37ddba3062c96acd1d25a7cd6a2c9553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20D=C3=A9siles?= <1536672+jokesterfr@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:36:01 +0100 Subject: [PATCH] Next ! --- .hadolint.yaml | 9 +++ assets/robots.txt | 5 ++ build.sh | 62 +++++++++---------- docker/alpine.Dockerfile | 101 +++++++++++++++++------------- docker/debian.Dockerfile | 130 ++++++++++++++++++++++----------------- lint.sh | 9 +++ 6 files changed, 185 insertions(+), 131 deletions(-) create mode 100644 .hadolint.yaml create mode 100644 assets/robots.txt create mode 100755 lint.sh diff --git a/.hadolint.yaml b/.hadolint.yaml new file mode 100644 index 00000000..0a7a438c --- /dev/null +++ b/.hadolint.yaml @@ -0,0 +1,9 @@ +ignored: + - DL3006 + - DL3008 + - DL3009 + - DL3018 + - DL4006 + - DL4001 + - SC1091 + - SC2086 diff --git a/assets/robots.txt b/assets/robots.txt new file mode 100644 index 00000000..15d1da2c --- /dev/null +++ b/assets/robots.txt @@ -0,0 +1,5 @@ +# www.robotstxt.org/ +# www.google.com/support/webmasters/bin/answer.py?hl=en&answer=156449 + +User-agent: * +Allow: / diff --git a/build.sh b/build.sh index 0c4644f0..dd31d573 100755 --- a/build.sh +++ b/build.sh @@ -1,41 +1,39 @@ #!/bin/bash set -e +cd "$(dirname "$0")" # Available variables # ------------------- -declare PLATFORM; # -- a comma separated list of target platforms (defaults to "linux/amd64") -declare OS_FLAVOUR; # -- either "debian" (default) or "alpine" -declare PHP_VERSION; # -- PHP version, defaults to recommended version for PrestaShop -declare PHP_FLAVOUR; # -- PHP flavour, defaults apache 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="debian"; DEFAULT_SERVER="apache"; DEFAULT_DOCKER_IMAGE=prestashop/prestashop -DEFAULT_PLATFORM=linux/amd64 +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 version of PrestaShop (via GitHub) get_latest_prestashop_version() { - curl --silent --location --request GET \ - 'https://api.github.com/repos/prestashop/prestashop/releases/latest' | jq -r '.tag_name' + 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 from `prestashop-versions.json` -# -# $1 - PrestaShop version -# -# Examples: -# - get_recommended_php_version "8.0.4" get_recommended_php_version() { local PS_VERSION=$1; local RECOMMENDED_VERSION=; @@ -96,6 +94,7 @@ get_php_version() { # # 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 @@ -134,37 +133,36 @@ 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" "$LATEST")" + 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 -# TAGS="--tag $TARGET_IMAGE:nightly"; -# echo "Ready to create: $TARGET_IMAGE:nightly" -#else -# TAGS="--tag $TARGET_IMAGE:$PS_VERSION-$PHP_FLAVOUR --tag $TARGET_IMAGE:latest"; -# echo "Ready to create: $TARGET_IMAGE:$PS_VERSION-$PHP_FLAVOUR" -#fi - -# Info -# ---------------------- -echo "🐳 Use $DEFAULT_DOCKER_IMAGE" -echo "Use PrestaShop $PS_VERSION with PHP $PHP_VERSION on $OS_FLAVOUR" +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 "${PLATFORM:-$DEFAULT_PLATFORM}" \ - --build-arg PHP_VERSION="$PHP_VERSION" \ + --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" \ - --cache-from type=registry,ref="$CACHE_IMAGE" \ - --cache-to type=inline \ + --build-arg ZIP_SOURCE="$ZIP_SOURCE" \ --label org.opencontainers.image.title="PrestaShop" \ --label org.opencontainers.image.description="PrestaShop docker image" \ --label org.opencontainers.image.source=https://github.com/PrestaShop/docker \ diff --git a/docker/alpine.Dockerfile b/docker/alpine.Dockerfile index b455f98f..b3f787b5 100644 --- a/docker/alpine.Dockerfile +++ b/docker/alpine.Dockerfile @@ -1,36 +1,39 @@ ARG PS_VERSION ARG PHP_VERSION ARG PHP_FLAVOUR +ARG GIT_SHA +ARG ZIP_SOURCE # ================================== # Stage 1/3: PHP base configuration # ================================== FROM php:${PHP_FLAVOUR} AS alpine-base-prestashop ARG PS_VERSION +ARG PHP_VERSION ENV PS_DOMAIN="" \ -DB_SERVER="" \ -DB_PORT=3306 \ -DB_NAME=prestashop \ -DB_USER=root \ -DB_PASSWD=admin \ -DB_PREFIX=ps_ \ -ADMIN_MAIL=demo@prestashop.com \ -ADMIN_PASSWD=prestashop_demo \ -PS_LANGUAGE=en \ -PS_COUNTRY=GB \ -PS_ALL_LANGUAGES=0 \ -PS_INSTALL_AUTO=0 \ -PS_ERASE_DB=0 \ -PS_INSTALL_DB=0 \ -PS_DEV_MODE=0 \ -PS_HOST_MODE=0 \ -PS_DEMO_MODE=0 \ -PS_ENABLE_SSL=0 \ -PS_HANDLE_DYNAMIC_DOMAIN=0 \ -PS_FOLDER_ADMIN=admin \ -PS_FOLDER_INSTALL=install \ -PHP_ENV=production + DB_SERVER="" \ + DB_PORT=3306 \ + DB_NAME=prestashop \ + DB_USER=root \ + DB_PASSWD=admin \ + DB_PREFIX=ps_ \ + ADMIN_MAIL=demo@prestashop.com \ + ADMIN_PASSWD=prestashop_demo \ + PS_LANGUAGE=en \ + PS_COUNTRY=GB \ + PS_ALL_LANGUAGES=0 \ + PS_INSTALL_AUTO=0 \ + PS_ERASE_DB=0 \ + PS_INSTALL_DB=0 \ + PS_DEV_MODE=0 \ + PS_HOST_MODE=0 \ + PS_DEMO_MODE=0 \ + PS_ENABLE_SSL=0 \ + PS_HANDLE_DYNAMIC_DOMAIN=0 \ + PS_FOLDER_ADMIN=admin \ + PS_FOLDER_INSTALL=install \ + PHP_ENV=production # The PHP configuration script COPY ./assets/php-configuration.sh /tmp/ @@ -38,10 +41,10 @@ COPY ./assets/php-configuration.sh /tmp/ # Install base tools RUN \ apk --no-cache add -U \ - ca-certificates geoip tzdata zip curl jq make \ - gnu-libiconv php-common mariadb-client oniguruma-dev \ - zlib-dev libzip-dev libjpeg-turbo-dev libpng-dev \ - icu-dev libmcrypt-dev libxml2 libxml2-dev \ + ca-certificates geoip tzdata zip curl jq make \ + gnu-libiconv php-common mariadb-client oniguruma-dev \ + zlib-dev libzip-dev libjpeg-turbo-dev libpng-dev \ + icu-dev libmcrypt-dev libxml2 libxml2-dev \ && /tmp/php-configuration.sh \ && apk del make \ && rm -rf /var/cache/apk/* @@ -59,42 +62,56 @@ COPY ./assets/docker_run.sh /tmp/ # Apache configuration RUN if [ -x "$(command -v apache2-foreground)" ]; then \ - a2enmod rewrite;\ - fi + a2enmod rewrite;\ + fi # ========================================= # Stage 2/3: PrestaShop sources downloader # ========================================= FROM alpine-base-prestashop AS alpine-download-prestashop ARG PS_VERSION -ENV PS_FOLDER=/tmp/prestashop +ARG GIT_SHA +ARG PHP_VERSION +ARG PS_FOLDER=/var/www/html +ARG ZIP_SOURCE # Get PrestaShop source code -RUN if [[ "$PS_VERSION" == "nightly" ]]; then \ - echo "Unsupported yet: https://prestashop.slack.com/archives/C03LFE4KV6K/p1703170152828039" \ - && exit 1; \ - else \ - curl -s -L -o /tmp/prestashop.zip "https://github.com/PrestaShop/PrestaShop/releases/download/${PS_VERSION}/prestashop_${PS_VERSION}.zip"; \ - fi +# hadolint ignore=DL3020 +ADD ${ZIP_SOURCE} /tmp/prestashop.zip # Extract the souces RUN mkdir -p "$PS_FOLDER" /tmp/unzip-ps \ && unzip -n -q /tmp/prestashop.zip -d /tmp/unzip-ps \ && ([ -f /tmp/unzip-ps/prestashop.zip ] \ - && unzip -n -q /tmp/unzip-ps/prestashop.zip -d "$PS_FOLDER" \ - || mv /tmp/unzip-ps/prestashop/* "$PS_FOLDER") + && unzip -n -q /tmp/unzip-ps/prestashop.zip -d "$PS_FOLDER" \ + || mv /tmp/unzip-ps/prestashop/* "$PS_FOLDER") \ + && chown -R www-data:www-data "$PS_FOLDER" \ + && rm -rf /tmp/prestashop.zip /tmp/unzip-ps + +# Ship a VERSION file +RUN echo "PrestaShop $PS_VERSION" > "$PS_FOLDER/VERSION" \ + && echo "PHP $PHP_VERSION" >> "$PS_FOLDER/VERSION" \ + && echo "Flashlight $GIT_SHA" >> "$PS_FOLDER/VERSION" + +# Adds a robots.txt file +ADD ./assets/robots.txt $PS_FOLDER # ============================ # Stage 3/3: Production image # ============================ FROM alpine-base-prestashop -ARG PS_FOLDER=/var/www/html/prestashop +ARG PS_FOLDER=/var/www/html +ARG PS_VERSION LABEL maintainer="PrestaShop Core Team " -ENV PS_VERSION $PS_VERSION +COPY --chown=www-data:www-data --from=alpine-download-prestashop ${PS_FOLDER} ${PS_FOLDER} + +HEALTHCHECK --interval=5s --timeout=5s --retries=10 --start-period=10s \ + CMD curl -Isf http://localhost:80/robots.txt || exit 1 + +EXPOSE 80 -# Copy the PrestaShop sources -COPY --chown=www-data:www-data --from=alpine-download-prestashop /tmp/prestashop ${PS_FOLDER} +STOPSIGNAL SIGQUIT -CMD ["/tmp/docker_run.sh"] +ENTRYPOINT ["/tmp/docker_run.sh"] diff --git a/docker/debian.Dockerfile b/docker/debian.Dockerfile index 5ab85dda..ac890878 100644 --- a/docker/debian.Dockerfile +++ b/docker/debian.Dockerfile @@ -1,6 +1,8 @@ ARG PS_VERSION ARG PHP_VERSION ARG PHP_FLAVOUR +ARG GIT_SHA +ARG ZIP_SOURCE # ================================== # Stage 1/3: PHP base configuration @@ -9,52 +11,52 @@ FROM php:${PHP_FLAVOUR} AS debian-base-prestashop ARG PS_VERSION ENV PS_DOMAIN="" \ -DB_SERVER="" \ -DB_PORT=3306 \ -DB_NAME=prestashop \ -DB_USER=root \ -DB_PASSWD=admin \ -DB_PREFIX=ps_ \ -ADMIN_MAIL=demo@prestashop.com \ -ADMIN_PASSWD=prestashop_demo \ -PS_LANGUAGE=en \ -PS_COUNTRY=GB \ -PS_ALL_LANGUAGES=0 \ -PS_INSTALL_AUTO=0 \ -PS_ERASE_DB=0 \ -PS_INSTALL_DB=0 \ -PS_DEV_MODE=0 \ -PS_HOST_MODE=0 \ -PS_DEMO_MODE=0 \ -PS_ENABLE_SSL=0 \ -PS_HANDLE_DYNAMIC_DOMAIN=0 \ -PS_FOLDER_ADMIN=admin \ -PS_FOLDER_INSTALL=install \ -PHP_ENV=production + DB_SERVER="" \ + DB_PORT=3306 \ + DB_NAME=prestashop \ + DB_USER=root \ + DB_PASSWD=admin \ + DB_PREFIX=ps_ \ + ADMIN_MAIL=demo@prestashop.com \ + ADMIN_PASSWD=prestashop_demo \ + PS_LANGUAGE=en \ + PS_COUNTRY=GB \ + PS_ALL_LANGUAGES=0 \ + PS_INSTALL_AUTO=0 \ + PS_ERASE_DB=0 \ + PS_INSTALL_DB=0 \ + PS_DEV_MODE=0 \ + PS_HOST_MODE=0 \ + PS_DEMO_MODE=0 \ + PS_ENABLE_SSL=0 \ + PS_HANDLE_DYNAMIC_DOMAIN=0 \ + PS_FOLDER_ADMIN=admin \ + PS_FOLDER_INSTALL=install \ + PHP_ENV=production # The PHP configuration script COPY ./assets/php-configuration.sh /tmp/ RUN export DEBIAN_FRONTEND=noninteractive \ - && apt-get update \ - && apt-get install --no-install-recommends -qqy \ - default-mysql-client \ - jq \ - libfreetype6-dev \ - libicu-dev \ - libjpeg62-turbo-dev \ - libmcrypt-dev \ - libonig-dev \ - libpcre3-dev \ - libpng-dev \ - libwebp-dev \ - libxml2-dev \ - libzip-dev \ - unzip \ - wget \ - && /tmp/php-configuration.sh \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* + && apt-get update \ + && apt-get install --no-install-recommends -qqy \ + default-mysql-client \ + jq \ + libfreetype6-dev \ + libicu-dev \ + libjpeg62-turbo-dev \ + libmcrypt-dev \ + libonig-dev \ + libpcre3-dev \ + libpng-dev \ + libwebp-dev \ + libxml2-dev \ + libzip-dev \ + unzip \ + wget \ + && /tmp/php-configuration.sh \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* # The PrestaShop docker entrypoint COPY ./assets/docker_run.sh /tmp/ @@ -69,42 +71,56 @@ COPY ./assets/docker_run.sh /tmp/ # Apache configuration RUN if [ -x "$(command -v apache2-foreground)" ]; then\ - a2enmod rewrite;\ - fi + a2enmod rewrite;\ + fi # ========================================= # Stage 2/3: PrestaShop sources downloader # ========================================= FROM debian-base-prestashop AS debian-download-prestashop ARG PS_VERSION -ENV PS_FOLDER=/tmp/prestashop +ARG GIT_SHA +ARG PHP_VERSION +ARG PS_FOLDER=/var/www/html +ARG ZIP_SOURCE # Get PrestaShop source code -RUN if [[ "$PS_VERSION" == "nightly" ]]; then \ - echo "Unsupported yet: https://prestashop.slack.com/archives/C03LFE4KV6K/p1703170152828039" \ - && exit 1; \ - else \ - curl -s -L -o /tmp/prestashop.zip "https://github.com/PrestaShop/PrestaShop/releases/download/${PS_VERSION}/prestashop_${PS_VERSION}.zip"; \ - fi +# hadolint ignore=DL3020 +ADD ${ZIP_SOURCE} /tmp/prestashop.zip # Extract the souces RUN mkdir -p "$PS_FOLDER" /tmp/unzip-ps \ && unzip -n -q /tmp/prestashop.zip -d /tmp/unzip-ps \ && ([ -f /tmp/unzip-ps/prestashop.zip ] \ - && unzip -n -q /tmp/unzip-ps/prestashop.zip -d "$PS_FOLDER" \ - || mv /tmp/unzip-ps/prestashop/* "$PS_FOLDER") + && unzip -n -q /tmp/unzip-ps/prestashop.zip -d "$PS_FOLDER" \ + || mv /tmp/unzip-ps/prestashop/* "$PS_FOLDER") \ + && chown -R www-data:www-data "$PS_FOLDER" \ + && rm -rf /tmp/prestashop.zip /tmp/unzip-ps + +# Ship a VERSION file +RUN echo "PrestaShop $PS_VERSION" > "$PS_FOLDER/VERSION" \ + && echo "PHP $PHP_VERSION" >> "$PS_FOLDER/VERSION" \ + && echo "Flashlight $GIT_SHA" >> "$PS_FOLDER/VERSION" + +# Adds a robots.txt file +ADD ./assets/robots.txt $PS_FOLDER # ============================ # Stage 3/3: Production image # ============================ FROM debian-base-prestashop -ARG PS_FOLDER=/var/www/html/prestashop +ARG PS_FOLDER=/var/www/html +ARG PS_VERSION LABEL maintainer="PrestaShop Core Team " -ENV PS_VERSION $PS_VERSION +COPY --chown=www-data:www-data --from=debian-download-prestashop ${PS_FOLDER} ${PS_FOLDER} + +HEALTHCHECK --interval=5s --timeout=5s --retries=10 --start-period=10s \ + CMD curl -Isf http://localhost:80/robots.txt || exit 1 + +EXPOSE 80 -# Copy the PrestaShop sources -COPY --chown=www-data:www-data --from=debian-download-prestashop /tmp/prestashop ${PS_FOLDER} +STOPSIGNAL SIGQUIT -CMD ["/tmp/docker_run.sh"] +ENTRYPOINT ["/tmp/docker_run.sh"] diff --git a/lint.sh b/lint.sh new file mode 100755 index 00000000..8d3265cf --- /dev/null +++ b/lint.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -e +cd "$(dirname "$0")" + +# Lint bash scripts +find . -type f -name '*.sh' -print0 | xargs -0 shellcheck -x; + +# Lint docker files +find . -type f -name '*.Dockerfile' -print0 | xargs -0 hadolint;