ddev / ddev-platformsh

Add integration with Platform.sh hosting service
Apache License 2.0
9 stars 10 forks source link

`PLATFORM_BRANCH` needs to be set for drush to work #81

Closed ptmkenny closed 1 year ago

ptmkenny commented 1 year ago

After setting up ddev, I ran ddev drush cr and got the following error:

Trying to drush The branch variable is not defined. Are you sure you're running on Platform.sh?

If I set PLATFORM_BRANCH=my-dev-branch in config.yaml under web_environment and ddev restart, then drush works.

My install

ddev config global --mutagen-enabled

ddev config

project name: myRepo docroot: web type: php (a Drupal 9 site)

ddev get platformsh/ddev-platformsh

Enter the platform ID (abcdefg). And enter the environment (my-dev-branch).

ddev composer install

After all this, config.yaml looked like this:

web_environment:
- PLATFORM_APPLICATION_NAME=drupal
- PLATFORM_ENVIRONMENT=my-dev-branch
- PLATFORM_PROJECT=abcdefg

So it seems to me that the solution is to set PLATFORM_BRANCH to the currently checked out git branch, or to ask the user to provide the branch as well (is it always the same as PLATFORM_ENVIRONMENT? it is in my case, but I don't know about others.)

In any case, it seems drush doesn't work without setting PLATFORM_ENVIRONMENT, and I couldn't find this documented anywhere, so I'm opening this issue.

rfay commented 1 year ago

Thanks so much for raising this issue!

Does it work if you ddev config --web-environment-add=PLATFORM_BRANCH=my-dev-branch ?

I'm not sure how I got as far in my Platform.sh education on this without hitting that. I've done piles of drupal9 sites.

Is it perhaps possible for you to share this site with me? (randy.fay at platform.sh)? Or to make a trivial project that demonstrates it?

ptmkenny commented 1 year ago

Yes, using ddev config --web-environment-add=PLATFORM_BRANCH=my-dev-branch also fixes the issue.

Sharing the entire site is a bit tricky-- the code is hosted on GitHub and there are several submodules. I spent the past half hour attempting to create a reproducible example by copying just the platform and composer scaffolding into a new repository.

Unfortunately, I haven't been able to reproduce the issue yet. So I will keep digging, but it may take awhile.

rfay commented 1 year ago

If you'd show a (perhaps edited) version of ddev debug configyaml it might help me. I'm also unable so far to recreate this using a variant of https://github.com/platformsh-templates/drupal9

(It's totally legit to add the PLATFORM_BRANCH env var, it's just that I want to understand what it means in your ecosystem, because I'm not understanding that yet.)

I've tried changing the project type to php, removing settings.ddev.php, etc, but haven't hit that behavior. Perhaps you have a checked-in settings.php that has something in it that affects this?

ptmkenny commented 1 year ago

config.yaml:

name: my-project
type: drupal9
docroot: web
php_version: "8.1"
webserver_type: nginx-fpm
router_http_port: "80"
router_https_port: "443"
xdebug_enabled: false
additional_hostnames: []
additional_fqdns: []
database:
  type: mariadb
  version: "10.5"
nfs_mount_enabled: false
mutagen_enabled: false
use_dns_when_possible: true
composer_version: "2"
web_environment:
- PLATFORM_APPLICATION_NAME=drupal
- PLATFORM_ENVIRONMENT=my-dev-branch
- PLATFORM_PROJECT=abcdefg
nodejs_version: "16"

When I moved config.yaml, platform.app.yaml, composer.json and composer.lock, and my build / deploy scripts to a new repo, I was unable to reproduce.

settings.php

<?php

/**
 * Drupal default settings.
 * Load services definition file.
 */
$settings['container_yamls'][] = __DIR__ . '/services.yml';
$settings['file_scan_ignore_directories'] = [
  'node_modules',
  'bower_components',
];

// Automatic Platform.sh settings.
if (isset($app_root) && isset($site_path) &&
  file_exists($app_root . '/' . $site_path . '/settings.platformsh.php')) {
  include $app_root . '/' . $site_path . '/settings.platformsh.php';
}

/**
 * If there is a local settings file, then include it
 */
$local_settings = __DIR__ . "/settings.local.php";
if (file_exists($local_settings)) {
  include $local_settings;
}

/**
 * If there is a local settings file, then include it
 */
$custom_settings = __DIR__ . "/settings.custom.php";
if (file_exists($custom_settings)) {
  include $custom_settings;
}

// Automatically generated include for settings managed by ddev.
$ddev_settings = dirname(__FILE__) . '/settings.ddev.php';
if (getenv('IS_DDEV_PROJECT') == 'true' && is_readable($ddev_settings)) {
  require $ddev_settings;
}

// Include settings required for Redis cache.
if ((file_exists(__DIR__ . '/settings.ddev.redis.php') && getenv('IS_DDEV_PROJECT') == 'true')) {
  include __DIR__ . '/settings.ddev.redis.php';
}

settings.custom.php

<?php

use Platformsh\ConfigReader\Config;
$platformsh = new Config();

// Disable eval because it is bad for security.
$settings['field_encrypt.use_eval_for_entity_hooks'] = FALSE;

// Config Split.
if ($platformsh->inRuntime()) {
  if (!$platformsh->branch !== 'main') {
    $config['webprofiler.config']['active_toolbar_items']['config'] = 'config';
    $config['webprofiler.config']['active_toolbar_items']['events'] = 'events';
    $config['webprofiler.config']['active_toolbar_items']['http'] = 'http';
    $config['webprofiler.config']['active_toolbar_items']['mail'] = 'mail';
    $config['webprofiler.config']['active_toolbar_items']['routing'] = 'routing';
    $config['webprofiler.config']['active_toolbar_items']['services'] = 'services';
    $config['webprofiler.config']['active_toolbar_items']['state'] = 'state';
    $config['webprofiler.config']['active_toolbar_items']['translations'] = 'translations';
    // Currently bugged.
    // https://gitlab.com/drupalspoons/devel/-/issues/334
    // $config['webprofiler.config']['active_toolbar_items']['theme']
    // = 'theme';.
  }
  if ($platformsh->environment === 'lando') {
    // Let anonymous run update.php.
    // $settings['update_free_access'] = TRUE;

    // DRUPAL CONFIG
    // Manually set this, even though Lando should do it automatically.
    $config['system.file']['path']['temporary'] = '/tmp';
    // Views UI.
    $config['views.settings']['ui']['show']['advanced_column'] = TRUE;
    $config['views.settings']['ui']['show']['master_display'] = TRUE;
    $config['views.settings']['ui']['show']['performance_statistics'] = TRUE;
    $config['views.settings']['ui']['show']['preview_information'] = TRUE;
    $config['views.settings']['ui']['show']['sql_query']['enabled'] = TRUE;
    $config['views.settings']['ui']['show']['sql_query']['where'] = 'above';
    $config['views.settings']['ui']['show']['display_embed'] = TRUE;
    $config['views.settings']['ui']['always_live_preview'] = TRUE;
    $config['views.settings']['ui']['exposed_filter_any_label'] = 'new_any';
    // Debug.
    $config['system.logging']['error_level'] = 'verbose';
    // Performance.
    // $config['system.performance']['css']['preprocess'] = FALSE;
    // $config['system.performance']['js']['preprocess'] = FALSE;
    // $config['system.performance']['minifyhtml']['minify_html'] = FALSE;
    // Turn off "strip comments" because it deletes twig debugging info.
    $config['minifyhtml.config']['strip_comments'] = FALSE;
    // Configure devel.
    $config['devel.settings']['page_alter'] = TRUE;
    $config['devel.settings']['raw_names'] = TRUE;
    $config['devel.settings']['rebuild_theme'] = TRUE;
    // Backtrace above the rendered page.
    $config['devel.settings']['error_handlers']['2'] = '2';
    $config['devel.settings']['devel_dumper'] = 'kint';
    // Update.
    $config['update.settings']['check']['disabled_extensions'] = TRUE;
  }
}

// Place the config directory outside of the Drupal root.
$settings['config_sync_directory'] = dirname(DRUPAL_ROOT) . '/config/base';

// Drush language import/export.
$settings['custom_translations_directory'] = dirname(DRUPAL_ROOT) . '/translations';

$keys = dirname(DRUPAL_ROOT) . '/submodules/secrets/keys.php';
if (file_exists($keys)) {
  include $keys;
}

$settings['tome_static_directory'] = dirname(DRUPAL_ROOT) . '/submodules/tome/html';

and

ddev debug configyaml

These config files were loaded for project my-project: [/Users/private/d/my-project/.ddev/config.yaml /Users/private/d/my-project/.ddev/config.platformsh.yaml]
name: my-project
type: drupal9
docroot: web
php_version: 8.1
webserver_type: nginx-fpm
webimage: drud/ddev-webserver:v1.21.3
router_http_port: 80
router_https_port: 443
database: {mariadb 10.5}
hooks: map[post-start:[map[exec:platform self:update -qy --no-major || true] map[exec:mkdir -p ${PLATFORM_CACHE_DIR} || true] map[exec:[ ! -z "${PLATFORMSH_CLI_TOKEN:-}" ] && (platform ssh-cert:load  -y || true)] map[exec:set -e
/app/.ci/platformsh/build.sh $PLATFORM_APP_DIR $PLATFORM_CACHE_DIR
] map[exec:/app/.ci/platformsh/deploy.sh
] map[exec:/app/.ci/platformsh/post_deploy.sh
] map[exec:platform self:update -qy --no-major || true] map[exec:mkdir -p ${PLATFORM_CACHE_DIR} || true] map[exec:[ ! -z "${PLATFORMSH_CLI_TOKEN:-}" ] && (platform ssh-cert:load  -y || true)] map[exec:set -e
/app/.ci/platformsh/build.sh $PLATFORM_APP_DIR $PLATFORM_CACHE_DIR
] map[exec:/app/.ci/platformsh/deploy.sh
] map[exec:/app/.ci/platformsh/post_deploy.sh
]]]
mailhog_port: 8025
mailhog_https_port: 8026
phpmyadmin_port: 8036
phpmyadmin_https_port: 8037
webimage_extra_packages: [figlet php8.1-redis php8.1-xdebug php-sodium]
project_tld: ddev.site
use_dns_when_possible: true
composer_version: 2
disable_settings_management: true
web_environment: [PLATFORM_PROJECT=abcdefg PLATFORM_RELATIONSHIPS=ewoiZGF0YWJhc2UiOiBbCiAgewogICAgInVzZXJuYW1lIjogImRiIiwKICAgICJzY2hlbWUiOiAibXlzcWwiLAogICAgInNlcnZpY2UiOiAiZGF0YWJhc2UiLAogICAgImZyYWdtZW50IjogbnVsbCwKICAgICJpcCI6ICIyNTUuMjU1LjI1NS4yNTUiLAogICAgImhvc3RuYW1lIjogImRiIiwKICAgICJwdWJsaWMiOiBmYWxzZSwKICAgICJjbHVzdGVyIjogImRkZXYtZHVtbXktY2x1c3RlciIsCiAgICAiaG9zdCI6ICJkYiIsCiAgICAicmVsIjogIm15c3FsIiwKICAgICJxdWVyeSI6IHsKICAgICAgImlzX21hc3RlciI6IHRydWUKICAgIH0sCiAgICAicGF0aCI6ICJkYiIsCiAgICAicGFzc3dvcmQiOiAiZGIiLAogICAgInR5cGUiOiAibWFyaWFkYjoxMC41IiwKICAgICJwb3J0IjogMzMwNiwKICAgICJob3N0X21hcHBlZCI6IGZhbHNlCiAgfQpdLCAKInJlZGlzIjogWwogICAgewogICAgICAidXNlcm5hbWUiOiBudWxsLAogICAgICAic2NoZW1lIjogInJlZGlzIiwKICAgICAgInNlcnZpY2UiOiAiY2FjaGUiLAogICAgICAiZnJhZ21lbnQiOiBudWxsLAogICAgImlwIjogIjI1NS4yNTUuMjU1LjI1NSIsCiAgICAgICJob3N0bmFtZSI6ICJyZWRpcyIsCiAgICAgICJwdWJsaWMiOiBmYWxzZSwKICAgICAgImNsdXN0ZXIiOiAiZGRldi1kdW1teS1jbHVzdGVyIiwKICAgICAgImhvc3QiOiAicmVkaXMiLAogICAgICAicmVsIjogInJlZGlzIiwKICAgICAgInF1ZXJ5Ijoge30sCiAgICAgICJwYXRoIjogbnVsbCwKICAgICAgInBhc3N3b3JkIjogbnVsbCwKICAgICAgInR5cGUiOiAicmVkaXM6Ni4wIiwKICAgICAgInBvcnQiOiA2Mzc5LAogICAgICAiaG9zdF9tYXBwZWQiOiBmYWxzZQogICAgfQogIF0gfQo= PLATFORM_ROUTES=eyAiaHR0cHM6Ly9hbmF0YS5kZGV2LnNpdGUvIjogeyAicHJpbWFyeSI6IHRydWUsICJpZCI6IG51bGwsICJwcm9kdWN0aW9uX3VybCI6ICJodHRwczovL2FuYXRhLmRkZXYuc2l0ZS8iLCAiYXR0cmlidXRlcyI6IHt9LCAidXBzdHJlYW0iOiAiZHJ1cGFsIiwgInR5cGUiOiAidXBzdHJlYW0iLCAib3JpZ2luYWxfdXJsIjogImh0dHBzOi8ve2RlZmF1bHR9LyIgfSB9 PLATFORM_ENVIRONMENT=prototype PLATFORM_CACHE_DIR=/mnt/ddev-global-cache/ddev-platformsh/my-project PLATFORM_TREE_ID=2dc356f2fea13ef683f9adc5fc5bd28e05ad992a PLATFORM_APP_DIR=/var/www/html PLATFORM_PROJECT_ENTROPY=d421eccdd88936b577e0041cff43a2a18fd60fd4de1116966162c9b5c2ba1808 NODE_VERSION=v16.15.0 NVM_VERSION=v0.39.1 PLATFORM_MOUNTS=/.drush /drush-backups /private /testing-module-symlinks /tmp /versioning /web/sites/default/files  PLATFORM_DIR=/var/www/html PLATFORM_VARIABLES=e30= PLATFORM_APPLICATION_NAME=drupal]
nodejs_version: 16
default_container_timeout: 120
rfay commented 1 year ago

When I moved config.yaml, platform.app.yaml, composer.json and composer.lock, and my build / deploy scripts to a new repo, I was unable to reproduce.

You would have to move config.platformsh.yaml and the .platform directory probably, and then perhaps run ddev get again.

Can you grep -r PLATFORM_BRANCH . in your project directory to figure out where it's referenced?

Thanks for your effort on this!

gilzow commented 1 year ago

@ptmkenny can you tell me approximately how old this repo is? Trying to gauge how far back to look into the git history for the D9 template. I think I know what's going on but want to make sure before I say definitively.

Also, can you do ❯ composer show platformsh/config-reader | grep version and tell which version of config-reader you have installed?

ptmkenny commented 1 year ago

@rfay

 grep -r PLATFORM_BRANCH .
./scripts/platformsh/sanitize-platformsh.php:$git_branch = getenv('PLATFORM_BRANCH');
./scripts/platformsh/dispatch-github-workflow.sh:branch_current_env=$(printenv PLATFORM_BRANCH)
./scripts/platformsh/initialize-site.php:$git_branch = getenv('PLATFORM_BRANCH');
./scripts/feedme.php:$git_branch = getenv('PLATFORM_BRANCH');
./.ddev/config.yaml:# - PLATFORM_BRANCH=prototype
./vendor/platformsh/config-reader/tests/valid/ENV_runtime.json:    "PLATFORM_BRANCH": "feature-x",
./vendor/platformsh/config-reader/tests/ConfigTest.php:        $env['PLATFORM_BRANCH'] = 'production';
./vendor/platformsh/config-reader/tests/ConfigTest.php:        $env['PLATFORM_BRANCH'] = 'staging';
./vendor/platformsh/config-reader/tests/ConfigTest.php:        $env['PLATFORM_BRANCH'] = 'master';
./vendor/platformsh/config-reader/tests/ConfigTest.php:        unset($env['PLATFORM_APPLICATION'], $env['PLATFORM_ENVIRONMENT'], $env['PLATFORM_BRANCH']);
./vendor/platformsh/config-reader/tests/ConfigTest.php:        unset($env['PLATFORM_APPLICATION'], $env['PLATFORM_ENVIRONMENT'], $env['PLATFORM_BRANCH']);

Edit: It seems to be config reader is at fault here when using Drush. But they are only in the tests, so I'm not sure why I get an error when running drush.

There are also many calls to PLATFORM_BRANCH in my own custom scripts that run during build/deploy. To avoid this kind of issue in the future, it might be good to clarify which Platformsh variables are available and which are not in the documentation. I imagine there are many others out there using the various platform environment variables in build scripts as well, and since ddev triggers the build scripts, it would be nice to know what variables can be used without worry and what needs to be rewritten to work with ddev.

@gilzow This repo is pretty old, maybe started on Drupal 8.6? And it was originally hosted on pantheon, then moved to platformsh, so there's probably some cruft, although I've tried to keep it clean and up to date as best I can.

ddev composer show platformsh/config-reader | grep version
versions : * 2.4.0
gilzow commented 1 year ago

To avoid this kind of issue in the future, it might be good to clarify which Platformsh variables are available and which are not in the documentation.

Fair point. The goal is to have all of them available that are applicable, but having a list of currently implemented is a solid piece of feedback.

For those pieces of code where you're using PLATFORM_BRANCH, you might update them to target $PLATFORM_ENVIRONMENT_TYPE which lets you distinguish between development, staging, and production environment types vs a specific branch name (since that can change).

Having said that, it isn't available in this addon yet, but that one is on the roadmap to add in soon.

rfay commented 1 year ago

I think this is completed, let me know if I've missed something. Thanks all!