Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
209 lines (165 loc) · 5.79 KB
/
Copy pathdeploy.sh
File metadata and controls
209 lines (165 loc) · 5.79 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
set -euo pipefail
REPO_NAME="$1"
REPO="${DEPLOY_REPOS_LOCATION:-$HOME/repos}/${REPO_NAME}.git"
WORK="$(mktemp -d)"
DEFAULT_BRANCH="main"
NGINX_CERTS_DIR="/etc/nginx/certs"
cleanup() {
rm -rf "$WORK"
}
trap cleanup EXIT
require_cmd() {
command -v "$1" >/dev/null 2>&1 || { echo "Missing required command: $1" >&2; exit 1; }
}
parse_deploy_toml() {
local file="deploy.toml"
local _q='yq -p toml -oy -r'
APP_NAME="$($_q '.name' "$file")"
APP_TYPE="$($_q '.type' "$file")"
APP_HOST="$($_q '.hosts // [] | join(" ")' "$file")"
APP_PORT="$($_q '.port // ""' "$file")"
BUILD_CMD="$($_q '.build.command // ""' "$file")"
BUILD_OUT="$($_q '.build.build_location // "./"' "$file")"
DOCKERFILE="$($_q '.build.dockerfile_path // "Dockerfile"' "$file")"
# newline-separated list of "host_path:container_path" strings, or empty
APP_VOLUMES="$($_q '.build.docker_volumes // [] | .[]' "$file" 2>/dev/null || true)"
SERVE_DIR="/var/www/${APP_NAME}"
[[ -z "$APP_NAME" || "$APP_NAME" == "null" ]] && { echo "deploy.toml: 'name' is required" >&2; exit 1; }
[[ -z "$APP_TYPE" || "$APP_TYPE" == "null" ]] && { echo "deploy.toml: 'type' is required" >&2; exit 1; }
[[ "$APP_HOST" == "null" ]] && APP_HOST=""
[[ "$APP_PORT" == "null" ]] && APP_PORT=""
[[ "$BUILD_CMD" == "null" ]] && BUILD_CMD=""
[[ "$BUILD_OUT" == "null" ]] && BUILD_OUT="./"
[[ "$DOCKERFILE" == "null" ]] && DOCKERFILE="Dockerfile"
[[ "$APP_VOLUMES" == "null" ]] && APP_VOLUMES=""
return 0
}
cert_domain_from_host() {
local host="$1"
local -a parts
local count
IFS='.' read -r -a parts <<< "$host"
count="${#parts[@]}"
if (( count >= 2 )); then
printf '%s.%s\n' "${parts[count - 2]}" "${parts[count - 1]}"
else
printf '%s\n' "$host"
fi
}
write_nginx_config() {
local CONF="/etc/nginx/conf.d/${APP_NAME}.conf"
local FIRST_HOST="${APP_HOST%% *}"
local CERT_DOMAIN
[[ -z "$FIRST_HOST" ]] && { echo "deploy.toml: 'hosts' is required" >&2; exit 1; }
CERT_DOMAIN="$(cert_domain_from_host "$FIRST_HOST")"
local CERT_DIR="${NGINX_CERTS_DIR}/${CERT_DOMAIN}"
local SSL_LINES="
listen 443 ssl;
ssl_certificate ${CERT_DIR}/fullchain.cer;
ssl_certificate_key ${CERT_DIR}/${CERT_DOMAIN}.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_dhparam ${CERT_DIR}/dhparam.pem;"
local REDIRECT_BLOCK="
server {
listen 80;
server_name ${APP_HOST};
return 301 https://\$host\$request_uri;
}"
if [[ "$APP_TYPE" == "static" ]]; then
sudo tee "$CONF" > /dev/null <<EOF
${REDIRECT_BLOCK}
server {
${SSL_LINES}
server_name ${APP_HOST};
root ${SERVE_DIR};
index index.html;
location / { try_files \$uri \$uri/ /index.html; }
}
EOF
elif [[ "$APP_TYPE" == "proxy" ]]; then
sudo tee "$CONF" > /dev/null <<EOF
${REDIRECT_BLOCK}
server {
${SSL_LINES}
server_name ${APP_HOST};
location / {
proxy_pass http://127.0.0.1:${APP_PORT};
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
EOF
fi
sudo nginx -t
}
require_cmd git
require_cmd rsync
require_cmd docker
require_cmd yq
while read -r oldrev newrev ref; do
BRANCH="${ref#refs/heads/}"
[[ "$BRANCH" != "$DEFAULT_BRANCH" ]] && continue
echo "▶ Deploying ${REPO_NAME} from ${BRANCH}..."
# git archive avoids ref writes, which are forbidden in the pre-receive quarantine environment
git --git-dir="$REPO" archive "$newrev" | tar -x -C "$WORK"
cd "$WORK"
if [[ ! -f deploy.toml ]]; then
echo "deploy.toml missing in repo root" >&2
exit 1
fi
parse_deploy_toml
case "$APP_TYPE" in
static)
sudo mkdir -p "$SERVE_DIR"
[[ -n "$BUILD_CMD" ]] && eval "$BUILD_CMD"
sudo rsync -a --delete "${BUILD_OUT%/}/" "${SERVE_DIR}/"
sudo chmod 755 "$SERVE_DIR"
# Fedora/RHEL: nginx needs httpd_sys_content_t; restorecon alone is not
# enough until semanage defines a file context for this path.
if command -v restorecon >/dev/null 2>&1; then
if command -v semanage >/dev/null 2>&1; then
sudo semanage fcontext -a -t httpd_sys_content_t "${SERVE_DIR}(/.*)?" 2>/dev/null \
|| sudo semanage fcontext -m -t httpd_sys_content_t "${SERVE_DIR}(/.*)?" 2>/dev/null \
|| true
fi
sudo restorecon -Rv "$SERVE_DIR" 2>/dev/null || true
fi
write_nginx_config
sudo systemctl reload nginx
;;
proxy)
[[ -z "$APP_HOST" ]] && { echo "deploy.toml: 'hosts' is required for type=proxy" >&2; exit 1; }
[[ -z "$APP_PORT" ]] && { echo "deploy.toml: 'port' is required for type=proxy" >&2; exit 1; }
sudo docker build -f "$DOCKERFILE" -t "${APP_NAME}:latest" .
# Build -v flags from volumes declared in deploy.toml
VOLUME_FLAGS=()
if [[ -n "$APP_VOLUMES" ]]; then
while IFS= read -r vol; do
[[ -z "$vol" ]] && continue
host_path="${vol%%:*}"
sudo mkdir -p "$host_path"
VOLUME_FLAGS+=(-v "$vol")
done <<< "$APP_VOLUMES"
fi
# Bind ONLY to localhost — not exposed to internet directly
sudo docker rm -f "$APP_NAME" 2>/dev/null || true
sudo docker run -d \
--name "$APP_NAME" \
--restart unless-stopped \
-p "127.0.0.1:${APP_PORT}:${APP_PORT}" \
"${VOLUME_FLAGS[@]+"${VOLUME_FLAGS[@]}"}" \
"${APP_NAME}:latest"
write_nginx_config
sudo systemctl reload nginx
;;
*)
echo "deploy.toml: unknown type '${APP_TYPE}'" >&2
exit 1
;;
esac
echo "✓ ${APP_NAME} deployed."
done