Skip to content

Add environment variable configuration support to wp-cli.yml#6169

Merged
swissspidy merged 18 commits intomainfrom
copilot/add-env-var-support-wp-cli-yml
Mar 13, 2026
Merged

Add environment variable configuration support to wp-cli.yml#6169
swissspidy merged 18 commits intomainfrom
copilot/add-env-var-support-wp-cli-yml

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 14, 2025

Description

Adds support for configuring environment variable fallback values in wp-cli.yml. When an environment variable is not set in the shell, WP-CLI checks the configuration file for a fallback value. Actual environment variables always take precedence.

The env key in wp-cli.yml is treated as extra config (consistent with other command-specific defaults like config create:), not a formal config spec entry.

Changes

Core implementation:

  • The env key in wp-cli.yml is stored in extra_config (not config-spec.php) for consistency with other command defaults
  • Created WP_CLI\Utils\get_env_or_config() helper that checks environment first, then config
  • Updated environment variable reads throughout the codebase to use the helper for all WP_CLI_* vars that are read after config is initialized:
    • Cache: WP_CLI_CACHE_DIR, WP_CLI_CACHE_EXPIRY, WP_CLI_CACHE_MAX_SIZE
    • Packages: WP_CLI_PACKAGES_DIR
    • Update behavior: WP_CLI_DISABLE_AUTO_CHECK_UPDATE, WP_CLI_AUTO_CHECK_UPDATE_DAYS, WP_CLI_AUTO_UPDATE_PROMPT
    • Shutdown/rerun prompts: WP_CLI_SKIP_PROMPT
    • Command behavior: WP_CLI_AUTOCORRECT, WP_CLI_SUPPRESS_GLOBAL_PARAMS
    • SSH/Docker: WP_CLI_SSH_PRE_CMD, WP_CLI_SSH_BINARY, WP_CLI_DOCKER_NO_TTY, WP_CLI_DOCKER_NO_INTERACTIVE
    • Misc: WP_CLI_ALIAS_GROUPS_PARALLEL, WP_CLI_WINDOWS_CODE_PAGE

Note: Env vars read before config is initialized (e.g. WP_CLI_CONFIG_PATH, WP_CLI_REQUIRE, WP_CLI_ALLOW_ROOT) cannot be configured this way by nature.

Documentation:

  • Updated JSON schema (wp-cli-config.json) with env property definition
  • Updated wp-cli.example.yml with all supported environment variables and a note about quoting YAML boolean-like string values

Tests:

  • Added Behat scenarios verifying config fallbacks for: WP_CLI_CACHE_DIR, WP_CLI_PACKAGES_DIR, WP_CLI_CACHE_EXPIRY, WP_CLI_CACHE_MAX_SIZE, WP_CLI_SKIP_PROMPT, WP_CLI_AUTO_UPDATE_PROMPT, WP_CLI_AUTOCORRECT
  • Added test scenarios verifying environment variable precedence over config values

Example Usage

env:
  WP_CLI_CACHE_DIR: /tmp/custom-cache
  WP_CLI_PACKAGES_DIR: /tmp/custom-packages
  WP_CLI_CACHE_EXPIRY: 3600
  WP_CLI_CACHE_MAX_SIZE: 104857600
  WP_CLI_SKIP_PROMPT: "yes"
  WP_CLI_AUTO_UPDATE_PROMPT: "no"
  WP_CLI_DISABLE_AUTO_CHECK_UPDATE: "1"

Note: String values that look like YAML booleans (yes/no/true/false) must be quoted.

How has this been tested?

  • All existing static analysis passes (lint, phpcs, phpstan)
  • Manual testing confirms both config fallbacks and environment variable precedence
  • Comprehensive Behat test coverage for supported environment variables
  • Tests verify both config fallback and environment variable override behavior

Checklist:

  • Functional tests have been added
  • Documentation has been updated as necessary
Original prompt

This section details on the original issue you should resolve

<issue_title>Ability to set environment variables in wp-cli.yml</issue_title>
<issue_description>Sometimes environment variables (https://make.wordpress.org/cli/handbook/references/config/#environment-variables) are not available in the shell for wp-cli, and thus it would be nice to be able to set these in wp-cli.yml

Example would be to configure WP_CLI_CACHE_DIR etc. using the wp-cli.yml file.</issue_description>

Comments on the Issue (you are @copilot in this section)

@danielbachhuber Thanks for the suggestion, @tomsommer

I'd be open to this if someone wants to submit a pull request!

To clarify the implementation: we wouldn't want to have wp-cli.yml set environment variables. Rather, we'd want to update WP-CLI to check wp-cli.yml for a value if the corresponding environment variable wasn't set.

Sometimes environment variables are not available in the shell for wp-cli

For posterity, could you provide a bit more detail as to when this isn't the case?

@danielbachhuber Related https://github.com//issues/6130 @swissspidy Related: #5645

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 14, 2025 21:02
- Add 'env' configuration key to config-spec.php
- Create get_env_or_config() helper function in utils.php
- Update get_cache_dir() to use config fallback
- Update cache expiry/max_size to use config fallback
- Update packages_dir to use config fallback
- Add Behat tests for env variable configuration
- Environment variables still take precedence over config

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Add env example to wp-cli.example.yml
- Update JSON schema to include env property
- Document that env values are fallbacks when env vars not set

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Add null check for runner->config in get_env_or_config()
- Update schema description to clarify values are converted to strings
- Allow both string and numeric types in schema (YAML parses numbers)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add ability to set environment variables in wp-cli.yml Add environment variable configuration support to wp-cli.yml Dec 14, 2025
Copilot AI requested a review from swissspidy December 14, 2025 21:10
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 14, 2025

This comment was marked as resolved.

- Add scenario testing WP_CLI_CACHE_EXPIRY configuration
- Add scenario testing WP_CLI_CACHE_MAX_SIZE configuration
- Add scenario testing environment variable precedence for both
- Tests verify config fallbacks and env var override behavior

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI requested a review from swissspidy December 14, 2025 21:43
@swissspidy swissspidy marked this pull request as ready for review December 14, 2025 22:04
@swissspidy swissspidy requested a review from a team as a code owner December 14, 2025 22:04
@github-actions github-actions bot added command:cli Related to 'cli' command command:config labels Dec 22, 2025
@swissspidy

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

Add WP_CLI_CACHE_EXPIRY and WP_CLI_CACHE_MAX_SIZE to the
example configuration to showcase all environment variables
that can be configured via wp-cli.yml

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Add is_array() check for $runner->config['env'] to prevent
potential warnings if the env key exists but is not an array
due to a malformed config file.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy added this to the 3.0.0 milestone Feb 23, 2026
@swissspidy

This comment was marked as resolved.

- Remove 'env' from config-spec.php so it lives in extra_config
  (consistent with other command defaults like 'config create:')
- Update get_env_or_config() to look in extra_config['env']
- Extend coverage to more WP_CLI_* env vars that are read after
  config is loaded: WP_CLI_AUTOCORRECT, WP_CLI_SUPPRESS_GLOBAL_PARAMS,
  WP_CLI_SKIP_PROMPT, WP_CLI_AUTO_UPDATE_PROMPT,
  WP_CLI_DISABLE_AUTO_CHECK_UPDATE, WP_CLI_AUTO_CHECK_UPDATE_DAYS,
  WP_CLI_SSH_PRE_CMD, WP_CLI_SSH_BINARY, WP_CLI_DOCKER_NO_TTY,
  WP_CLI_DOCKER_NO_INTERACTIVE, WP_CLI_ALIAS_GROUPS_PARALLEL,
  WP_CLI_WINDOWS_CODE_PAGE
- Add Behat tests for WP_CLI_SKIP_PROMPT, WP_CLI_AUTO_UPDATE_PROMPT,
  WP_CLI_AUTOCORRECT
- Update example YAML with all supported env vars and a note about
  quoting YAML boolean-like string values

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy swissspidy merged commit 637e73b into main Mar 13, 2026
67 of 69 checks passed
@swissspidy swissspidy deleted the copilot/add-env-var-support-wp-cli-yml branch March 13, 2026 14:29
swissspidy added a commit to apermo/wp-cli that referenced this pull request Mar 16, 2026
…6169)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
swissspidy added a commit that referenced this pull request Mar 16, 2026
…6270)

* fix(context): resolve admin user dynamically

Replace hardcoded user ID 1 fallback in Admin
context with smart user resolution. On multisite,
queries get_super_admins() to find a valid super
admin. On single site, queries for the first user
with the administrator role. Emits a clear error
if no suitable user is found.

Fixes #6269

* fix(context): optimize query and add test cases

Use get_users() with login__in for single DB query
instead of looping get_user_by(). Add single-site
error case test. Replace wp super-admin commands
with direct option manipulation in tests.

* fix(context): fix linting issues

Use single quotes for strings per PHPCS rules.
Fix gherkin use-and lint violations by replacing
consecutive When steps with And.

* fix(context): address review feedback

Revert multisite super admin lookup from get_users()
back to foreach + get_user_by('login') loop because
get_users() only fetches users on the current site
but a super admin might not be a member of any site.

Add debug logging after resolving admin user ID.

* fix(context): use instanceof for type safety

* Fix autoloader priority: locally installed packages now fully override phar-bundled versions (#6218)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>

* Add environment variable configuration support to wp-cli.yml (#6169)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>

* Autocomplete `--url` (#5704)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>

* Add WP-CLI handbook link to `wp help` output (#6273)

* Initial plan

* Add WP-CLI handbook URL reference to wp help output

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

* Update file(s) from wp-cli/.github

* Update file(s) from wp-cli/.github

* Fix SSH alias path not forwarded to remote when defined as a separate config key (#6274)

* Initial plan

* Fix SSH alias path not forwarded to remote when path is a separate config key

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

* Fix: forward active alias to runcommand subprocess (#6272)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

* Fix vagrant SSH strict host key checking failure (#6275)

* Initial plan

* Fix vagrant SSH strict host key checking failure

When ssh: vagrant is configured and vagrant ssh-config is parsed,
add -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null to
the SSH command to match what vagrant itself sets. This prevents
failures when the vagrant VM has been recreated and has a different
host key than what is stored in ~/.ssh/known_hosts.

Adds a Behat test scenario to verify the fix.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Apply suggestion from @swissspidy

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Add optional `$newline` parameter to `WP_CLI::log()` and `WP_CLI::line()` (#6276)

* Initial plan

* Add optional $newline parameter to WP_CLI::log(), WP_CLI::line(), and logger info() methods

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

* Undo Base class change

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>

* Refactor: remove unused imports (#6277)

* Harden some tests on macOS

* Update tests

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Ian Dunn <ian@iandunn.name>
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Co-authored-by: swissspidy <swissspidy@users.noreply.github.com>
Co-authored-by: Sören Wünsch <soerenwrede@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to set environment variables in wp-cli.yml

3 participants