If you are deploying your PHP web application manually by updating revision, setting proper permissions, running tests etc, from the command line, maybe it’s time for you to automate these things better to save time. In case you have already tried, and maybe you are a little distracted to learn such a build system like “phing”, well, there is good news for you, you can avoid that third-party software and rather use the gnu-make software to do all the stuff to build PHP web application that you used to do by executing several terminal commands instead.
How To Use GNU Make?
As this usually comes with most of the Linux operating systems, there is no more headache to get it installed manually anywhere at all. All we have to do is create a file named ‘makefile’ and put our commands in there and execute them! It’s that easy! really! Well, you might be already excited to start some experiments. I am going to show you to do it in the right way with examples in the next few moments.
What Are The Commands Usually Needed?
Well, there could be a couple of tasks, such as:
Update to the latest version from the source control repository.
Update PHP Dependencies to the latest versions using composer.
Run PHPUnit Tests.
Update front-end dependencies(like using bower)
Set permission on certain directories
Generate proxy classes for doctrine ORM using CLI command.
Note that the order in which the commands are required to execute is also important as well. So, we should better organize the commands into different groups and execute them in a specific order. Makefile facilitates this completely by creating separate execution groups and executing those in a specific order. This may be a reason why we may prefer using makefile over a shell script, as both seem similar in other cases.
Create Command Groups:
Let’s categorize our necessary commands into meaningful steps. I will be using my CodeIgniterPlus project’s build steps here as an example so that you can use this one as an exercise in case you don’t have any of your own projects in mind at this moment.
Update Source From Repository: Assuming the repo is on a git source repository, the following commands will do the necessary updates most of the time without any issue:
git reset --hard git pull origin master
Just to give you a heads up, the ‘reset’ command will undo all changes done in the repository directory which aren’t committed. So, any changes that might have been made by your application will be gone as well! so make sure to exclude such paths(cache/logs/file upload paths etc) from the repository by editing .gitignore file.
Installing The dependencies: Using composer to update your dependencies is as simple as the following command:
#assuming you commit the lock file with latest stable dependencies # otherwise you may try 'update' command, but not recommended as that will have chance to fail
/usr/local/bin/composer install
Running PHPUnit Tests: Assuming that you are already using phpunit tests in your application after all codes are updated, it’s time to run those tests to verify nothing is broken yet. If you are using the XML configuration file, then running PHPUnit is as simple as:
./vendor/bin/phpunit
Installing Front End Dependencies: If you are using front end package manager such as bower to install CSS/js libraries/frameworks, you will need to run that command as well:
bower install
File permissions: This step may not be needed all the time usually, but it’s still good to have in case you need to switch servers in cases so that this step doesn’t get forgotten and keep your project broken. The following commands will do such task:
Generate ORM Proxies: In case you are using doctrine, you may want to have all proxy classes created at the beginning instead of on the fly. Then you are probably using this command:
vendor/bin/doctrine orm:generate:proxies
It’s Now Time To Build PHP Web Application:
Now, if we bring it all together in a single ‘makefile’, it should look like something as below:
Now, we can run the command ‘make’ or ‘make all’ to execute all the commands in specific orders defined in ‘all: …’ section. You can also run separate command group using ‘make unit-tests’ or ‘make dependency-install’. We are using ‘.PHONY’ section just to avoid confusion with any other existing file named similar to the command group names.
Now, let’s run our command ‘make all'(or only ‘make’), and you should see some output below:
git reset --hard
HEAD is now at 9dbd0c4 * minor text changes.
git pull origin master
From github.com:ranacseruet/codeigniterplus
* branch master -> FETCH_HEAD
Already up-to-date.
/usr/local/bin/composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Removing ocramius/instantiator (1.0.0)
- Installing ocramius/instantiator (1.1.2)
Downloading: 100%
- Installing doctrine/instantiator (dev-master bbb57e0)
Cloning bbb57e04abb15f4a4eb3778158f01280bbb982cd
- Updating doctrine/dbal dev-master (6d0b048 => 83f92c3)
Checking out 83f92c3f28e927917ddcb7cf214ce99c0b72004b
Writing lock file
Generating autoload files
vendor/bin/phpunit
PHPUnit 4.4-gaef6cd6 by Sebastian Bergmann.
Configuration read from /Users/Rana/Sites/codeigniterplus/phpunit.xml
.
Time: 38 ms, Memory: 2.50Mb
OK (1 test, 1 assertion)
chmod 765 application/logs
chmod 765 application/cache
chmod 765 application/models/proxies
Code language:JavaScript(javascript)
And finally, you can see that our project is completely ready to explore, which was done in a single command instead of several!
Final Words:
You need to remember that this makefile must need to be an ignored file so that it doesn’t get updated/modified during source update while executing itself. Also, the user executing the make command should have proper write permission on the file system as well. If you are practicing with my example project above, make sure to change the database settings and commit them locally before running(otherwise doctrine ORM proxy generation step will fail).
I hope this will help you save time by implementing this single-step build PHP web application. Happy coding 🙂
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
Leave a Reply