-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_deploy.sh
More file actions
190 lines (157 loc) · 4.63 KB
/
git_deploy.sh
File metadata and controls
190 lines (157 loc) · 4.63 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
#!/bin/bash
# Set variables for FTP
FTP_CMD='C:\ProgramData\chocolatey\bin\lftp.exe'
HOST='ftp.domain.com'
USER='user@domain.com'
PASS='userpassword'
ENCODED_PASS=true
SSL_VERIFY=false
REMOTE_DIR='/'
IGNORED_FILES=(".git" ".gitignore" ".gitftp" "*.log" "*.code-workspace" "node_modules" ".vs")
# Start program
echo "Starting FTP Upload to: $HOST"
echo -n "How many commits back to upload? (Press Enter for current commit): "
read COMMIT_COUNT
echo ""
# Set default if empty
if [[ -z "$COMMIT_COUNT" ]]; then
COMMIT_COUNT=1
fi
# Decode password if it's encoded
if [ "$ENCODED_PASS" = true ]; then
PASS=$(echo "$PASS" | base64 --decode)
fi
# Determine SSL setting
if [ "$SSL_VERIFY" = true ]; then
SSL_OPTION="set ssl:verify-certificate yes"
else
SSL_OPTION="set ssl:verify-certificate no"
fi
# Get the list changed, and deleted files based on user input
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRT HEAD~$COMMIT_COUNT HEAD)
DELETED_FILES=$(git diff --name-only --diff-filter=D HEAD~$COMMIT_COUNT HEAD)
# Initialize counters
UPDATED_COUNT=0
DELETED_COUNT=0
# Function to check if a file is in the ignored list
is_ignored() {
local file="$1"
for ignore in "${IGNORED_FILES[@]}"; do
# Check for exact match or directory match
if [[ "$file" == "$ignore" || "$file" == "$ignore"/* ]]; then
return 0
fi
# Check for wildcard patterns using 'fnmatch' with bash [[ ]]
if [[ "$file" == $ignore ]]; then
return 0
fi
done
return 1
}
# Display the files that will be updated
HAS_INVALID=false
HAS_UPLOADS=false
HAS_UPLOADS_NUM=1
while IFS= read -r FILE; do
[ -z "$FILE" ] && continue
# Check invalid files
if [[ ! "$FILE" =~ ^[a-zA-Z0-9._/\ -]+$ ]]; then
HAS_INVALID=true
fi
# Skip ignored files
if ! is_ignored "$FILE"; then
if [ "$HAS_UPLOADS" = false ]; then
echo "Files to be uploaded:"
HAS_UPLOADS=true
fi
echo "$HAS_UPLOADS_NUM) $FILE"
((HAS_UPLOADS_NUM++))
fi
done <<< "$CHANGED_FILES"
# Display the files that will be deleted
HAS_DELETES=false
HAS_DELETES_NUM=1
while IFS= read -r FILE; do
[ -z "$FILE" ] && continue
# Check invalid files
if [[ ! "$FILE" =~ ^[a-zA-Z0-9._/\ -]+$ ]]; then
HAS_INVALID=true
fi
# Skip ignored files
if ! is_ignored "$FILE"; then
if [ "$HAS_DELETES" = false ]; then
if [ "$HAS_UPLOADS" = true ]; then
echo ""
fi
echo -e "Files to be deleted:"
HAS_DELETES=true
fi
echo "$HAS_DELETES_NUM) $FILE"
((HAS_DELETES_NUM++))
fi
done <<< "$DELETED_FILES"
# Display note for not uploaded or delete
if [ "$HAS_UPLOADS" = false ] && [ "$HAS_DELETES" = false ]; then
echo "No files to upload or delete."
fi
# Display note for invalid file names
if [ "$HAS_INVALID" = true ]; then
echo -e "\nError: Detected invalid characters in file names. Please rename them..."
read
exit 1
fi
# Prompt user for confirmation
echo -e "\nPress Enter to continue or Ctrl + C to cancel..."
read
# Upload changed files to FTP
LFTP_UPLOAD_CMDS="$SSL_OPTION; open $HOST; user $USER $PASS"
while IFS= read -r FILE; do
[ -z "$FILE" ] && continue
# Skip ignored files
if is_ignored "$FILE"; then
echo "Skipping: $FILE"
continue
fi
# Make sure the file exists before uploading
if [ -f "$FILE" ]; then
echo "Uploading: $FILE"
# Get remote directory path
REMOTE_PATH=$(dirname "$FILE")
# Update lftp to upload the changed file to the FTP server
LFTP_UPLOAD_CMDS="$LFTP_UPLOAD_CMDS; $( [ "$REMOTE_PATH" != "." ] && echo "cls -d \"$REMOTE_DIR$REMOTE_PATH\" || mkdir -p \"$REMOTE_DIR$REMOTE_PATH\"" ); put \"$FILE\" -o \"$REMOTE_DIR$FILE\""
((UPDATED_COUNT++))
fi
done <<< "$CHANGED_FILES"
LFTP_UPLOAD_CMDS="$LFTP_UPLOAD_CMDS; bye"
$FTP_CMD -e "$LFTP_UPLOAD_CMDS" -u $USER,$PASS
# Delete removed files from FTP
LFTP_DELETE_CMDS="$SSL_OPTION; open $HOST; user $USER $PASS"
while IFS= read -r FILE; do
[ -z "$FILE" ] && continue
# Skip ignored files
if is_ignored "$FILE"; then
echo "Skipping deletion: $FILE"
continue
fi
echo "Deleting: $FILE"
# Get remote directory path
REMOTE_PATH=$(dirname "$FILE")
# Remove directory paths
CLEAN_CMDS=""
if [ "$REMOTE_PATH" != "." ]; then
CURRENT_PATH="$REMOTE_PATH"
while [ "$CURRENT_PATH" != "." ]; do
CLEAN_CMDS="$CLEAN_CMDS rmdir \"$REMOTE_DIR$CURRENT_PATH\";"
CURRENT_PATH=$(dirname "$CURRENT_PATH")
done
fi
# Update lftp to delete the changed file to the FTP server
LFTP_DELETE_CMDS="$LFTP_DELETE_CMDS; rm \"$REMOTE_DIR$FILE\"; $CLEAN_CMDS"
((DELETED_COUNT++))
done <<< "$DELETED_FILES"
LFTP_DELETE_CMDS="$LFTP_DELETE_CMDS; bye"
$FTP_CMD -e "$LFTP_DELETE_CMDS" -u $USER,$PASS
# Display total counts and finish program
echo -e "\nSuccessfully Updated: $UPDATED_COUNT Files, Deleted: $DELETED_COUNT Files."
echo "Press Enter to exit..."
read