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 <br>Code language: HTML, XML (xml)
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
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:
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 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:
<pre>chmod 765 application/logs chmod 765 application/cache chmod 765 application/models/proxies</pre>Code language: HTML, XML (xml)
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 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:
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).
I hope this will help you save time by implementing this single-step build PHP web application. Happy coding 🙂
Leave a Reply