← retour aux snippets

Makefile: targets .PHONY et variables

Écrire un Makefile simple avec variables, recettes, et cibles phony.

objectif

Standardiser les commandes de build/test/deploy avec make.

code minimal

# Makefile
APP ?= api
TAG ?= latest

.PHONY: build run test push clean

build:
	docker build -t $(APP):$(TAG) .

run:
	docker run --rm -p 8080:8080 $(APP):$(TAG)

test:
	pytest -q || true

push:
	docker tag $(APP):$(TAG) registry.data.pm/$(APP):$(TAG)
	docker push registry.data.pm/$(APP):$(TAG)

clean:
	rm -rf dist .pytest_cache || true

utilisation

make build TAG=1.2.3
make test

variante(s) utile(s)

# variables avec substitution et includes
include .env.mk
REGISTRY ?= registry.data.pm
IMAGE := $(REGISTRY)/$(APP):$(TAG)

notes

  • .PHONY évite les collisions avec des fichiers réels.
  • passez des variables en ligne de commande pour surcharger.