-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverfull.sh
More file actions
154 lines (126 loc) · 4.53 KB
/
Copy pathserverfull.sh
File metadata and controls
154 lines (126 loc) · 4.53 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
#!/bin/bash
# Ensure a directory name was provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <directory-name>"
echo "Example: $0 myapp"
exit 1
fi
APP_NAME="$1"
CURRENT_DIR="$(pwd)"
# Create the basic directory structure
echo "Creating directory structure for $APP_NAME..."
mkdir -p "$APP_NAME"/{repo,deployments,config/{prod,feature}}
# Initialize the bare git repository
cd "$APP_NAME/repo"
git init --bare
# Create the post-receive hook
cat > hooks/post-receive << 'EOF'
#!/bin/bash
# The script runs from the bare git repository directory (.git)
REPO_ROOT="$(dirname "$(dirname "$GIT_DIR")")"
DEPLOY_ROOT="$REPO_ROOT/deployments"
CONFIG_ROOT="$REPO_ROOT/config"
MAIN_BRANCHES="main|master"
TRAEFIK_NETWORK="traefik-proxy"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
get_repo_name() {
basename "$(dirname "$GIT_DIR")"
}
for cmd in docker-compose docker git yq; do
if ! command -v $cmd &> /dev/null; then
log "Error: $cmd is required but not installed"
exit 1
fi
done
setup_traefik_labels() {
local compose_file="$1"
local domain="$2"
local repo_name=$(get_repo_name)
local service_name="$3"
if ! yq e '.services.'$service_name'.labels[] | select(contains("traefik"))' "$compose_file" &>/dev/null; then
yq e -i '
.services.'$service_name'.networks += ["traefik-proxy"] |
.services.'$service_name'.labels += [
"traefik.enable=true",
"traefik.http.routers.'$repo_name'-'$service_name'.rule=Host(`'$domain'`)",
"traefik.http.routers.'$repo_name'-'$service_name'.entrypoints=websecure",
"traefik.http.routers.'$repo_name'-'$service_name'.tls=true"
] |
.networks.["traefik-proxy"].external = true
' "$compose_file"
fi
}
deploy() {
local branch="$1"
local repo_name=$(get_repo_name)
local target_dir="$DEPLOY_ROOT/$repo_name"
local config_dir="$CONFIG_ROOT/$repo_name"
if echo "$branch" | grep -E "^($MAIN_BRANCHES)$" > /dev/null; then
override_dir="$config_dir/prod"
if [[ ! -f "$override_dir/.env" ]]; then
log "Error: Production .env file not found at $override_dir/.env"
exit 1
fi
DEPLOY_DOMAIN=$(grep '^DOMAIN=' "$override_dir/.env" | cut -d '=' -f2)
SERVICE_NAME=$(grep '^SERVICE_NAME=' "$override_dir/.env" | cut -d '=' -f2)
else
if [[ ! -f "$config_dir/feature/.env" ]]; then
log "Feature branch deployments not configured. Skipping deployment of branch '$branch'"
return 0
fi
FEATURE_DOMAIN=$(grep '^DOMAIN=' "$config_dir/feature/.env" | cut -d '=' -f2)
if [[ -z "$FEATURE_DOMAIN" ]]; then
log "Feature branch domain not configured. Skipping deployment of branch '$branch'"
return 0
fi
override_dir="$config_dir/feature"
DEPLOY_DOMAIN="${branch}.${FEATURE_DOMAIN}"
target_dir="${target_dir}_${branch}"
SERVICE_NAME=$(grep '^SERVICE_NAME=' "$config_dir/feature/.env" | cut -d '=' -f2)
fi
# Default to 'web' if SERVICE_NAME is not set
SERVICE_NAME=${SERVICE_NAME:-web}
mkdir -p "$target_dir"
log "Deploying branch '$branch' to $target_dir"
git --work-tree="$target_dir" --git-dir="$GIT_DIR" checkout -f "$branch"
if [[ -d "$override_dir" ]]; then
cp -rf "$override_dir/." "$target_dir/"
fi
if [[ ! -f "$target_dir/docker-compose.yml" ]]; then
log "Error: docker-compose.yml not found in repository"
exit 1
fi
setup_traefik_labels "$target_dir/docker-compose.yml" "$DEPLOY_DOMAIN" "$SERVICE_NAME"
cd "$target_dir"
log "Building containers..."
docker-compose build --no-cache
log "Stopping old containers..."
docker-compose down
log "Starting new containers..."
docker-compose up -d
}
while read oldrev newrev ref; do
branch=$(echo "$ref" | sed 's|refs/heads/||')
log "Received push to branch: $branch"
deploy "$branch"
done
EOF
# Make the hook executable
chmod +x hooks/post-receive
# Create example environment files
cd "$CURRENT_DIR/$APP_NAME/config"
# Production environment example
cat > prod/.env.example << EOF
DOMAIN=example.com
SERVICE_NAME=web
# Add your production environment variables here
EOF
# Feature branch environment example
cat > feature/.env.example << EOF
DOMAIN=dev.example.com
SERVICE_NAME=web
# Add your feature branch environment variables here
EOF
echo "Setup complete! Directory structure created at $CURRENT_DIR/$APP_NAME"