In one of our projects, I tried setting up the CI/CD pipeline using the new Build.php
file. This, however, failed.
The following is a process that we should implement in our projects so that the CI/CD pipeline will work.
Existing issue
The main problem with the script at the moment is that it expects the WP_CLI
class to be present when the script is executed. This will only be true if the wp-cli
package has been installed using Composer. This would require an intermediary step and a modification of the Build.php
script.
A solution could be to have a simple composer.json
present where we'd have this dependency set up as a requirement, alongside any constraints (such as PHP versions) and autoloading.
Then we could just load the autoloader in the Build.php
script and all the WP_CLI
commands would run.
This does add an additional setup step that the CI server should run composer install
from the bin
folder before executing the Build.php
script.
Missing CD part and incomplete CI part
At the moment, we only have a partial CI part done. We only install the project dependencies and that's it. The CI (continuous integration) step usually implies the running of certain code checks.
Since we cannot rely on GH actions for private repositories (those have to be paid), we then need to set up checks on the CI server. Ideally from the Check.php
script.
This script will run after the Build.php
one, and will execute all the lint scripts that we are using (eslint, stylelint, phpcs and phpstan).
Also, the Build.php
script needs to run a full composer install --no-progress
command (we are not interested in the progress output) in the project repo, so that we can then run the linters in the Check.php
script.
The Check.php
script should then autoload the correct packages in order to run the linters one by one.
Deploy script
The Deploy.php
script can look similar to Build.php
(will need the autoloader like Build.php
), but the packages installed in this step must be installed for the production only.
For the JS packages that would be
npm install
- Need to install the packages
npm run build
- Need to bundle the assets
For PHP that would be
composer install --no-ansi --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader
WP_CLI::runcommand('boilerplate update');
- Deploy with the exclusions either from the
ci-exclude.txt
or from the .gitattributes
and .gitignore
files.
In this step, we wouldn't run the linters, as that step is done in the Build
part of the CI/CD pipeline.