Make WordPress Core

Changeset 60432


Ignore:
Timestamp:
07/07/2025 11:40:32 PM (9 months ago)
Author:
westonruter
Message:

Build/Test Tools: Improve dev environment's CLI in speed, non-interactive usage, and argument handling.

  • Start cli container when running env:start. This greatly speeds up calls to WP-CLI since the container is already running rather than having to start up for each call.
  • Facilitate calls to env:cli in non-interactive context (non-TTY) to allow piping content into commands or use in shell scripts.
  • Fix passing arguments to WP-CLI from env:cli so that arguments with spaces are passed as expected.
  • Fix JSHint issues.

This aligns the wordpress-develop environment closer to wp-env. See https://github.com/WordPress/gutenberg/pull/50007.

Reviewed by audrasjb.
Merges [60308] to the 6.8 branch, minus the change to .github/workflows/reusable-test-local-docker-environment-v1.yml which was made irrelevant in [60092].

Props westonruter, jorbin, SirLouen, sandeepdahiya.
Fixes #63564.

Location:
branches/6.8
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/6.8

  • branches/6.8/docker-compose.yml

    r60431 r60432  
    112112      - ./:/var/www
    113113
     114    # Keeps the service alive.
     115    command: 'sleep infinity'
     116
    114117    # The init directive ensures the command runs with a PID > 1, so Ctrl+C works correctly.
    115118    init: true
  • branches/6.8/package.json

    r60210 r60432  
    185185        "env:reset": "node ./tools/local-env/scripts/docker.js down --rmi all -v --remove-orphans",
    186186        "env:install": "node ./tools/local-env/scripts/install.js",
    187         "env:cli": "node ./tools/local-env/scripts/docker.js run --rm cli",
     187        "env:cli": "node ./tools/local-env/scripts/docker.js exec cli wp --allow-root",
    188188        "env:logs": "node ./tools/local-env/scripts/docker.js logs",
    189189        "env:pull": "node ./tools/local-env/scripts/docker.js pull",
  • branches/6.8/tools/local-env/scripts/docker.js

    r59659 r60432  
    1 const dotenv       = require( 'dotenv' );
     1/* jshint node:true */
     2
     3const dotenv = require( 'dotenv' );
    24const dotenvExpand = require( 'dotenv-expand' );
    3 const { execSync } = require( 'child_process' );
     5const { spawnSync } = require( 'child_process' );
    46const local_env_utils = require( './utils' );
    57
     
    810const composeFiles = local_env_utils.get_compose_files();
    911
    10 if (process.argv.includes('--coverage-html')) {
     12if ( process.argv.includes( '--coverage-html' ) ) {
    1113    process.env.LOCAL_PHP_XDEBUG = 'true';
    1214    process.env.LOCAL_PHP_XDEBUG_MODE = 'coverage';
    1315}
    1416
    15 // This try-catch prevents the superfluous Node.js debugging information from being shown if the command fails.
    16 try {
    17     // Execute any Docker compose command passed to this script.
    18     execSync( 'docker compose ' + composeFiles + ' ' + process.argv.slice( 2 ).join( ' ' ), { stdio: 'inherit' } );
    19 } catch ( error ) {
    20     process.exit( 1 );
     17// Add --no-TTY (-T) arg after exec and run commands when STDIN is not a TTY.
     18const dockerCommand = process.argv.slice( 2 );
     19if ( [ 'exec', 'run' ].includes( dockerCommand[0] ) && ! process.stdin.isTTY ) {
     20    dockerCommand.splice( 1, 0, '--no-TTY' );
    2121}
     22
     23// Execute any Docker compose command passed to this script.
     24const returns = spawnSync(
     25    'docker',
     26    [
     27        'compose',
     28        ...composeFiles
     29            .map( ( composeFile ) => [ '-f', composeFile ] )
     30            .flat(),
     31        ...dockerCommand,
     32    ],
     33    { stdio: 'inherit' }
     34);
     35
     36process.exit( returns.status );
  • branches/6.8/tools/local-env/scripts/install.js

    r60431 r60432  
    1515
    1616// Create wp-config.php.
    17 wp_cli( `config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force --config-file=${process.env.LOCAL_DIR}/../wp-config.php` );
     17wp_cli( `config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force --config-file="wp-config.php"` );
     18
     19// Since WP-CLI runs as root, the wp-config.php created above will be read-only. This needs to be writable for the sake of E2E tests.
     20execSync( 'node ./tools/local-env/scripts/docker.js exec cli chmod 666 wp-config.php' );
    1821
    1922// Add the debug settings to wp-config.php.
     
    6366 */
    6467function wp_cli( cmd ) {
    65     const composeFiles = local_env_utils.get_compose_files();
    66 
    67     execSync( `docker compose ${composeFiles} run --quiet-pull --rm cli ${cmd} --path=/var/www/${process.env.LOCAL_DIR}`, { stdio: 'inherit' } );
     68    execSync( `npm --silent run env:cli -- ${cmd} --path=/var/www/${process.env.LOCAL_DIR}`, { stdio: 'inherit' } );
    6869}
  • branches/6.8/tools/local-env/scripts/start.js

    r59658 r60432  
     1/* jshint node:true */
     2
    13const dotenv       = require( 'dotenv' );
    24const dotenvExpand = require( 'dotenv-expand' );
    3 const { execSync } = require( 'child_process' );
     5const { execSync, spawnSync } = require( 'child_process' );
    46const local_env_utils = require( './utils' );
    57const { constants, copyFile } = require( 'node:fs' );
    68
    79// Copy the default .env file when one is not present.
    8 copyFile( '.env.example', '.env', constants.COPYFILE_EXCL, (e) => {
     10copyFile( '.env.example', '.env', constants.COPYFILE_EXCL, () => {
    911    console.log( '.env file already exists. .env.example was not copied.' );
    1012});
     
    2931
    3032// Start the local-env containers.
    31 const containers = ( process.env.LOCAL_PHP_MEMCACHED === 'true' )
    32     ? 'wordpress-develop memcached'
    33     : 'wordpress-develop';
    34 execSync( `docker compose ${composeFiles} up --quiet-pull -d ${containers}`, { stdio: 'inherit' } );
     33const containers = [ 'wordpress-develop', 'cli' ];
     34if ( process.env.LOCAL_PHP_MEMCACHED === 'true' ) {
     35    containers.push( 'memcached' );
     36}
     37
     38spawnSync(
     39    'docker',
     40    [
     41        'compose',
     42        ...composeFiles.map( ( composeFile ) => [ '-f', composeFile ] ).flat(),
     43        'up',
     44        '--quiet-pull',
     45        '-d',
     46        ...containers,
     47    ],
     48    { stdio: 'inherit' }
     49);
    3550
    3651// If Docker Toolbox is being used, we need to manually forward LOCAL_PORT to the Docker VM.
    3752if ( process.env.DOCKER_TOOLBOX_INSTALL_PATH ) {
    3853    // VBoxManage is added to the PATH on every platform except Windows.
    39     const vboxmanage = process.env.VBOX_MSI_INSTALL_PATH ? `${ process.env.VBOX_MSI_INSTALL_PATH }/VBoxManage` : 'VBoxManage'
     54    const vboxmanage = process.env.VBOX_MSI_INSTALL_PATH ? `${ process.env.VBOX_MSI_INSTALL_PATH }/VBoxManage` : 'VBoxManage';
    4055
    4156    // Check if the port forwarding is already configured for this port.
    42     const vminfoBuffer = execSync( `"${ vboxmanage }" showvminfo "${ process.env.DOCKER_MACHINE_NAME }" --machinereadable` );
     57    const vminfoBuffer = spawnSync(
     58        vboxmanage,
     59        [
     60            'showvminfo',
     61            process.env.DOCKER_MACHINE_NAME,
     62            '--machinereadable'
     63        ]
     64    ).stdout;
    4365    const vminfo = vminfoBuffer.toString().split( /[\r\n]+/ );
    4466
     
    5476        // Delete rules that are using the port we need.
    5577        if ( rule[ 3 ] === process.env.LOCAL_PORT || rule[ 5 ] === process.env.LOCAL_PORT ) {
    56             execSync( `"${ vboxmanage }" controlvm "${ process.env.DOCKER_MACHINE_NAME }" natpf1 delete ${ rule[ 0 ] }`, { stdio: 'inherit' } );
     78            spawnSync(
     79                vboxmanage,
     80                [
     81                    'controlvm',
     82                    process.env.DOCKER_MACHINE_NAME,
     83                    'natpf1',
     84                    'delete',
     85                    rule[ 0 ]
     86                ],
     87                { stdio: 'inherit' }
     88            );
    5789        }
    5890    } );
    5991
    6092    // Add our port forwarding rule.
    61     execSync( `"${ vboxmanage }" controlvm "${ process.env.DOCKER_MACHINE_NAME }" natpf1 "tcp-port${ process.env.LOCAL_PORT },tcp,127.0.0.1,${ process.env.LOCAL_PORT },,${ process.env.LOCAL_PORT }"`, { stdio: 'inherit' } );
     93    spawnSync(
     94        vboxmanage,
     95        [
     96            'controlvm',
     97            process.env.DOCKER_MACHINE_NAME,
     98            'natpf1',
     99            `tcp-port${ process.env.LOCAL_PORT },tcp,127.0.0.1,${ process.env.LOCAL_PORT },,${ process.env.LOCAL_PORT }`
     100        ],
     101        { stdio: 'inherit' }
     102    );
    62103}
  • branches/6.8/tools/local-env/scripts/utils.js

    r59283 r60432  
     1/* jshint node:true */
     2
    13const { existsSync } = require( 'node:fs' );
    24
     
    1113     * When PHP 7.2 or 7.3 is used in combination with MySQL 8.4, an override file will also be returned to ensure
    1214     * that the mysql_native_password plugin authentication plugin is on and available for use.
     15     *
     16     * @return {string[]} Compose files.
    1317     */
    1418    get_compose_files: function() {
    15         var composeFiles = '-f docker-compose.yml';
     19        const composeFiles = [ 'docker-compose.yml' ];
    1620
    1721        if ( existsSync( 'docker-compose.override.yml' ) ) {
    18             composeFiles = composeFiles + ' -f docker-compose.override.yml';
     22            composeFiles.push( 'docker-compose.override.yml' );
    1923        }
    2024
     
    2933        // PHP 7.2/7.3 in combination with MySQL 8.4 requires additional configuration to function properly.
    3034        if ( process.env.LOCAL_DB_VERSION === '8.4' ) {
    31             composeFiles = composeFiles + ' -f tools/local-env/old-php-mysql-84.override.yml';
     35            composeFiles.push( 'tools/local-env/old-php-mysql-84.override.yml' );
    3236        }
    3337
Note: See TracChangeset for help on using the changeset viewer.