Roots Discourse

[Workaround] PHP 7.4.x: Fix CLI deprecation warning

With current PHP7.4.x and WP-CLI 2.4.0, the WP CLI has issues during Trellis deploy.
WP CLI also uses the doctor subcommand, which fails because it causes a PHP deprecation warning (as it is, as we know, a PHP-based tool) that disturbs ansible.
The issue in WP CLI is known but not fixed yet: https://github.com/wp-cli/doctor-command/issues/151
If you still want to be able to deploy, there is a relatively easy fix that can be added to your Trellis config:

roles/wp-cli/tasks/main.yml:

# Add to end:
- name: Add extra config file to be required by WP CLI (workaround)
  file:
    src: extra-config.php
    dest: /home/web/.wp-cli/extra-config.php
    state: file

roles/wp-cli/files/extra-config.php:

<?php
// Workaround for WP CLI doctor subcommand on PHP 7.4
error_reporting(E_ERROR | E_WARNING | E_PARSE);

roles/deploy/hooks/finalize-before.yml:

# Change the 'WordPress Installed?' task to this:
- name: WordPress Installed?
  command: wp core is-installed --skip-plugins --skip-themes --require={{ deploy_helper.shared_path }}/tmp_multisite_constants.php --require=/home/web/.wp-cli/extra-config.php
  args:
    chdir: "{{ deploy_helper.new_release_path }}"
  register: wp_installed
  changed_when: false
  failed_when: wp_installed.stderr | default("") != "" or wp_installed.rc > 1

When you are also using the trellis-backup-during-deploy extension, you also have to adjust its WP-CLI invoking task:
vendor/roles/trellis-backup-during-deploy/tasks/main.yml:

# Change the 'WordPress Installed?' task to this:
- name: WordPress installed?
  command: wp core is-installed --skip-plugins --skip-themes --require={{ deploy_helper.shared_path }}/tmp_multisite_constants.php  --require=/home/web/.wp-cli/extra-config.php
  args:
    chdir: "{{ deploy_helper.current_path }}"
  register: initialize_after_wp_installed
  when: initialize_after_current_path.stat.exists
  changed_when: false
  failed_when: initialize_after_wp_installed.stderr | default("") != "" or initialize_after_wp_installed.rc > 1

Reapply the ansible-playbook to apply the fix.

This fix let WP CLI load an extra config file that disabled the PHP notice/warning messages so the WP-CLI output doesn’t throw off ansible.

1 Like

Maybe failed_when could be updated to check more output (or rc code) instead of having to load that extra PHP file?

1 Like

Yes, the status code should be the same (success), even with those notices/warnings printed to console.

Has been fixed now: https://github.com/wp-cli/doctor-command/pull/152
Though the workaround still applies and is helpful is warnings/notices occur again.