Remember all the commands of a project with Makefile
Problematic
When we want to build our application, whatever the language, there are usually a ton of commands to remember.
Here is an example that we can find when we develop in PHP and Javascript:
The problem is: the more technologies we have, the more terminal commands we have to remember.
And if we have several projects at the same time, we run the risk of getting confused by forgetting the commands.
And now, imagine leaving this project for 2 months, and trying to re-understand how to compile your project :/
Makefile
One solution I use on a daily basis is to use a Makefile. It is an utility installed by default in all linux distributions, used first for the C language.
Basically, Makefile is a utility that scans files and runs commands whether files need to be recompiled or not. However, here, we can use it to “store” the important commands of our project.
In software development, Make is a build automation tool that automatically builds executable programs and libraries from source code by reading files called Makefiles which specify how to derive the target program. Though integrated development environments and language-specific compiler features can also be used to manage a build process, Make remains widely used, especially in Unix and Unix-like operating systems. Wikipedia
The basic structure
The usage is very simple. First, create a file named Makefile
. The syntax inside is very simple:
You must indent with a tabulation and not 4 spaces!
Here is an example of a real case:
The help command
Paste this on the top of your Makefile:
Then use the make
command and press enter ! 🎉
Makefile tips
Use environment variables from .env
file
First create a .env
file in the root project directory and define your variables like this:
Then, we can include
the file and use the variable with the $(VARIABLE)
syntax:
Hide commands:
In some cases, if we use password or API keys in our commands, we can write a @
in front of our commands like this:
Here, the command @deploy $(SSH_URL) $(SSH_KEY)
won’t be displayed on screen.
Define variables
We can define variables on top of the file like this:
Use global variables
If we want to export locally variables for our tasks, we can put this line on the top of the file:
Note: the exported variables are only available in the Makefile context.
Full example
Here is a full example:
The SOLID/STUPID principles
Learn what are the SOLID and STUPID principles with examples
Create a Docker Swarm playground
Let's create Docker Swarm playground on your local machine
Setup a Kubernetes cluster with K3S, Traefik, CertManager and Kubernetes Dashboard
Let's setup step by step our own K3S cluster !
Create an Ansible playground with Docker
Let's create an Ansible playground with Docker
Database ACID/BASE - Understanding the CAP Theorem
Learn what is the CAP Theorem in less than 5 minutes !
HashiCorp Vault - Technological watch
Learn what is HashiCorp Vault in less than 5 minutes !
How to internationalize an AstroJS website while maintaining good SEO ?
We will see how to create an implementation of i18n with AstroJS
LFTP - Deploy an application in command line
Here we will see how to automatically deploy an application with lftp in command line.