#!/bin/bash # Deploy Paper Dynasty Database API # Usage: ./scripts/deploy.sh [commit] # # Dev: Force-updates the "dev" git tag → CI builds :dev Docker image # Prod: Creates CalVer tag + force-updates "latest" and "production" git tags # → CI builds :, :latest, :production Docker images set -euo pipefail RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' REMOTE="origin" usage() { echo "Usage: $0 [commit]" echo "" echo " dev [commit] Force-update 'dev' tag on HEAD or specified commit" echo " prod [commit] Create CalVer + 'latest' + 'production' tags on HEAD or specified commit" exit 1 } [[ $# -lt 1 ]] && usage ENV="$1" COMMIT="${2:-HEAD}" SHA=$(git rev-parse "$COMMIT" 2>/dev/null) || { echo -e "${RED}Invalid commit: $COMMIT${NC}" exit 1 } SHA_SHORT="${SHA:0:7}" git fetch --tags "$REMOTE" if ! git branch -a --contains "$SHA" 2>/dev/null | grep -qE '(^|\s)(main|remotes/origin/main)$'; then echo -e "${RED}Commit $SHA_SHORT is not on main. Aborting.${NC}" exit 1 fi case "$ENV" in dev) echo -e "${YELLOW}Deploying to dev...${NC}" echo -e " Commit: ${SHA_SHORT}" git tag -f dev "$SHA" git push "$REMOTE" dev --force echo -e "${GREEN}Tagged ${SHA_SHORT} as 'dev' and pushed. CI will build :dev image.${NC}" ;; prod) echo -e "${YELLOW}Deploying to prod...${NC}" YEAR=$(date -u +%Y) MONTH=$(date -u +%-m) PREFIX="${YEAR}.${MONTH}." LAST_BUILD=$(git tag -l "${PREFIX}*" | sed "s/^${PREFIX}//" | sort -n | tail -1) BUILD=$((${LAST_BUILD:-0} + 1)) CALVER="${PREFIX}${BUILD}" echo -e " Commit: ${SHA_SHORT}" echo -e " Version: ${CALVER}" echo -e " Tags: ${CALVER}, latest, production" read -rp "Proceed? [y/N] " confirm [[ "$confirm" =~ ^[Yy]$ ]] || { echo "Aborted." exit 0 } git tag "$CALVER" "$SHA" git tag -f latest "$SHA" git tag -f production "$SHA" git push "$REMOTE" "$CALVER" git push "$REMOTE" latest --force git push "$REMOTE" production --force echo -e "${GREEN}Tagged ${SHA_SHORT} as '${CALVER}', 'latest', 'production' and pushed.${NC}" echo -e "${GREEN}CI will build :${CALVER}, :latest, :production images.${NC}" ;; *) usage ;; esac