-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·41 lines (30 loc) · 1.19 KB
/
deploy.sh
File metadata and controls
executable file
·41 lines (30 loc) · 1.19 KB
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
#!/usr/bin/env bash
set -euo pipefail
# Usage: ./deploy.sh [commit-ish]
# When no commit is supplied, the script deploys the current branch's HEAD.
commit_ref="${1:-HEAD}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if ! REPO_ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null)"; then
echo "Error: $SCRIPT_DIR is not inside a git repository." >&2
exit 1
fi
if ! commit="$(git -C "$REPO_ROOT" rev-parse --verify "$commit_ref" 2>/dev/null)"; then
echo "Error: Unable to resolve commit '$commit_ref'." >&2
exit 1
fi
DEPLOY_ROOT="$REPO_ROOT/releases"
CURRENT_DIR="$DEPLOY_ROOT/current"
BACKUP_DIR="$DEPLOY_ROOT/backups"
TMP_DIR="$DEPLOY_ROOT/.deploy_tmp"
mkdir -p "$CURRENT_DIR" "$BACKUP_DIR"
# Ensure we start from a clean staging area before checking out the commit.
rm -rf "$TMP_DIR"
mkdir -p "$TMP_DIR"
git -C "$REPO_ROOT" archive "$commit" | tar -C "$TMP_DIR" -xf -
timestamp="$(date +%Y%m%d-%H%M%S)"
backup_path="$BACKUP_DIR/$timestamp"
rsync -a --delete --exclude=".git" "$TMP_DIR/" "$CURRENT_DIR/"
rsync -a --delete "$TMP_DIR/" "$backup_path/"
echo "$commit" > "$backup_path/.commit"
echo "Deployed commit $commit to $CURRENT_DIR"
echo "Backup created at $backup_path"