From 88afbcc20dd4b9d4ca1065900421c6dac86382a3 Mon Sep 17 00:00:00 2001 From: Riccardo Slanzi Date: Fri, 8 Jan 2021 09:37:26 +0100 Subject: [PATCH 1/5] Added WP Cli install task --- src/tasks/wp.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tasks/wp.php b/src/tasks/wp.php index 032ed9c..2222308 100644 --- a/src/tasks/wp.php +++ b/src/tasks/wp.php @@ -9,6 +9,16 @@ require_once 'utils/localhost.php'; require_once 'utils/rsync.php'; +/** + * Install WP Cli + */ +task('wp:install-cli', function () { + $remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath(); + run("cd $remotePath && curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar"); + run("chmod +x wp-cli.phar"); + run("{{sudo_cmd}} mv wp-cli.phar /usr/local/bin/wp", ['tty' => true] ); +})->desc('Installs WP Cli on remote server'); + /** * Installs WordPress core via WP CLI * Needs the following variables: From 718880dc2b3a3f0f341890a19316d5d2c9a264da Mon Sep 17 00:00:00 2001 From: Riccardo Slanzi Date: Fri, 8 Jan 2021 09:41:29 +0100 Subject: [PATCH 2/5] Updated README for wp:install-cli --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ba756c4..ec77f34 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ You can also run `dep --list` to see all available tasks and their description. #### WordPress Tasks (`tasks/wp.php`) +- `wp:install-cli`: Installs WP CLI on remote machine - `wp:install`: Installs WordPress core via WP CLI - `wp:push`: Pushes WordPress core files via rsync - `wp:pull`: Pulls WordPress core files via rsync From ba2334b6905f0399aa55cbbc407658a4a33d23b9 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Fri, 8 Jan 2021 13:30:06 +0100 Subject: [PATCH 3/5] added WP-CLI util functions and tasks --- README.md | 8 ++++++-- src/set.php | 21 +++++++++++++++++++-- src/tasks/wp.php | 24 ++++++++---------------- src/utils/wp-cli.php | 31 +++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 src/utils/wp-cli.php diff --git a/README.md b/README.md index ec77f34..8840aae 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre - [Plugin Tasks (`tasks/plugins.php`)](#plugin-tasks-taskspluginsphp) - [MU Plugin Tasks (`tasks/mu-plugins.php`)](#mu-plugin-tasks-tasksmu-pluginsphp) - [WordPress Tasks (`tasks/wp.php`)](#wordpress-tasks-taskswpphp) + - [WP-CLI](#wp-cli) - [Simple Tasks (`tasks/simple.php`)](#simple-tasks-taskssimplephp) - [Recipes](#recipes) - [Default aka Vanilla - `deploy.php`](#default-aka-vanilla---deployphp) @@ -163,12 +164,15 @@ You can also run `dep --list` to see all available tasks and their description. #### WordPress Tasks (`tasks/wp.php`) -- `wp:install-cli`: Installs WP CLI on remote machine -- `wp:install`: Installs WordPress core via WP CLI +- `wp:download-core`: Installs WordPress core via WP CLI - `wp:push`: Pushes WordPress core files via rsync - `wp:pull`: Pulls WordPress core files via rsync - `wp:info`: Runs the --info command via WP CLI - just a helper/test task +##### WP-CLI + +You can set the `bin/wp` variable to the existing path of your WP-CLI binary or let the default `bin/wp` function download it to `{{deploy_path}}/.dep/wp-cli.phar`. So without setting the variable the binary will be downloaded on the first usage of WP-CLI. There's a task for downloading core and `--info`. You can generate your own tasks to handle other WP-CLI commands, there's a util function `Gaambo\DeployerWordpress\Utils\WPCLI\runCommand` (`src/utils/wp-cli.php`); + #### Simple Tasks (`tasks/simple.php`) - Contains some overwrites of Deployer default `deploy:*` tasks to be used in a "simple" recipe without release paths. See [Simple Recipe](#simple) diff --git a/src/set.php b/src/set.php index 1c1c4ab..c60153b 100644 --- a/src/set.php +++ b/src/set.php @@ -6,16 +6,33 @@ namespace Deployer; +use function \Gaambo\DeployerWordpress\Utils\WPCLI\installWPCLI; + require_once 'utils/localhost.php'; +require_once 'utils/wp-cli.php'; // BINARIES set('bin/npm', function () { return locateBinaryPath('npm'); }); +// can be overwritten if you eg. use wpcli in a docker container set('bin/wp', function () { - // can be overwritten if you eg. use wpcli in a docker container - return locateBinaryPath('wp'); + if (commandExist('wp')) { + return locateBinaryPath('wp'); + } + + $installPath = '{{deploy_path}}/.dep'; + $binaryFile = 'wp-cli.phar'; + + if (test("[ -f $installPath/$binaryFile ]")) { + return "{{bin/php}} $installPath/$binaryFile"; + } + + + writeln("WP-CLI binary wasn't found. Installing latest wp-cli to \"$installPath/$binaryFile\"."); + installWPCLI($installPath, $binaryFile); + return "$installPath/$binaryFile"; }); set('composer_options', 'install --no-dev'); diff --git a/src/tasks/wp.php b/src/tasks/wp.php index 2222308..f289e75 100644 --- a/src/tasks/wp.php +++ b/src/tasks/wp.php @@ -5,30 +5,23 @@ namespace Deployer; +use function Gaambo\DeployerWordpress\Utils\WPCLI\runCommand; + require_once 'utils/files.php'; require_once 'utils/localhost.php'; require_once 'utils/rsync.php'; +require_once 'utils/wp-cli.php'; /** - * Install WP Cli - */ -task('wp:install-cli', function () { - $remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath(); - run("cd $remotePath && curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar"); - run("chmod +x wp-cli.phar"); - run("{{sudo_cmd}} mv wp-cli.phar /usr/local/bin/wp", ['tty' => true] ); -})->desc('Installs WP Cli on remote server'); - -/** - * Installs WordPress core via WP CLI + * Downloads WordPress core via WP CLI * Needs the following variables: * - deploy_path or release_path: to build remote path * - bin/wp: WP CLI binary/command to use (has a default) * - wp/version: WordPress verstion to install */ -task('wp:install', function () { - $remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath(); - run("cd $remotePath && {{bin/wp}} core download --version={{wp/version}}"); +task('wp:download-core', function () { + $wpVersion = get('wp/version', 'latest'); + runCommand("core download --version=$wpVersion"); })->desc('Installs a WordPress version via WP CLI'); /** @@ -69,6 +62,5 @@ * - bin/wp: WP CLI binary/command to use (has a default) */ task('wp:info', function () { - $remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath(); - run("cd $remotePath && {{bin/wp}} --info"); + runCommand("--info"); }); diff --git a/src/utils/wp-cli.php b/src/utils/wp-cli.php new file mode 100644 index 0000000..76623bc --- /dev/null +++ b/src/utils/wp-cli.php @@ -0,0 +1,31 @@ + Date: Thu, 14 Jan 2021 11:30:51 +0100 Subject: [PATCH 4/5] add wp-cli install methods and documentation --- README.md | 66 ++++++++++++++++++++++++++++++-------------- src/set.php | 12 +++----- src/tasks/wp.php | 14 ++++++++++ src/utils/wp-cli.php | 56 +++++++++++++++++++++++++++++++------ 4 files changed, 111 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 8840aae..d1ceb16 100644 --- a/README.md +++ b/README.md @@ -13,15 +13,15 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre - [wp-config.php](#wp-configphp) - [Rsync filters/excludes/includes](#rsync-filtersexcludesincludes) - [Tasks](#tasks) - - [Database Tasks (`tasks/database.php`)](#database-tasks-tasksdatabasephp) - - [File Tasks (`tasks/files.php`)](#file-tasks-tasksfilesphp) - - [Theme Tasks (`tasks/theme.php`)](#theme-tasks-tasksthemephp) - - [Uploads Tasks (`tasks/uploads.php`)](#uploads-tasks-tasksuploadsphp) - - [Plugin Tasks (`tasks/plugins.php`)](#plugin-tasks-taskspluginsphp) - - [MU Plugin Tasks (`tasks/mu-plugins.php`)](#mu-plugin-tasks-tasksmu-pluginsphp) - - [WordPress Tasks (`tasks/wp.php`)](#wordpress-tasks-taskswpphp) - - [WP-CLI](#wp-cli) - - [Simple Tasks (`tasks/simple.php`)](#simple-tasks-taskssimplephp) + - [Database Tasks (`tasks/database.php`)](#database-tasks-tasksdatabasephp) + - [File Tasks (`tasks/files.php`)](#file-tasks-tasksfilesphp) + - [Theme Tasks (`tasks/theme.php`)](#theme-tasks-tasksthemephp) + - [Uploads Tasks (`tasks/uploads.php`)](#uploads-tasks-tasksuploadsphp) + - [Plugin Tasks (`tasks/plugins.php`)](#plugin-tasks-taskspluginsphp) + - [MU Plugin Tasks (`tasks/mu-plugins.php`)](#mu-plugin-tasks-tasksmu-pluginsphp) + - [WordPress Tasks (`tasks/wp.php`)](#wordpress-tasks-taskswpphp) + - [WP-CLI](#wp-cli) + - [Simple Tasks (`tasks/simple.php`)](#simple-tasks-taskssimplephp) - [Recipes](#recipes) - [Default aka Vanilla - `deploy.php`](#default-aka-vanilla---deployphp) - [Custom Theme](#custom-theme) @@ -30,6 +30,7 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre - [Custom Theme](#custom-theme-1) - [Custom MU-Plugin](#custom-mu-plugin-1) - [Contributing](#contributing) + - [Built by](#built-by) ## Installation @@ -109,7 +110,7 @@ This prevents any development files/development tools from syncing. I strongly r All tasks reside in the `src/tasks` directory and are documented well. Here's a summary of all tasks - for details (eg required variables/config) see their source. You can also run `dep --list` to see all available tasks and their description. -#### Database Tasks (`tasks/database.php`) +### Database Tasks (`tasks/database.php`) - `db:remote:backup`: Backup remote database and download to localhost - `db:local:backup`: Backup local database and upload to remote host @@ -118,12 +119,12 @@ You can also run `dep --list` to see all available tasks and their description. - `db:push`: Pushes local database to remote host (combines `db:local:backup` and `db:remote:import`) - `db:pull`: Pulls remote database to localhost (combines `db:remote:backup` and `db:local:import`) -#### File Tasks (`tasks/files.php`) +### File Tasks (`tasks/files.php`) - `files:push`: Pushes all files from local to remote host (combines `wp:push`, `uploads:push`, `plugins:push`, `mu-plugins:push`, `themes:push`) - `files:pull`: Pulls all files from remote to local host (combines `wp:pull`, `uploads:pull`, `plugins:pull`, `mu-plugins:pull`, `themes:pull`) -#### Theme Tasks (`tasks/theme.php`) +### Theme Tasks (`tasks/theme.php`) - `theme:assets:vendors`: Install theme assets vendors/dependencies (npm), can be run locally or remote - `theme:assets:build`: Run theme assets (npm) build script, can be run locally or remote @@ -136,7 +137,7 @@ You can also run `dep --list` to see all available tasks and their description. - `themes:backup:remote`: Backup themes on remote host and download zip - `themes:backup:local`: Backup themes on localhost -#### Uploads Tasks (`tasks/uploads.php`) +### Uploads Tasks (`tasks/uploads.php`) - `uploads:push`: Push uploads from local to remote - `uploads:pull`: Pull uploads from remote to local @@ -144,7 +145,7 @@ You can also run `dep --list` to see all available tasks and their description. - `uploads:backup:remote`: Backup uploads on remote host and download zip - `uploads:backup:local`: Backup uploads on localhost -#### Plugin Tasks (`tasks/plugins.php`) +### Plugin Tasks (`tasks/plugins.php`) - `plugins:push`: Push plugins from local to remote - `plugins:pull`: Pull plugins from remote to local @@ -152,7 +153,7 @@ You can also run `dep --list` to see all available tasks and their description. - `plugins:backup:remote`: Backup plugins on remote host and download zip - `plugins:backup:local`: Backup plugins on localhost -#### MU Plugin Tasks (`tasks/mu-plugins.php`) +### MU Plugin Tasks (`tasks/mu-plugins.php`) - `mu-plugin:vendors`: Install mu-plugin vendors (composer), can be run locally or remote - `mu-plugin`: A combined tasks - at the moment only runs mu-plugin:vendors task @@ -162,18 +163,39 @@ You can also run `dep --list` to see all available tasks and their description. - `mu-plugins:backup:remote`: Backup mu-plugins on remote host and download zip - `mu-plugins:backup:local`: Backup mu-plugins on localhost -#### WordPress Tasks (`tasks/wp.php`) +### WordPress Tasks (`tasks/wp.php`) - `wp:download-core`: Installs WordPress core via WP CLI - `wp:push`: Pushes WordPress core files via rsync - `wp:pull`: Pulls WordPress core files via rsync - `wp:info`: Runs the --info command via WP CLI - just a helper/test task -##### WP-CLI +#### WP-CLI -You can set the `bin/wp` variable to the existing path of your WP-CLI binary or let the default `bin/wp` function download it to `{{deploy_path}}/.dep/wp-cli.phar`. So without setting the variable the binary will be downloaded on the first usage of WP-CLI. There's a task for downloading core and `--info`. You can generate your own tasks to handle other WP-CLI commands, there's a util function `Gaambo\DeployerWordpress\Utils\WPCLI\runCommand` (`src/utils/wp-cli.php`); +Handling and installing the WP-CLI binary can be done in one of multiple ways: -#### Simple Tasks (`tasks/simple.php`) +1. The default `bin/wp` in `set.php` checks for a usable WP-CLI binary and if none is found it downloads and installs it to `{{deploy_path}}/.dep/wp-cli.phar` (this path is checked in the future as well). +2. If you want this behaviour (check if installed, else install) but in another path, overwrite the `bin/wp` variable with a function: + ```php + set('bin/wp', function() { + if($path = getWPCLIBinary()) { + return $path; + } + return installWPCLI('/usr/local/bin', 'wp', true); + } + ``` + This would install the WP-CLI binary into `/usr/local/bin` with sudo, since this path is probably in $PATH it's found via `getWPCLIBinary` the next time. + +3. Set the `bin/wp` variable path on the host configuration, if WP-CLI is already installed. +4. Install the WP-CLI binary manually with the `wp:install-wpcli` task and set the path as `/bin/wp` afterwards. +You can pass the installPath, binaryFile and sudo usage via CLI: +`dep wp:install-wpcli production -o installPath='{{deploy_path}}/.bin -o binaryFile=wp -o sudo=true` + +See [original PR](https://github.com/gaambo/deployer-wordpress/pull/5) for more information. + +There's a task for downloading core and `--info`. You can generate your own tasks to handle other WP-CLI commands, there's a util function `Gaambo\DeployerWordpress\Utils\WPCLI\runCommand` (`src/utils/wp-cli.php`); + +### Simple Tasks (`tasks/simple.php`) - Contains some overwrites of Deployer default `deploy:*` tasks to be used in a "simple" recipe without release paths. See [Simple Recipe](#simple) @@ -228,4 +250,8 @@ Installing PHP/composer vendors/dependencies is done on the server. The `mu-plug ## Contributing If you have feature requests, find bugs or need help just open an issue on [GitHub](https://github.com/gaambo/deployer-wordpress). -Pull requests are always welcome. PSR2 coding standard are used I try to adhere to Deployer best-practices. +Pull requests are always welcome. PSR2 coding standard are used and I try to adhere to Deployer best-practices. + +## Built by + +[Gaambo](https://github.com/gaambo) and [Contributors](https://github.com/gaambo/deployer-wordpress/graphs/contributors) \ No newline at end of file diff --git a/src/set.php b/src/set.php index c60153b..33fc312 100644 --- a/src/set.php +++ b/src/set.php @@ -6,6 +6,7 @@ namespace Deployer; +use function Gaambo\DeployerWordpress\Utils\WPCLI\getWPCLIBinary; use function \Gaambo\DeployerWordpress\Utils\WPCLI\installWPCLI; require_once 'utils/localhost.php'; @@ -18,21 +19,16 @@ // can be overwritten if you eg. use wpcli in a docker container set('bin/wp', function () { - if (commandExist('wp')) { - return locateBinaryPath('wp'); + if ($path = getWPCLIBinary()) { + return $path; } $installPath = '{{deploy_path}}/.dep'; $binaryFile = 'wp-cli.phar'; - if (test("[ -f $installPath/$binaryFile ]")) { - return "{{bin/php}} $installPath/$binaryFile"; - } - - writeln("WP-CLI binary wasn't found. Installing latest wp-cli to \"$installPath/$binaryFile\"."); + installWPCLI($installPath, $binaryFile); - return "$installPath/$binaryFile"; }); set('composer_options', 'install --no-dev'); diff --git a/src/tasks/wp.php b/src/tasks/wp.php index f289e75..b011d81 100644 --- a/src/tasks/wp.php +++ b/src/tasks/wp.php @@ -5,6 +5,7 @@ namespace Deployer; +use function Gaambo\DeployerWordpress\Utils\WPCLI\installWPCLI; use function Gaambo\DeployerWordpress\Utils\WPCLI\runCommand; require_once 'utils/files.php'; @@ -64,3 +65,16 @@ task('wp:info', function () { runCommand("--info"); }); + +/** + * Installs the WP-CLI binary - for usage via CLI + * Pass installPath, binaryFile and sudo via CLI like so: + * `dep wp:install-wpcli production -o installPath='{{deploy_path}}/.bin -o binaryFile=wp -o sudo=true` + */ +task('wp:install-wpcli', function () { + $installPath = get('installPath'); + $binaryFile = get('binaryFile', 'wp-cli.phar'); + $sudo = get('sudo', false); + + installWPCLI($installPath, $binaryFile, $sudo); +}); diff --git a/src/utils/wp-cli.php b/src/utils/wp-cli.php index 76623bc..a9c8946 100644 --- a/src/utils/wp-cli.php +++ b/src/utils/wp-cli.php @@ -7,15 +7,6 @@ const INSTALLER_DOWNLOAD = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar'; -function installWPCLI($installPath, $binaryName = 'wp-cli.phar') -{ - \Deployer\run("mkdir -p $installPath"); - \Deployer\run("cd $installPath && curl -sS -O " . INSTALLER_DOWNLOAD . " && chmod +x wp-cli.phar"); - if ($binaryName !== 'wp-cli.phar') { - \Deployer\run("mv $installPath/wp-cli.phar $installPath/$binaryName"); - } -} - /** * Runs any WP-CLI command * Passes on the verbosity flags passed to Deployer CLI @@ -29,3 +20,50 @@ function runCommand(string $command, string $path = '{{deploy_path}}', string $a { return \Deployer\run("cd $path && {{bin/wp}} $command $arguments"); } + +/** + * Gets the path to the WP-CLI binary + * + * Uses locateBinaryPath to get a global binary + * or alternatively check in a given path for a file + * + * Default path/file to check is {{deploy_path}}/.dep/wp-cli.phar + * Which is the same as the default path PHPDeployer installs composer if not installed + * + * @param string $path Path in which the WP-CLI binary is stored + * @param string $binaryFile Name of the WP-CLI binary file + * @return string|boolean Path to binary file or false if not found + */ +function getWPCLIBinary($path = '{{deploy_path}}/.dep', $binaryFile = 'wp-cli.phar') +{ + if (\Deployer\commandExist('wp')) { + return \Deployer\locateBinaryPath('wp'); + } + + if (\Deployer\test("[ -f $path/$binaryFile ]")) { + return "{{bin/php}} $path/$binaryFile"; + } + return false; +} + +/** + * Installs the WP-CLI Binary to a specified installPath + * Allows passing a name for the binary file and using sudo (eg to move to /usr/local/bin) + * + * @param string $installPath + * @param string $binaryName + * @param boolean $sudo + * @return string Path to installed and moved binary file + */ +function installWPCLI($installPath, $binaryName = 'wp-cli.phar', $sudo = false) +{ + $sudoCommand = $sudo ? 'sudo ' : ''; + + \Deployer\run("mkdir -p $installPath"); + \Deployer\run("cd $installPath && curl -sS -O " . INSTALLER_DOWNLOAD . " && chmod +x wp-cli.phar"); + if ($binaryName !== 'wp-cli.phar') { + \Deployer\run("$sudoCommand mv $installPath/wp-cli.phar $installPath/$binaryName"); + } + + return "$installPath/$binaryName"; +} From 5c84c76ac9526f8c286ec3e8bd2a6f5d4f6b56b9 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Thu, 14 Jan 2021 11:34:43 +0100 Subject: [PATCH 5/5] fix typo in doc --- src/tasks/wp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/wp.php b/src/tasks/wp.php index b011d81..02fd9e6 100644 --- a/src/tasks/wp.php +++ b/src/tasks/wp.php @@ -69,7 +69,7 @@ /** * Installs the WP-CLI binary - for usage via CLI * Pass installPath, binaryFile and sudo via CLI like so: - * `dep wp:install-wpcli production -o installPath='{{deploy_path}}/.bin -o binaryFile=wp -o sudo=true` + * `dep wp:install-wpcli production -o installPath=/usr/local/bin -o binaryFile=wp -o sudo=true` */ task('wp:install-wpcli', function () { $installPath = get('installPath');