summaryrefslogtreecommitdiff
path: root/scripts/deploy
blob: 3ee0baeffba0f7c9fd38c39fc6263c35cb866cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# deploy — Build and deploy claudomator to /site/doot.terst.org
# Usage: ./scripts/deploy [--dirty] [--branch <name>]
#   --branch <name>  Deploy a named local branch as claudomator-<name> (e.g. oss)
#   --dirty          Skip stash check (main deploy only)
# Example: sudo ./scripts/deploy
#          sudo ./scripts/deploy --branch oss

set -euo pipefail

DIRTY=false
BRANCH=""
while [[ $# -gt 0 ]]; do
  case "$1" in
    --dirty) DIRTY=true; shift ;;
    --branch) BRANCH="$2"; shift 2 ;;
    --branch=*) BRANCH="${1#--branch=}"; shift ;;
    *) echo "Unknown argument: $1" >&2; exit 1 ;;
  esac
done

FQDN="doot.terst.org"
SITE_DIR="/site/${FQDN}"
BIN_DIR="${SITE_DIR}/bin"
REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)"

export GOCACHE="${SITE_DIR}/cache/go-build"
export GOPATH="${SITE_DIR}/cache/gopath"
mkdir -p "${GOCACHE}" "${GOPATH}"

if [ -n "$BRANCH" ]; then
  # ── Branch deploy ──────────────────────────────────────────────────────────
  # Builds a named branch in an isolated git worktree so the main checkout is
  # undisturbed. The resulting binary is deployed as claudomator-<branch> and
  # managed by the claudomator-<branch>@<fqdn> service.
  BINARY_NAME="claudomator-${BRANCH}"
  SERVICE="claudomator-${BRANCH}@${FQDN}.service"
  WORKTREE="/tmp/claudomator-deploy-${BRANCH}"

  cleanup() { git -C "${REPO_DIR}" worktree remove "${WORKTREE}" --force 2>/dev/null || true; }
  trap cleanup EXIT

  cd "${REPO_DIR}"

  echo "==> Fetching all remotes..."
  git fetch --all 2>/dev/null || git fetch local 2>/dev/null || true

  echo "==> Setting up worktree for ${BRANCH}..."
  git worktree remove "${WORKTREE}" --force 2>/dev/null || true
  git worktree add "${WORKTREE}" "${BRANCH}"

  echo "==> Fast-forwarding ${BRANCH} to tracking remote..."
  git -C "${WORKTREE}" merge --ff-only "@{u}" 2>/dev/null || echo "    (no upstream or already current)"

  echo "==> Verifying ${BRANCH} (build + tests)..."
  (cd "${WORKTREE}" && go build ./... && go test -race ./...)
  echo "==> All checks passed."

  echo "==> Building ${BINARY_NAME}..."
  (cd "${WORKTREE}" && go build -o "${BIN_DIR}/${BINARY_NAME}" ./cmd/claudomator/)

else
  # ── Main deploy ────────────────────────────────────────────────────────────
  BINARY_NAME="claudomator"
  SERVICE="claudomator@${FQDN}.service"

  cd "${REPO_DIR}"

  echo "==> Pulling latest from bare repo..."
  git pull --ff-only local main

  STASHED=false
  if [ "$DIRTY" = false ] && [ -n "$(git status --porcelain)" ]; then
      echo "==> Stashing uncommitted changes..."
      git stash push -u -m "Auto-stash before deploy"
      STASHED=true
      trap 'if [ "$STASHED" = true ]; then echo "==> Popping stash..."; git stash pop; fi' EXIT
  fi

  echo "==> Verifying (build + tests)..."
  "${REPO_DIR}/scripts/verify"

  echo "==> Building claudomator..."
  go build -o "${BIN_DIR}/claudomator" ./cmd/claudomator/

  echo "==> Copying scripts..."
  mkdir -p "${SITE_DIR}/scripts"
  find "${REPO_DIR}/scripts" -maxdepth 1 -type f -exec cp -p {} "${SITE_DIR}/scripts/" \;

  echo "==> Installing to /usr/local/bin..."
  install -m 755 "${BIN_DIR}/claudomator" /usr/local/bin/claudomator

  echo "==> Verifying system CLI version..."
  /usr/local/bin/claudomator version

  echo "==> Fixing permissions..."
  "${REPO_DIR}/scripts/fix-permissions"

  echo "==> Syncing credentials..."
  "${REPO_DIR}/scripts/sync-credentials"

  echo "==> Ensuring binary and scripts are executable..."
  chmod +x "${BIN_DIR}/claudomator" /usr/local/bin/claudomator
  find "${SITE_DIR}/scripts" -maxdepth 1 -type f -exec chmod +x {} +
fi

echo "==> Ensuring ${BINARY_NAME} is executable..."
chmod +x "${BIN_DIR}/${BINARY_NAME}"
chown www-data:www-data "${BIN_DIR}/${BINARY_NAME}"

echo "==> Restarting ${SERVICE}..."
sudo systemctl restart "${SERVICE}"
sudo systemctl status "${SERVICE}" --no-pager -l

echo "==> Done!"