Skip to content
Closed

vi #1230

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ export GIT_VERSION ?= $(shell git rev-parse --short HEAD)

# Variables for deploy
# ====================
# Auto-detect and sanitize current git branch
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
# Replace "/" and other non-alphanumeric characters with "-"
SAFE_BRANCH := $(shell echo "$(BRANCH)" | sed 's/[^A-Za-z0-9-]/-/g')
FORCE_DEPLOY ?= false
SITE_URL ?= "https://$(SAFE_BRANCH).ep-preview.click"

.PHONY: build deploy dev clean install
.PHONY: build deploy dev clean install safe_branch preview clean-previews prune-prod-releases

safe_branch:
@echo $(SAFE_BRANCH)
Expand Down Expand Up @@ -53,18 +51,36 @@ preview:
@echo "\n\n**** Deploying preview of a branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
$(REMOTE_CMD) "mkdir -p $(TARGET)"
rsync -avz --delete ./dist/ $(VPS_USER)@$(VPS_HOST):$(TARGET)/
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current"
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current && \
find . -maxdepth 1 -mindepth 1 -type d ! -name $(TIMESTAMP) -exec rm -rf {} +"
@echo "\n\n**** Preview complete.\n\n"
@echo "Open the preview site at: $(PREVIEW_SITE_URL)\n\n"


ifeq ($(FORCE_DEPLOY), true)
deploy: RELEASES_DIR = $(VPS_PROD_PATH)/releases
deploy: TARGET = $(RELEASES_DIR)/$(TIMESTAMP)
deploy: clean-previews prune-prod-releases
deploy:
@echo "\n\n**** Deploying branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
$(REMOTE_CMD) "mkdir -p $(TARGET)"
rsync -avz --delete ./dist/ $(VPS_USER)@$(VPS_HOST):$(TARGET)/
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current"
@echo "\n\n**** Deployment complete.\n\n"
endif

# Cleanup preview builds that no longer match any remote branches
clean-previews:
@echo "\n\n**** Cleaning up preview builds of deleted branches...\n\n"
$(REMOTE_CMD) 'cd $(VPS_PREVIEW_PATH) && \
for d in *; do \
if ! git ls-remote --heads https://github.com/YOUR_ORG/YOUR_REPO.git "$$d" | grep -q "$$d"; then \
echo "Removing preview branch: $$d"; \
rm -rf "$$d"; \
fi \
done'

# Keep only latest 10 production builds
prune-prod-releases:
@echo "\n\n**** Pruning old production builds, keeping latest 10...\n\n"
$(REMOTE_CMD) 'cd $(VPS_PROD_PATH)/releases && \
ls -1dt */ | tail -n +11 | xargs -r rm -rf'