@@ -15,14 +15,12 @@ export GIT_VERSION ?= $(shell git rev-parse --short HEAD)
1515
1616# Variables for deploy
1717# ====================
18- # Auto-detect and sanitize current git branch
1918BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
20- # Replace "/" and other non-alphanumeric characters with "-"
2119SAFE_BRANCH := $(shell echo "$(BRANCH ) " | sed 's/[^A-Za-z0-9-]/-/g')
2220FORCE_DEPLOY ?= false
2321SITE_URL ?= "https://$(SAFE_BRANCH ) .ep-preview.click"
2422
25- .PHONY : build deploy dev clean install
23+ .PHONY : build deploy dev clean install safe_branch preview clean-previews prune-prod-releases
2624
2725safe_branch :
2826 @echo $(SAFE_BRANCH )
@@ -53,18 +51,36 @@ preview:
5351 @echo " \n\n**** Deploying preview of a branch '$( BRANCH) ' (safe: $( SAFE_BRANCH) ) to $( TARGET) ...\n\n"
5452 $(REMOTE_CMD ) " mkdir -p $( TARGET) "
5553 rsync -avz --delete ./dist/ $(VPS_USER ) @$(VPS_HOST ) :$(TARGET ) /
56- $(REMOTE_CMD ) " cd $( RELEASES_DIR) && ln -snf $( TIMESTAMP) current" && vi
54+ $(REMOTE_CMD ) " cd $( RELEASES_DIR) && ln -snf $( TIMESTAMP) current && \
55+ find . -maxdepth 1 -mindepth 1 -type d ! -name $(TIMESTAMP ) -exec rm -rf {} +"
5756 @echo " \n\n**** Preview complete.\n\n"
5857 @echo " Open the preview site at: $( PREVIEW_SITE_URL) \n\n"
5958
60-
6159ifeq ($(FORCE_DEPLOY ) , true)
6260deploy : RELEASES_DIR = $(VPS_PROD_PATH ) /releases
6361deploy : TARGET = $(RELEASES_DIR ) /$(TIMESTAMP )
62+ deploy : clean-previews prune-prod-releases
6463deploy :
6564 @echo " \n\n**** Deploying branch '$( BRANCH) ' (safe: $( SAFE_BRANCH) ) to $( TARGET) ...\n\n"
6665 $(REMOTE_CMD ) " mkdir -p $( TARGET) "
6766 rsync -avz --delete ./dist/ $(VPS_USER ) @$(VPS_HOST ) :$(TARGET ) /
6867 $(REMOTE_CMD ) " cd $( RELEASES_DIR) && ln -snf $( TIMESTAMP) current"
6968 @echo " \n\n**** Deployment complete.\n\n"
7069endif
70+
71+ # Cleanup preview builds that no longer match any remote branches
72+ clean-previews :
73+ @echo " \n\n**** Cleaning up preview builds of deleted branches...\n\n"
74+ $(REMOTE_CMD ) ' cd $(VPS_PREVIEW_PATH) && \
75+ for d in * ; do \
76+ if ! git ls-remote --heads https://github.com/YOUR_ORG/YOUR_REPO.git " $$ d" | grep -q " $$ d" ; then \
77+ echo " Removing preview branch: $$ d" ; \
78+ rm -rf " $$ d" ; \
79+ fi \
80+ done'
81+
82+ # Keep only latest 10 production builds
83+ prune-prod-releases :
84+ @echo " \n\n**** Pruning old production builds, keeping latest 10...\n\n"
85+ $(REMOTE_CMD ) ' cd $(VPS_PROD_PATH)/releases && \
86+ ls -1dt * / | tail -n +11 | xargs -r rm -rf'
0 commit comments