-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
executable file
·90 lines (73 loc) · 2.48 KB
/
init.sh
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
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to handle errors
handle_error() {
echo "Error: $1"
exit 1
}
# Check if project containers are running and issue make down command
if docker ps | grep -q "prod-automation_prod-auto_1"; then
make down
fi
# Set execute permissions for scripts
chmod +x ./scripts/setup-host.sh
chmod +x ./scripts/env-set.sh
chmod +x ./scripts/hosts-registry.sh
chmod +x ./scripts/pipeline.sh
# Check if Docker, Docker Compose, and Git are installed
if ! command_exists docker || ! command_exists docker-compose || ! command_exists git; then
echo "Docker, Docker Compose, or Git is not installed. Running setup script..."
if ! ./scripts/setup-host.sh; then
handle_error "Failed to setup host environment"
fi
fi
# Step 1: Get GitHub webhook secret from user
if [ -z "$1" ]; then
handle_error "Please provide the GitHub webhook secret as an argument"
fi
# Step 2: Sync to upstream
if ! git pull; then
handle_error "Failed to sync with upstream repository"
fi
# Step 3: Build project image
if ! make build; then
handle_error "Failed to build the project image"
fi
# Step 4: Build secret key
if ! make keygen; then
handle_error "Failed to build the secret key"
fi
# Step 5: Execute hosts-registry.sh
if ! ./scripts/hosts-registry.sh insecure_registry; then
handle_error "Failed to execute hosts-registry.sh with insecure_registry"
fi
# Step 6: Deploy project components
if ! make up; then
handle_error "Failed to deploy project components"
fi
# Step 7: Execute env-set.sh with GitHub webhook secret
if ! ./scripts/env-set.sh GITHUB_WEBHOOK_SECRET "$1"; then
handle_error "Failed to execute env-set.sh"
fi
# step 8: Update host /etc/hosts file with new container ip
if ! ./scripts/hosts-registry.sh update_hosts; then
handle_error "Failed to execute hosts-registry.sh with update_hosts"
fi
# Check if cronjob exists
if ! crontab -l | grep -q "docker image prune"; then
# Setup cronjob for Docker image prune
(crontab -l ; echo "0 0 * * * docker image prune -a -f") | crontab -
fi
# Run the shell pipeline if the second argument is set "auto"
if [ ! -z "$2" ]; then
# Check if pipeline.sh is already running with nohup
if pgrep -f "pipeline.sh" > /dev/null; then
# If it's running, kill the existing process
echo "Existing pipeline.sh process found ..."
return
fi
nohup ./scripts/pipeline.sh $1 > pipeline.log &
fi