If you are deploying your PHP web application manually by updating revision, setting proper permissions, running tests etc from command line, may be its time for you to better automate these things to save your time. In case you already tried and may be you are a little distracted to learn such build system like phing, well there is a good news for you that, you can avoid those third-party software and rather use the gnu-make software to do all the staffs 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 next few moments.
What Are The Commands Usually Needed?
Well there could be a couple of tasks such as:
- Update to latest version from source control repository.
- Update PHP Dependencies to 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 executed, is also important as well. So, we should better organize the commands into different groups and then execute them in specific order. Makefile facilitate this completely to create separate execution group and execute those in specific order. This may could be a reason, why we may prefer using makefile over a shell script as both seems similar in other cases.
Create Command Groups:
Lets categorize our necessary commands into meaningful steps. I will be using my codeigniterplus project’s build steps here as example so that you can use this one as exercise in case you don’t have any of your own project in mind at this moment.
- Update Source From Repository: Assuming the repo 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 warn you that, ‘reset’ command will undo all changes done in the repository directory which aren’t committed. So, any changes that might have been done by your application will be gone as well! so make sure to exclude such paths(cache/logs/file upload paths etc) from 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, its time to run those tests to verify nothing is broken yet. If you are using XML configuration file, then running phpunit is as simple as:
- 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:
- File permissions: This step may not be needed all the times 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:
chmod 765 application/logs chmod 765 application/cache chmod 765 application/models/proxies
- 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:
It’s Now Time To Build PHP Web Application:
Now, if we bring all together in a single ‘makefile’, it should look like something as below:
.PHONY: all update-repo dependency-install unit-tests file-permission all: update-repo dependency-install unit-tests file-permission update-repo: git reset --hard git pull origin master dependency-install: /usr/local/bin/composer update unit-tests: vendor/bin/phpunit file-permission: chmod 765 application/logs chmod 765 application/cache chmod 765 application/models/proxies
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 seperate command group using ‘make unit-tests’ or ‘make dependency-install’ as well. We are using ‘.PHONY’ section just to avoid confusion with any other existing file named similar to the command group names.
Now, lets run our command ‘make all'(or only ‘make’) and you should see some output as 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
And finally you can see that our project is completely ready to explore, which was done in a single command instead of several!
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).
Hope this will help you save time by implementing this single step build PHP web application. Happy coding 🙂