From e5610b36f74ed2b93dcf11416e2e9591deae4a3f Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Mon, 11 Mar 2024 16:01:22 +0100 Subject: [PATCH] feat: add shopware (#9) --- .github/workflows/build-shopware.yml | 53 ++++++++ README.md | 20 +++ images/shopware-nginx/Dockerfile | 4 + .../etc/nginx/templates/default.conf.template | 72 +++++++++++ images/shopware/.env.template | 121 ++++++++++++++++++ images/shopware/Dockerfile | 121 ++++++++++++++++++ .../config/.psh.yaml.override.template | 14 ++ .../nginx/templates/default.conf.template | 72 +++++++++++ images/shopware/config/php/conf.d/00-core.ini | 5 + .../shopware/config/php/conf.d/10-xdebug.ini | 3 + .../shopware/config/php/conf.d/20-opcache.ini | 4 + .../config/php/conf.d/99-blackfire.ini | 7 + .../shopware/config/root/.yarnrc.yml.template | 3 + .../actions/install-dependencies.sh | 2 + .../dev-ops/common/actions/init-composer.sh | 10 ++ .../actions/install-dependencies.sh | 4 + images/shopware/scripts/_add-preset-data.sh | 24 ++++ images/shopware/scripts/_build.sh | 23 ++++ images/shopware/scripts/_cache.sh | 11 ++ images/shopware/scripts/_dependencies.sh | 29 +++++ images/shopware/scripts/_helpers.sh | 49 +++++++ images/shopware/scripts/_install-plugins.sh | 27 ++++ images/shopware/scripts/_install-shopware.sh | 33 +++++ images/shopware/scripts/_start-php-fpm.sh | 19 +++ images/shopware/scripts/_wait-for-deps.sh | 15 +++ images/shopware/scripts/entrypoint.sh | 28 ++++ 26 files changed, 773 insertions(+) create mode 100644 .github/workflows/build-shopware.yml create mode 100644 images/shopware-nginx/Dockerfile create mode 100644 images/shopware-nginx/etc/nginx/templates/default.conf.template create mode 100644 images/shopware/.env.template create mode 100644 images/shopware/Dockerfile create mode 100644 images/shopware/config/.psh.yaml.override.template create mode 100644 images/shopware/config/nginx/templates/default.conf.template create mode 100644 images/shopware/config/php/conf.d/00-core.ini create mode 100644 images/shopware/config/php/conf.d/10-xdebug.ini create mode 100644 images/shopware/config/php/conf.d/20-opcache.ini create mode 100644 images/shopware/config/php/conf.d/99-blackfire.ini create mode 100644 images/shopware/config/root/.yarnrc.yml.template create mode 100644 images/shopware/config/root/dev-ops/administration/actions/install-dependencies.sh create mode 100644 images/shopware/config/root/dev-ops/common/actions/init-composer.sh create mode 100644 images/shopware/config/root/dev-ops/storefront/actions/install-dependencies.sh create mode 100644 images/shopware/scripts/_add-preset-data.sh create mode 100644 images/shopware/scripts/_build.sh create mode 100644 images/shopware/scripts/_cache.sh create mode 100755 images/shopware/scripts/_dependencies.sh create mode 100755 images/shopware/scripts/_helpers.sh create mode 100755 images/shopware/scripts/_install-plugins.sh create mode 100755 images/shopware/scripts/_install-shopware.sh create mode 100755 images/shopware/scripts/_start-php-fpm.sh create mode 100644 images/shopware/scripts/_wait-for-deps.sh create mode 100755 images/shopware/scripts/entrypoint.sh diff --git a/.github/workflows/build-shopware.yml b/.github/workflows/build-shopware.yml new file mode 100644 index 0000000..43d1303 --- /dev/null +++ b/.github/workflows/build-shopware.yml @@ -0,0 +1,53 @@ +name: 'Build shopware images' + +on: + push: + branches: + - main + + paths: + - images/shopware/**/* + + workflow_dispatch: + +permissions: + contents: read + packages: write + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + +jobs: + prepare-php: + uses: ./.github/workflows/--prepare-php.yml + secrets: inherit + with: + min-version: '8.1' + + + build: + runs-on: ubuntu-22.04 + needs: + - prepare-wp + strategy: + fail-fast: false + matrix: + php: [ '8.1', '8.2' ] + sw: [ '6.5.0.0' ] + steps: + - uses: actions/checkout@v3 + + - uses: ./.github/actions/login-ghcr + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - uses: ./.github/actions/build-push + with: + registry: ghcr.io + cache-mode: 'min' + context: images/shopware + image: myparcelnl/shopware + suffix: ${{ matrix.sw }}-php${{ matrix.php }} + build-args: | + PHP_VERSION=${{ matrix.php }} + SW_VERSION=${{ matrix.sw }} diff --git a/README.md b/README.md index 78c93ac..c416332 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,26 @@ PHP images based on `php:-alpine`, with Composer 2.5 and XDebug. - 8.1 (latest XDebug 3) - 8.2 (latest XDebug 3) +## shopware + +[View on GitHub Container Registry 📦](https://ghcr.io/myparcelnl/shopware) + +Shopware images based on `php-xd`. + +### Platforms + +- linux/amd64 +- linux/arm64 + +### Shopware versions + +- 6.5 (latest) + +### PHP versions + +- 8.2 +- 8.1 + ## wordpress [View on GitHub Container Registry 📦](https://ghcr.io/myparcelnl/wordpress) diff --git a/images/shopware-nginx/Dockerfile b/images/shopware-nginx/Dockerfile new file mode 100644 index 0000000..24eef79 --- /dev/null +++ b/images/shopware-nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:1-alpine + +# Copy nginx config +COPY ./etc/nginx/templates/* /etc/nginx/templates/ diff --git a/images/shopware-nginx/etc/nginx/templates/default.conf.template b/images/shopware-nginx/etc/nginx/templates/default.conf.template new file mode 100644 index 0000000..14feed8 --- /dev/null +++ b/images/shopware-nginx/etc/nginx/templates/default.conf.template @@ -0,0 +1,72 @@ + +server { + set $site_url "${BASE_URL}"; + set $root_dir "${PROJECT_ROOT}"; + + listen 80; + + # Handle / to index.php + index index.php; + + # Our server name + server_name $server_name; + + # Where the code is located + root $root_dir/public; + + # Redirect to https + add_header Content-Security-Policy upgrade-insecure-requests; + + # Needed for Shopware install / update + location /recovery/install { + index index.php; + try_files $uri /recovery/install/index.php$is_args$args; + } + + location /recovery/update/ { + if (!-e $request_filename){ + rewrite .. /recovery/update/index.php last; + } + } + + location ~* ^.+\.(?:css|cur|js|jpe?g|gif|ico|png|svg|webp|html|woff|woff2|xml)$ { + expires 1y; + add_header Cache-Control "public, must-revalidate, proxy-revalidate"; + + access_log off; + + # The directive enables or disables messages in error_log about files not found on disk. + log_not_found off; + + tcp_nodelay off; + + ## Set the OS file cache. + open_file_cache max=3000 inactive=120s; + open_file_cache_valid 45s; + open_file_cache_min_uses 2; + open_file_cache_errors off; + } + + location ~* ^.+\.svg$ { + add_header Content-Security-Policy "script-src 'none'"; + } + + # Forward any not found file to index.php. Also allows to have beautiful urls like /homemade-products/ + location / { + try_files $uri /index.php$is_args$args; + } + + # Let php-fpm handle .php files + location ~ \.php$ { + fastcgi_pass php:9000; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + include fastcgi.conf; + fastcgi_param HTTP_PROXY ""; + fastcgi_param HTTPS "on"; + fastcgi_buffers 8 16k; + fastcgi_buffer_size 32k; + fastcgi_read_timeout 300s; + client_body_buffer_size 128k; + http2_push_preload on; + } +} diff --git a/images/shopware/.env.template b/images/shopware/.env.template new file mode 100644 index 0000000..990da33 --- /dev/null +++ b/images/shopware/.env.template @@ -0,0 +1,121 @@ +# Available profiles: tools, es. +# - tools: includes tools for building and testing the project. +# - If using "es" you must set SHOPWARE_ES_ENABLED to 1. +COMPOSE_PROFILES= + +# Set your API keys here to have them prefilled in the configuration. +API_KEY_MYPARCELNL= +API_KEY_MYPARCELBE= + +# Fill in these variables to be able to use the blackfire service. You can find +# these keys at https://blackfire.io/my/settings/credentials +BLACKFIRE_CLIENT_ID= +BLACKFIRE_CLIENT_TOKEN= +BLACKFIRE_SERVER_ID= +BLACKFIRE_SERVER_TOKEN= + +# Set to 1 to enable very verbose logging during installation. +VERBOSE_LOGGING=0 +# Set to 1 to force clearing cache +FORCE_CLEAR_CACHE=0 +# Set to 1 to force building plugin assets. +FORCE_BUILD_PLUGIN_ASSETS=0 +# Set to 1 to force the installation of composer dependencies. +FORCE_COMPOSER_INSTALL=0 +# Set to 1 to force the installation of yarm dependencies. +FORCE_YARN_INSTALL=0 + +### +# ⚠️ You shouldn't NEED to change the following variables. +### + +BASE_URL=shopware.dev.myparcel.nl +FULL_URL=https://${BASE_URL} +FULL_URL_HTTP=http://${BASE_URL} +ROOT_DIR=/var/www/html +LOCK_DSN=flock + +### +# Database +### +DB_HOST=db +DB_PORT=3306 +DB_USER=shopware +DB_PASSWORD=shopware +DB_NAME=shopware +DB_PREFIX=sw_ + +### +# Shopware +### +PROJECT_ROOT=${ROOT_DIR} +DATABASE_URL="mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}" +APP_ENV=dev +APP_DEBUG=1 +APP_NO_PLUGINS=0 +APP_URL=${FULL_URL_HTTP} +APP_MAILER_URL="mailhog:1025" +APP_SECRET="" + +# Elasticsearch +SHOPWARE_ES_ENABLED=0 +SHOPWARE_ES_INDEXING_ENABLED=0 +SHOPWARE_ES_INDEX_PREFIX=sw_ +SHOPWARE_ES_THROW_EXCEPTION=0 + +# Admin user info +ADMIN_EMAIL=admin@${BASE_URL} +ADMIN_USER=${ADMIN_EMAIL} +ADMIN_PASSWORD=admin123 +ADMIN_FIRST_NAME=Admin +ADMIN_LAST_NAME=MyParcel + +# Customer user info +CUSTOMER_EMAIL=customer@${BASE_URL} +CUSTOMER_USER=${CUSTOMER_EMAIL} +CUSTOMER_PASSWORD=customer +CUSTOMER_FIRST_NAME=Customer +CUSTOMER_LAST_NAME=MyParcel + +# Email +MAILER_URL=smtp://mailhog:1025 + +# Disables type checking in building administration and storefront. Speeds up the build process. +DISABLE_ADMIN_COMPILATION_TYPECHECK=true +# Skips downloading the huge chromium package when installing puppeteer (which does not even work on our os). +PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true + +### +# MySQL +### +MYSQL_HOST=${DB_HOST} +MYSQL_TCP_PORT=${DB_PORT} +MYSQL_UNIX_PORT=${DB_PORT} +MYSQL_ROOT_PASSWORD=root +MYSQL_USER=${DB_USER} +MYSQL_PASSWORD=${DB_PASSWORD} +MYSQL_DATABASE=${DB_NAME} +MYSQL_TABLE_PREFIX=${DB_PREFIX} + +### +# Nginx +### +NGINX_HOST=${BASE_URL} +NGINX_PORT=80 + +### +# ElasticSearch +### +ES_HOST=es +ES_PORT=9200 + +### +# Miscellaneous +### + +# https://xdebug.org/docs/all_settings#mode +XDEBUG_MODE=debug + +PHP_IDE_CONFIG="serverName=${BASE_URL}" + +BLACKFIRE_DISABLE_LEGACY_PORT=1 diff --git a/images/shopware/Dockerfile b/images/shopware/Dockerfile new file mode 100644 index 0000000..3fdb6a6 --- /dev/null +++ b/images/shopware/Dockerfile @@ -0,0 +1,121 @@ +ARG PHP_VERSION="8.1" + +########## +# Shopware +########## +FROM ghcr.io/myparcelnl/php-xd:${PHP_VERSION}-fpm-alpine AS shopware + +ARG NODE_VERSION="16" +ARG SW_VERSION="6.5.0.0" +ARG XDEBUG_VERSION="3.1.6" +ARG YARN_VERSION="3.3.0" + +ARG ROOT_DIR=/var/www/html + +WORKDIR ${ROOT_DIR} + +RUN apk upgrade && \ + # Add dependencies + apk add --no-cache \ + # ext-gd + freetype-dev \ + libjpeg-turbo-dev \ + libpng-dev \ + # ext-intl + icu-dev \ + libzip-dev \ + # Used in enable-modules.sh + sudo \ + # Includes envsubst + gettext \ + # needed for wait-for-it + bash \ + # used in installation + mysql-client \ + # For building assets + "nodejs>${NODE_VERSION}" \ + npm \ + "$PHPIZE_DEPS" \ + && \ + docker-php-ext-configure gd \ + --with-jpeg \ + --with-freetype \ + && \ + # Install php extensions + docker-php-ext-install \ + pdo_mysql \ + zip \ + gd \ + intl \ + bcmath \ + && \ + # Install apcu + pecl install apcu && \ + docker-php-ext-enable apcu && \ + # Install wait-for-it + curl https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -o /usr/local/bin/wait-for-it && \ + chmod +x /usr/local/bin/wait-for-it && \ + # Set composer cache directory + composer config --global cache-dir /tmp/.cache/composer && \ + # Create Shopware project \ + COMPOSER_MEMORY_LIMIT=-1 composer create-project shopware/production:"${SW_VERSION}" . && \ + # Remove default env files + rm -f .env .env.dist && \ + touch .env .env.dist && \ + # Set up yarn + npm i -g yarn && \ + yarn set version "${YARN_VERSION}" && \ + # Configure yarn \ + yarn config set --home enableTelemetry 0 && \ + yarn config set enableGlobalCache true && \ + yarn config set globalFolder /tmp/.cache/yarn && \ + yarn config set logFilters --json '[ {"code":"YN0060","level":"discard"}, {"code":"YN0002","level":"discard"} ]' && \ + yarn config set nmHoistingLimits workspaces && \ + yarn config set nodeLinker node-modules && \ + yarn plugin import workspace-tools && \ + # Add workspaces + npm pkg set \ + "workspaces[]=platform/src/Storefront/Resources/app/storefront" \ + "workspaces[]=platform/src/Administration/Resources/app/administration" && \ + # Clear caches + npm cache clean --force && \ + rm -rf /var/cache/apk/* && \ + # Reset permissions + find . -not -user www-data -exec chown www-data {} \; && \ + find /tmp -not -user www-data -exec chown www-data {} \; && \ + # Remove composer.lock files to avoid issues with composer install + find . -name "composer.lock" -delete + +RUN version="$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;")" \ + architecture="$(uname -m)" && \ + mkdir -p /tmp/blackfire && \ + # Install Blackfire client + curl -A "Docker" -L "https://blackfire.io/api/v1/releases/cli/linux/$architecture" | tar zxp -C /tmp/blackfire && \ + mv /tmp/blackfire/blackfire /usr/bin/blackfire && \ + # Install Blackfire probe + curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s "https://blackfire.io/api/v1/releases/probe/php/linux/$architecture/$version" && \ + tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire && \ + mv /tmp/blackfire/blackfire-*.so "$(php -r "echo ini_get ('extension_dir');")/blackfire.so" && \ + rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz + +# add utilities +RUN apk add --no-cache \ + vim \ + # For zipping the plugin + zip \ + zsh + +COPY ./config/.psh.yaml.override.template /tmp/.psh.yaml.override.template +COPY ./config/nginx /etc/nginx +COPY ./config/php /usr/local/etc/php +COPY ./config/root . +COPY ./scripts /tmp/scripts + +ENV YARN_VERSION=${YARN_VERSION} +ENV SW_VERSION=${SW_VERSION} +ENV PHP_VERSION=${PHP_VERSION} +ENV XDEBUG_VERSION=${XDEBUG_VERSION} + +ENV ROOT_DIR=${ROOT_DIR} + +CMD ["/tmp/scripts/entrypoint.sh"] diff --git a/images/shopware/config/.psh.yaml.override.template b/images/shopware/config/.psh.yaml.override.template new file mode 100644 index 0000000..3f48d8a --- /dev/null +++ b/images/shopware/config/.psh.yaml.override.template @@ -0,0 +1,14 @@ +const: + DB_USER: "${DB_USER}" + DB_PASSWORD: "${DB_PASSWORD}" + DB_HOST: "${DB_HOST}" + DB_PORT: "${DB_PORT}" + DB_NAME: "${DB_NAME}" + APP_URL: "${APP_URL}" + APP_ENV: "${APP_ENV}" + APP_MAILER_URL: "${APP_MAILER_URL}" + SHOPWARE_ES_HOSTS: "${ES_HOST}:${ES_PORT}" + SHOPWARE_ES_ENABLED: "${SHOPWARE_ES_ENABLED}" + SHOPWARE_ES_THROW_EXCEPTION: "${SHOPWARE_ES_THROW_EXCEPTION}" + SHOPWARE_ES_INDEXING_ENABLED: "${SHOPWARE_ES_INDEXING_ENABLED}" + SHOPWARE_ES_INDEX_PREFIX: "${SHOPWARE_ES_INDEX_PREFIX}" diff --git a/images/shopware/config/nginx/templates/default.conf.template b/images/shopware/config/nginx/templates/default.conf.template new file mode 100644 index 0000000..14feed8 --- /dev/null +++ b/images/shopware/config/nginx/templates/default.conf.template @@ -0,0 +1,72 @@ + +server { + set $site_url "${BASE_URL}"; + set $root_dir "${PROJECT_ROOT}"; + + listen 80; + + # Handle / to index.php + index index.php; + + # Our server name + server_name $server_name; + + # Where the code is located + root $root_dir/public; + + # Redirect to https + add_header Content-Security-Policy upgrade-insecure-requests; + + # Needed for Shopware install / update + location /recovery/install { + index index.php; + try_files $uri /recovery/install/index.php$is_args$args; + } + + location /recovery/update/ { + if (!-e $request_filename){ + rewrite .. /recovery/update/index.php last; + } + } + + location ~* ^.+\.(?:css|cur|js|jpe?g|gif|ico|png|svg|webp|html|woff|woff2|xml)$ { + expires 1y; + add_header Cache-Control "public, must-revalidate, proxy-revalidate"; + + access_log off; + + # The directive enables or disables messages in error_log about files not found on disk. + log_not_found off; + + tcp_nodelay off; + + ## Set the OS file cache. + open_file_cache max=3000 inactive=120s; + open_file_cache_valid 45s; + open_file_cache_min_uses 2; + open_file_cache_errors off; + } + + location ~* ^.+\.svg$ { + add_header Content-Security-Policy "script-src 'none'"; + } + + # Forward any not found file to index.php. Also allows to have beautiful urls like /homemade-products/ + location / { + try_files $uri /index.php$is_args$args; + } + + # Let php-fpm handle .php files + location ~ \.php$ { + fastcgi_pass php:9000; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + include fastcgi.conf; + fastcgi_param HTTP_PROXY ""; + fastcgi_param HTTPS "on"; + fastcgi_buffers 8 16k; + fastcgi_buffer_size 32k; + fastcgi_read_timeout 300s; + client_body_buffer_size 128k; + http2_push_preload on; + } +} diff --git a/images/shopware/config/php/conf.d/00-core.ini b/images/shopware/config/php/conf.d/00-core.ini new file mode 100644 index 0000000..c61f368 --- /dev/null +++ b/images/shopware/config/php/conf.d/00-core.ini @@ -0,0 +1,5 @@ +allow_url_fopen=On +max_execution_time=300 +memory_limit=512M +post_max_size=128M +upload_max_filesize=128M diff --git a/images/shopware/config/php/conf.d/10-xdebug.ini b/images/shopware/config/php/conf.d/10-xdebug.ini new file mode 100644 index 0000000..0376583 --- /dev/null +++ b/images/shopware/config/php/conf.d/10-xdebug.ini @@ -0,0 +1,3 @@ +[xdebug] +xdebug.mode = ${XDEBUG_MODE} +xdebug.client_host = "host.docker.internal" diff --git a/images/shopware/config/php/conf.d/20-opcache.ini b/images/shopware/config/php/conf.d/20-opcache.ini new file mode 100644 index 0000000..34aef9a --- /dev/null +++ b/images/shopware/config/php/conf.d/20-opcache.ini @@ -0,0 +1,4 @@ +[opcache] +opcache.fast_shutdown = 0 +opcache.revalidate_freq = 0 +opcache.validate_timestamps = 1 diff --git a/images/shopware/config/php/conf.d/99-blackfire.ini b/images/shopware/config/php/conf.d/99-blackfire.ini new file mode 100644 index 0000000..6011ffe --- /dev/null +++ b/images/shopware/config/php/conf.d/99-blackfire.ini @@ -0,0 +1,7 @@ +[blackfire] +; if the environment variable BLACKFIRE_SERVER_ID is set, enable blackfire +; otherwise, disable it +blackfire.enabled = ${BLACKFIRE_SERVER_ID:+1} +extension = blackfire.so +blackfire.agent_socket = tcp://blackfire:8307 +blackfire.agent_timeout = 0.25 diff --git a/images/shopware/config/root/.yarnrc.yml.template b/images/shopware/config/root/.yarnrc.yml.template new file mode 100644 index 0000000..d1af7cb --- /dev/null +++ b/images/shopware/config/root/.yarnrc.yml.template @@ -0,0 +1,3 @@ +nodeLinker: node-modules +yarnPath: .yarn/releases/yarn-${YARN_VERSION}.cjs +nmHoistingLimits: workspaces diff --git a/images/shopware/config/root/dev-ops/administration/actions/install-dependencies.sh b/images/shopware/config/root/dev-ops/administration/actions/install-dependencies.sh new file mode 100644 index 0000000..5a461b0 --- /dev/null +++ b/images/shopware/config/root/dev-ops/administration/actions/install-dependencies.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +#DESCRIPTION: installs the dependencies for the administration using npm diff --git a/images/shopware/config/root/dev-ops/common/actions/init-composer.sh b/images/shopware/config/root/dev-ops/common/actions/init-composer.sh new file mode 100644 index 0000000..f66bdce --- /dev/null +++ b/images/shopware/config/root/dev-ops/common/actions/init-composer.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +# Copy of the original file, but except removing all vendor directories. This slows down a restart for no reason. +# Source: https://github.com/shopware/development/blob/v6.4.17.2/dev-ops/common/actions/init-composer.sh + +composer update --no-interaction --optimize-autoloader --no-scripts +composer install --no-interaction --optimize-autoloader --working-dir=dev-ops/analyze +if [ -e platform/src/Recovery ]; then composer install --no-interaction --optimize-autoloader --working-dir=platform/src/Recovery; fi + +if grep -q static-analyze platform/composer.json; then composer update --working-dir=platform; fi diff --git a/images/shopware/config/root/dev-ops/storefront/actions/install-dependencies.sh b/images/shopware/config/root/dev-ops/storefront/actions/install-dependencies.sh new file mode 100644 index 0000000..c110973 --- /dev/null +++ b/images/shopware/config/root/dev-ops/storefront/actions/install-dependencies.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +#DESCRIPTION: Installs the node.js dependencies + +node vendor/shopware/platform/src/Storefront/Resources/app/storefront/copy-to-vendor.js diff --git a/images/shopware/scripts/_add-preset-data.sh b/images/shopware/scripts/_add-preset-data.sh new file mode 100644 index 0000000..92dd547 --- /dev/null +++ b/images/shopware/scripts/_add-preset-data.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# todo add more initial data here +add-preset-data() { + h1 "Adding preset data..." + composer require shopware/dev-tools && \ + APP_ENV=prod bin/console framework:demodata + APP_ENV=prod bin/console dal:refresh:index + + php bin/console "$verbosity" user:create \ + "$ADMIN_USER" \ + --admin \ + --email "$ADMIN_EMAIL" \ + --password "$ADMIN_PASSWORD" \ + --firstName "$ADMIN_FIRST_NAME" \ + --lastName "$ADMIN_LAST_NAME" + + php bin/console "$verbosity" user:create \ + "$CUSTOMER_USER" \ + --email "$CUSTOMER_EMAIL" \ + --password "$CUSTOMER_PASSWORD" \ + --firstName "$CUSTOMER_FIRST_NAME" \ + --lastName "$CUSTOMER_LAST_NAME" +} diff --git a/images/shopware/scripts/_build.sh b/images/shopware/scripts/_build.sh new file mode 100644 index 0000000..e21f1c1 --- /dev/null +++ b/images/shopware/scripts/_build.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +# Build all assets +build-assets() { + h2 "Building administration..." + APP_DEBUG="${VERBOSE_LOGGING}" bin/build-administration.sh + + h2 "Building storefront..." + APP_DEBUG="${VERBOSE_LOGGING}" bin/build-storefront.sh +} + +# With the SHOPWARE_ADMIN_BUILD_ONLY_EXTENSIONS environment variable, Shopware only builds assets related to plugins. +build-plugin-assets() { + if [[ "$FORCE_BUILD_PLUGIN_ASSETS" != "1" && -f "build-plugin-assets.lock" ]]; then + h2 "Plugin assets already built. Set FORCE_BUILD_PLUGIN_ASSETS=1 to force build." + return + fi + + SHOPWARE_ADMIN_BUILD_ONLY_EXTENSIONS=1 build-assets + + touch "build-plugin-assets.lock" + touch "clear-cache.lock" +} diff --git a/images/shopware/scripts/_cache.sh b/images/shopware/scripts/_cache.sh new file mode 100644 index 0000000..7cbd110 --- /dev/null +++ b/images/shopware/scripts/_cache.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +clear-cache() { + h2 "Clearing cache..." + bin/console "$verbosity" cache:clear +} + +warmup-cache() { + h2 "Warming up cache..." + bin/console "$verbosity" cache:warmup +} diff --git a/images/shopware/scripts/_dependencies.sh b/images/shopware/scripts/_dependencies.sh new file mode 100755 index 0000000..49f6e9a --- /dev/null +++ b/images/shopware/scripts/_dependencies.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +install-dependencies-composer() { + dir=$1 + + if [[ "$FORCE_COMPOSER_INSTALL" != "1" && -d "$1/vendor" ]]; then + h2 "Skipping composer install, because $1/vendor directory already exists. Set FORCE_COMPOSER_INSTALL=1 to force install." + return + fi + + h2 "Installing composer dependencies in $dir..." + # Skip autoloader optimization if already done to speed up the process + composer install \ + --no-interaction \ + --no-progress \ + --no-scripts \ + $(test "vendor/autoload.php" && echo "--no-autoloader") \ + --working-dir "$dir" +} + +install-dependencies-node() { + if [[ "$FORCE_YARN_INSTALL" != "1" && -d "node_modules" ]]; then + h2 "Skipping yarn install, because node_modules directory already exists. Set FORCE_YARN_INSTALL=1 to force install." + return + fi + + h2 "Installing node dependencies..." + yarn install "$@" +} diff --git a/images/shopware/scripts/_helpers.sh b/images/shopware/scripts/_helpers.sh new file mode 100755 index 0000000..47b5da9 --- /dev/null +++ b/images/shopware/scripts/_helpers.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +declare -i term_width=70 + +h1() { + declare border padding text + border='\033[38;5;39m'"$(printf '⛌%.0s' $(seq 1 "$term_width"))"'\e[0m' + padding="$(printf ' %.0s' $(seq 1 $(((term_width - $(wc -m <<<"$*")) / 2))))" + text="\\e[1m$*\\e[0m" + echo -e "$border" + echo -e "${padding}${text}${padding}" + echo -e "$border" +} + +h2() { + printf '\e[97m==>\e[37;1m %s\e[0m\n' "$(echo -e "\033[36m$(date "+%H:%M:%S")\033[39m $*")" +} + +find-dirs() { + find "$1" -mindepth 1 -maxdepth 1 -type d > /dev/null 2>&1 +} + +link-paths() { + source_dir=$1 + dest_dir=$2 + + mkdir -p "$dest_dir" + + if find-dirs "$source_dir"; then + for path in "$source_dir"/*/; do + dest_path="$dest_dir/$(basename "$path")" + + # Ignore if path is not a directory. + [ -d "${path}" ] || continue + # Overwrite existing symlinks. + unlink "$dest_path" 2> /dev/null + + h2 "Linking $path => $dest_path" + ln -sf "$path" "$dest_path" + done + else + h2 "No folders found in $source_dir" + fi +} + +reset-permissions() { + h2 "Resetting permissions" + sudo chown -R www-data:www-data "$ROOT_DIR" +} diff --git a/images/shopware/scripts/_install-plugins.sh b/images/shopware/scripts/_install-plugins.sh new file mode 100755 index 0000000..0b29d49 --- /dev/null +++ b/images/shopware/scripts/_install-plugins.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +install-plugins() { + h1 "Installing and activating plugins" + link-paths "$TMP_PLUGINS_DIR" "$SW_PLUGINS_DIR" + + h2 "Refreshing plugins" + bin/console "$verbosity" plugin:refresh + + for path in "$TMP_PLUGINS_DIR"/*/; do + plugin=$(basename "$path") + + if [ bin/console plugin:info "$plugin" ]; then + h2 "[plugin:$plugin] Plugin already installed, skipping..." + continue + fi + + if [[ -f "$path/composer.json" ]]; then + install-dependencies-composer "$path" + fi + + h2 "[plugin:$plugin] Installing and activating..." + bin/console plugin:install "$verbosity" --activate "$plugin" + done + + build-plugin-assets +} diff --git a/images/shopware/scripts/_install-shopware.sh b/images/shopware/scripts/_install-shopware.sh new file mode 100755 index 0000000..a9284f8 --- /dev/null +++ b/images/shopware/scripts/_install-shopware.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +install-shopware() { + install-dependencies-node + wait-for-deps + + h1 "Starting Shopware installation" + + if [ -f install.lock ]; then + h2 "Shopware is already installed." + return + fi + + if [[ ! -f .psh.yaml.override ]]; then + h2 "Creating psh.yaml.override..." + envsubst < /tmp/.psh.yaml.override.template > .psh.yaml.override + fi + + h2 "Running installation..." + php bin/console "$verbosity" system:install --basic-setup --create-database --force + + # if command failed + if [ $? -ne 0 ]; then + h2 "Shopware installation failed." + exit 1 + fi + + h2 "Shopware installation succeeded." + touch install.lock + touch "clear-cache.lock" + + add-preset-data +} diff --git a/images/shopware/scripts/_start-php-fpm.sh b/images/shopware/scripts/_start-php-fpm.sh new file mode 100755 index 0000000..4601996 --- /dev/null +++ b/images/shopware/scripts/_start-php-fpm.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +start-php-fpm() { + h1 "Starting php-fpm..." + + if pidof -s php-fpm > /dev/null; then + h2 "php-fpm is already running" + return + fi + + php-fpm -D + + if [ "$?" -eq 0 ]; then + h2 "Started php-fpm." + else + h2 "Failed to start php-fpm." + exit 1 + fi +} diff --git a/images/shopware/scripts/_wait-for-deps.sh b/images/shopware/scripts/_wait-for-deps.sh new file mode 100644 index 0000000..c4fbe22 --- /dev/null +++ b/images/shopware/scripts/_wait-for-deps.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +wait-for-deps() { + if ! wait-for-it -q -t 0 "$DB_HOST:$DB_PORT"; then + h2 "Waiting for database..." + wait-for-it -q -t 30 "$DB_HOST:$DB_PORT" + h2 "Database is ready." + fi + + if [[ "$SHOPWARE_ES_ENABLED" == "1" ]]; then + h2 "Waiting for ElasticSearch..." + curl -s -XGET "http://$ES_HOST:$ES_PORT/_cluster/health?wait_for_status=yellow&timeout=30s" > /dev/null + h2 "ElasticSearch is ready." + fi +} diff --git a/images/shopware/scripts/entrypoint.sh b/images/shopware/scripts/entrypoint.sh new file mode 100755 index 0000000..df01d3d --- /dev/null +++ b/images/shopware/scripts/entrypoint.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +export TMP_PLUGINS_DIR="/tmp/plugins" +export SW_PLUGINS_DIR="$PROJECT_ROOT/custom/plugins" + +if [[ "$VERBOSE_LOGGING" == "1" ]]; then + export verbosity=-vvv +else + export verbosity=-v +fi + +for script in /tmp/scripts/_*; do source "$script"; done + +# Start php fpm in the background in advance. +start-php-fpm + +install-shopware +install-plugins + +if [[ "$FORCE_CLEAR_CACHE" == "1" || -f "clear-cache.lock" ]]; then + clear-cache + warmup-cache +fi + +reset-permissions + +h1 "🔵 Shopware is ready at ${FULL_URL}/admin ✨" +tail -f var/log/*.log & wait