Skip to content

Instantly share code, notes, and snippets.

@koko-ng
Created July 28, 2021 22:16
Show Gist options
  • Save koko-ng/29277fe712bb0c587b664d6a56e6f375 to your computer and use it in GitHub Desktop.
Save koko-ng/29277fe712bb0c587b664d6a56e6f375 to your computer and use it in GitHub Desktop.
Automatically install wp-cli on first connection
<?php
/**
* Automatically install wp-cli on remote hosts if absent
*
* @author koko-ng <koko@kokong.info>
* @package koko-ng/wp-cli-config
*/
WP_CLI::add_hook(
'before_ssh',
function () {
$host = WP_CLI\Utils\parse_ssh_url(
WP_CLI::get_runner()->config['ssh'],
PHP_URL_HOST
);
$scheme = WP_CLI\Utils\parse_ssh_url(
WP_CLI::get_runner()->config['ssh'],
PHP_URL_SCHEME
);
// Default home in docker images is /var/www.
if ( 'docker' === $scheme ) {
$home = '/tmp';
} else {
$home = '$HOME';
}
putenv(
sprintf(
'WP_CLI_SSH_PRE_CMD=[ ! -f "%1$s/.bin/wp" ] && (mkdir -p %1$s/.bin; cd %1$s/.bin/; curl -o wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar; chmod +x %1$s/.bin/wp); export PATH=%1$s/.bin/:$PATH',
$home
)
);
}
);
@koko-ng
Copy link
Author

koko-ng commented Jul 28, 2021

Automatically install wp-cli on remote host

wp-cli is installed and added to path if it isn't available.

Installation

Save the above file in ~/.wp-cli/
In ~/.wp-cli/config.yml put

require:
  - pre-ssh.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment