-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathdeploy.sh.erb
119 lines (103 loc) · 3.24 KB
/
deploy.sh.erb
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
116
117
118
119
<%
default = commands.delete(:default).map { |s| indent 2, s }.join(" &&\n")
build = commands.delete(:build).map { |s| indent 2, s }.join(" &&\n")
launch = commands.delete(:launch).map { |s| indent 2, s }.join(" &&\n")
clean = commands.delete(:clean).map { |s| indent 2, s }.join(" &&\n")
%>
#!/usr/bin/env bash
# Go to the deploy path
cd "<%= fetch(:deploy_to) %>" || (
echo "! ERROR: not set up."
echo "The path '<%= fetch(:deploy_to) %>' is not accessible on the server."
echo "You may need to run 'mina setup' first."
false
) || exit 15
# Check releases path
if [ ! -d "<%= fetch(:releases_path) %>" ]; then
echo "! ERROR: not set up."
echo "The directory '<%= fetch(:releases_path) %>' does not exist on the server."
echo "You may need to run 'mina setup' first."
exit 16
fi
# Check lockfile
if [ -e "<%= fetch(:lock_file) %>" ]; then
echo "! ERROR: another deployment is ongoing."
echo "The file '<%= fetch(:lock_file) %>' was found. File was last modified at $(stat -c %y <%= fetch(:lock_file) %>)"
echo "If no other deployment is ongoing, run 'mina deploy:force_unlock' to delete the file."
exit 17
fi
# Determine $previous_path and other variables
[ -h "<%= fetch(:current_path) %>" ] && [ -d "<%= fetch(:current_path) %>" ] && previous_path=$(cd "<%= fetch(:current_path) %>" >/dev/null && pwd -LP)
build_path="./tmp/build-$(date +%s)$RANDOM"
version="<%= next_version %>"
release_path="<%= fetch(:releases_path) %>/$version"
# Sanity check
if [ -e "$build_path" ]; then
echo "! ERROR: Path already exists."
exit 18
fi
# Bootstrap script (in deployer)
(
echo "-----> Creating a temporary build path"
<%= echo_cmd %[touch "#{fetch(:lock_file)}"] %> &&
<%= echo_cmd %[mkdir -p "$build_path"] %> &&
<%= echo_cmd %[cd "$build_path"] %> &&
(
<%= indent 2, (default.empty? ? "true" : default) %>
) &&
echo "-----> Deploy finished"
) &&
#
# Build
(
echo "-----> Building"
echo "-----> Moving build to $release_path"
<%= echo_cmd %[mv "$build_path" "$release_path"] %> &&
<%= echo_cmd %[cd "$release_path"] %> &&
(
<%= indent 2, (build.empty? ? "true" : build) %>
) &&
echo "-----> Build finished"
) &&
#
# Launching
# Rename to the real release path, then symlink 'current'
(
echo "-----> Launching"
echo "-----> Updating the <%= fetch(:current_path) %> symlink" &&
<%= echo_cmd %[ln -nfs "$release_path" "#{fetch(:current_path)}"] %>
) &&
# ============================
# === Start up server => (in deployer)
(
<%= echo_cmd %[cd "#{fetch(:current_path)}"] %>
<%= indent 2, (launch.empty? ? "true" : launch) %>
) &&
# ============================
# === Complete & unlock
(
rm -f "<%= fetch(:lock_file) %>"
echo "-----> Done. Deployed version $version"
) ||
# ============================
# === Failed deployment
(
echo "! ERROR: Deploy failed."
<%= indent 2, clean %>
echo "-----> Cleaning up build"
[ -e "$build_path" ] && (
<%= echo_cmd %[rm -rf "$build_path"] %>
)
[ -e "$release_path" ] && (
echo "Deleting release"
<%= echo_cmd %[rm -rf "$release_path"] %>
)
(
echo "Unlinking current"
[ -n "$previous_path" ] && <%= echo_cmd %[ln -nfs "$previous_path" "#{fetch(:current_path)}"] %>
)
# Unlock
<%= echo_cmd %[rm -f "#{fetch(:lock_file)}"] %>
echo "OK"
exit 19
)