Would like to ask, I made a simple php script to auto deploy from bitbucket when new set of code is being pushed to the repository which work fine. but from time to time there will be changes made to the composer.json file. So what I've done at this moment is by calling exec('composer update') every time the PHP script is being fired, which could be time consuming even though there are no changes made to the composer.json file.
Is there any way that I can call PHP to check if the composer.json file has been changed then only execute exec('composer update') ?
Is there any way that I can call PHP to check if the composer.json file has been changed then only execute exec('composer update') ?
I would suggest to do a history check of just one file from the repo by using "git diff" or compare/check the modification date of the file.
Examples:
git --no-pager diff --name-only HEAD~50 -- ./composer.json
git --no-pager diff --name-only origin/master~15 -- ./composer.json
If the file name is returned, then the file was changed within the commit range.
You might need to change the number of commits the diff goes back in the history to look for a change, but here is a rough draft to get you started:
<?php
function fileChanged() {
$cmd = 'git --no-pager diff --name-only HEAD~50 -- ./composer.json';
exec($cmd, $output);
return ($output[0] === 'composer.json') ? true : false;
}
if(fileChanged()) {
exec('composer update');
} else {
echo 'Found no change to "composer.json" with 50 commits. Skipping "composer update"';
}
Also for an auto-deployment pushing the composer.lock file to the repo and switching from running composer update to composer install could help, because Composer wouldn't need to do dependency resolution and version lookups, which improves deployment speed.
Running composer install will:
composer.lock exists
composer update to create itcomposer.lock exists, install the specified versions from the lock fileRunning composer update will:
composer.jsoncomposer.lock to reflect the latest versions installedSidenote: I remember a similar or pretty close feature request (--install-only-if-changed) on the Github tracker: https://github.com/composer/composer/issues/3888.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With