Skip to content

Commit

Permalink
feat(SLB-397): platform.sh integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Leksat committed Jun 26, 2024
1 parent 2099de5 commit 4ecac2a
Show file tree
Hide file tree
Showing 22 changed files with 560 additions and 63 deletions.
4 changes: 4 additions & 0 deletions .environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Platform.sh env vars. Used for all apps.
# The file is a script which is executed in Dash.

export PREVIEW_URL=$(echo $PLATFORM_ROUTES | base64 --decode | jq -r 'to_entries[] | select (.value.id == "preview") | .key')
158 changes: 158 additions & 0 deletions .platform/applications.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
cms:
type: 'php:8.2'

dependencies:
php:
composer/composer: '^2.1'

runtime:
extensions:
- sodium

relationships:
database: 'db:mysql'

disk: 2048

mounts:
'/apps/cms/web/sites/default/files':
source: local
source_path: 'files'
'/.drush':
source: local
source_path: 'drush'

variables:
env:
NVM_VERSION: v0.39.3
NODE_VERSION: v18.20.2

build:
flavor: none

hooks:
build: |
set -ex
# Install NVM and switch to node version defined in env vars. Script from
# https://docs.platform.sh/languages/nodejs/node-version.html#use-nvm
set +x
unset NPM_CONFIG_PREFIX
export NVM_DIR="$PLATFORM_APP_DIR/.nvm"
if [ ! -d "$PLATFORM_CACHE_DIR/.nvm" ]; then
mkdir -p $PLATFORM_CACHE_DIR/.nvm
fi
ln -s $PLATFORM_CACHE_DIR/.nvm $NVM_DIR
if [ ! -d "$PLATFORM_CACHE_DIR/.nvm/versions/node/$NODE_VERSION" ]; then
export NVM_INSTALL_FILE="${PLATFORM_CACHE_DIR}/nvm_${NVM_VERSION}_install.sh"
if [ ! -f "$NVM_INSTALL_FILE" ]; then
wget -nc -O "$NVM_INSTALL_FILE" "https://raw.githubusercontent.com/nvm-sh/nvm/$NVM_VERSION/install.sh"
fi
bash $NVM_INSTALL_FILE
fi
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use "$NODE_VERSION"
set -x
npm install -g [email protected]
pnpm i
pnpm turbo:prep:cms
deploy: |
set -ex
cd apps/cms
php ./drush/platformsh_generate_drush_yml.php
cd web
../vendor/bin/drush -y deploy
web:
locations:
'/':
root: 'apps/cms/web'
expires: 5m
passthru: '/index.php'
# Deny access to all static files, except those specifically allowed below.
allow: false
rules:
'\.(avif|webp|jpe?g|png|gif|svgz?|css|js|map|ico|bmp|eot|woff2?|otf|ttf)$':
allow: true
'^/robots\.txt$':
allow: true
'^/sitemap\.xml$':
allow: true
# Deny direct access to configuration files.
'^/sites/sites\.php$':
scripts: false
'^/sites/[^/]+/settings.*?\.php$':
scripts: false

'/sites/default/files':
allow: true
expires: 5m
passthru: '/index.php'
root: 'apps/cms/web/sites/default/files'
scripts: false
rules:
'^/sites/default/files/(css|js)':
expires: 2w
# Ensure that private files are not accessible.
'^/sites/default/files/private/.*':
allow: false

crons:
drupal:
spec: '*/15 * * * *'
cmd: 'cd apps/cms/web && ../vendor/bin/drush cron'

################################################################################

preview:
type: 'nodejs:18'

# AXXX check
# relationships:
# cms: 'cms:http'

disk: 1024

web:
commands:
start: cd apps/preview && pnpm start

build:
flavor: none

hooks:
build: |
set -ex
npm install -g [email protected]
pnpm i
pnpm turbo:prep:preview
################################################################################

publisher:
type: 'nodejs:18'

disk: 1024

mounts:
'/apps/website/.cache':
source: local
source_path: 'cache'

web:
commands:
start: cd apps/website && pnpm start

build:
flavor: none

hooks:
build: |
set -ex
npm install -g [email protected]
pnpm i
pnpm turbo:prep:website
17 changes: 17 additions & 0 deletions .platform/routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'https://cms.{default}/':
id: cms
type: upstream
upstream: 'cms:http'
cache:
enabled: true
cookies: ['/^SS?ESS/', '/^Drupal.visitor/']

'https://preview.{default}/':
id: preview
type: upstream
upstream: 'preview:http'

'https://build.{default}/':
id: publisher
type: upstream
upstream: 'publisher:http'
3 changes: 3 additions & 0 deletions .platform/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
db:
type: mariadb:10.11
disk: 2048
3 changes: 2 additions & 1 deletion apps/cms/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
!/web/themes/custom

# Lagoon integrations
/drush
/drush/sites
/drush/Commands
/.drush-lock-update

# Silverback
Expand Down
4 changes: 3 additions & 1 deletion apps/cms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"drupal/userprotect": "^1.2",
"drupal/webform": "^6.1.5",
"drush/drush": "^12.5",
"enyo/dropzone": "^5.7.1"
"enyo/dropzone": "^5.7.1",
"platformsh/config-reader": "^2.4"
},
"conflict": {
"drupal/drupal": "*"
Expand Down Expand Up @@ -118,6 +119,7 @@
},
"[web-root]/sites/default/all.services.yml": "scaffold/all.services.yml",
"[web-root]/sites/default/production.settings.php": "scaffold/production.settings.php",
"[web-root]/sites/default/settings.platformsh.php": "scaffold/settings.platformsh.php",
"[web-root]/robots.txt": "scaffold/robots.txt"
},
"allowed-packages": [
Expand Down
48 changes: 44 additions & 4 deletions apps/cms/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions apps/cms/drush/platformsh_generate_drush_yml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @file
* A script that creates the .drush/drush.yml file.
*
* Copied from https://github.com/platformsh-templates/drupal10 (paths adjusted)
*/

// This file should only be executed as a PHP-CLI script.
if (PHP_SAPI !== 'cli') {
exit;
}

require_once(__DIR__ . '/../vendor/autoload.php');

/**
* Returns a site URL to use with Drush, if possible.
*
* @return string|NULL
*/
function _platformsh_drush_site_url() {
$platformsh = new \Platformsh\ConfigReader\Config();

if (!$platformsh->inRuntime()) {
return;
}

$routes = $platformsh->getUpstreamRoutes($platformsh->applicationName);

// Sort URLs, with the primary route first, then by HTTPS before HTTP, then by length.
usort($routes, function (array $a, array $b) {
// false sorts before true, normally, so negate the comparison.
return
[!$a['primary'], strpos($a['url'], 'https://') !== 0, strlen($a['url'])]
<=>
[!$b['primary'], strpos($b['url'], 'https://') !== 0, strlen($b['url'])];
});

// Return the url of the first one.
return reset($routes)['url'] ?: NULL;
}

$appRoot = dirname(__DIR__);
$filename = $appRoot . '/../../.drush/drush.yml';

$siteUrl = _platformsh_drush_site_url();

if (empty($siteUrl)) {
echo "Failed to find a site URL\n";

if (file_exists($filename)) {
echo "The file exists but may be invalid: $filename\n";
}

exit(1);
}

$siteUrlYamlEscaped = json_encode($siteUrl, JSON_UNESCAPED_SLASHES);
$scriptPath = __FILE__;

$success = file_put_contents($filename, <<<EOF
# Drush configuration file.
# This was automatically generated by the script:
# $scriptPath
options:
# Set the default site URL.
uri: $siteUrlYamlEscaped
EOF
);
if (!$success) {
echo "Failed to write file: $filename\n";
exit(1);
}

if (!chmod($filename, 0600)) {
echo "Failed to modify file permissions: $filename\n";
exit(1);
}

echo "Created Drush configuration file: $filename\n";
2 changes: 1 addition & 1 deletion apps/cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Drupal based content management system.",
"sideEffects": false,
"scripts": {
"prep:composer": "if command -v composer; then composer install; else echo 'Skipping composer install.'; fi",
"prep:composer": "./prep-composer.sh",
"prep:database": "./prep-database.sh",
"prep:schema": "pnpm drush silverback-gatsby:schema-export ../../../tests/schema || true",
"fix-premissions": "chmod +w web/sites/default/files/.htaccess && chmod +w web/sites/default/files/private/.htaccess",
Expand Down
12 changes: 12 additions & 0 deletions apps/cms/prep-composer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

set -e

# We don't want install composer dependencies in node environments, so we check
# for composer command. But, on Platform.sh, composer exists even in nodejs
# containers.
if (command -v composer >/dev/null) && ([ -z "$PLATFORM_PROJECT" ] || [ "$PLATFORM_APPLICATION_NAME" = "cms" ]); then
composer install
else
echo 'Skipping composer install.'
fi
Loading

0 comments on commit 4ecac2a

Please sign in to comment.