-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·163 lines (144 loc) · 4.17 KB
/
deploy.sh
File metadata and controls
executable file
·163 lines (144 loc) · 4.17 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
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Check if hostname is set
if [ -z "$HOSTNAME" ]; then
echo "⚠️ Error: HOSTNAME is not set!"
exit 1
fi
# Check if username is set
if [ -z "$USERNAME" ]; then
echo "⚠️ Error: USERNAME is not set!"
exit 1
fi
# Set host if port is set
if [ -n "$PORT" ]; then
HOST="$HOSTNAME:$PORT"
else
HOST="$HOSTNAME"
fi
# Set correct path for local directory
if [ -n "$SOURCE_DIR" ]; then
SOURCE_DIR="$(pwd)${SOURCE_DIR#./}"
else
SOURCE_DIR=$(pwd)
fi
# Check if local directory has a leading slash and no trailing slash
if [[ "$SOURCE_DIR" != /* || "$SOURCE_DIR" == */ ]] && [[ "$SOURCE_DIR" != "/" ]]; then
echo "⚠️ Error: ${SOURCE_DIR} must start with a leading slash and must not end with a trailing slash!"
exit 1
fi
# Check if remote directory has a leading slash and no trailing slash
if [[ "$TARGET_DIR" != /* || "$TARGET_DIR" == */ ]] && [[ "$TARGET_DIR" != "/" ]]; then
echo "⚠️ Error: ${TARGET_DIR} must start with a leading slash and must not end with a trailing slash!"
exit 1
fi
# Set the method to be used
if [ "$MODE" = 'zip' ]; then
METHOD="put"
SOURCE="${SOURCE_DIR}/${ARCHIVE_NAME}.zip"
elif [ "$MODE" = 'file' ]; then
METHOD="put"
SOURCE="${SOURCE_DIR}/${FILE}"
elif [ "$MODE" = 'dir' ]; then
METHOD="mirror"
SOURCE="${SOURCE_DIR}"
else
echo "⚠️ Error: Invalid MODE specified!"
exit 1
fi
# Check if the SOURCE exists
if [ "$MODE" = 'zip' ] || [ "$MODE" = 'file' ]; then
if [ ! -f "${SOURCE}" ]; then
echo "⚠️ Error: ${SOURCE} not found!"
exit 1
fi
elif [ "$MODE" = 'dir' ]; then
if [ ! -d "${SOURCE}" ]; then
echo "⚠️ Error: ${SOURCE} directory not found!"
exit 1
fi
fi
# Clean up the remote directory if CLEAN_DIR is set
if [ "$CLEAN_DIR" = true ]; then
CLEAN_DIR="rm -r $TARGET_DIR"
else
CLEAN_DIR=""
fi
# Upload only new files if ONLY_NEWER is set
if [ "$ONLY_NEWER" = true ]; then
ONLY_NEWER="-e --only-newer --ignore-time --delete-first"
else
ONLY_NEWER=""
fi
# Check if the ignore file is specified
if [ -n "$IGNORE_FILE" ]; then
# Check if the ignore file exists and is readable
if [ -f "$IGNORE_FILE" ] && [ -r "$IGNORE_FILE" ]; then
IGNORES=$(sed '/^!/d;s/#.*//;/^$/d' "${SOURCE_DIR}/$IGNORE_FILE" | awk '{printf "--exclude-glob %s ", $1}')
else
echo "⚠️ Error: Ignore file '$IGNORE_FILE' not found or not readable. Skipping..."
IGNORES=""
fi
else
IGNORES=""
fi
# Set CHMOD
if [ "$CHMOD" = true ]; then
CHMOD="chmod $CHMOD"
else
CHMOD=""
fi
# Set debug mode if DEBUG is set to true
if [ "$DEBUG" = true ]; then
DEBUG="-d"
VERBOSE="--verbose"
else
DEBUG=""
VERBOSE=""
fi
# Set protocol-specific options
case "$PROTOCOL" in
ftp)
PROTOCOL_OPTIONS=""
;;
ftps | https)
PROTOCOL_OPTIONS="set ftp:ssl-allow true; \
set ftp:ssl-force $SSL_FORCE; \
set ftp:ssl-protect-data $SSL_PROTECT_DATA; \
set tp:ssl-auth $SSL_AUTH; \
set ssl:verify-certificate $SSL_VERIFY_CERT; \
set ssl:check-hostname $SSL_CHECK_HOST; \
set ssl:key-file $SSL_KEY_FILE;"
;;
sftp)
PROTOCOL_OPTIONS="set sftp:auto-confirm $SFTP_AUTO_CONFIRM; \
set sftp:connect-program $SFTP_CONNECT_PROGRAM;"
;;
*)
echo "⚠️ Error: Unsupported protocol specified!"
exit 1
;;
esac
# Determine the command to be executed
if [ "$METHOD" = 'mirror' ]; then
# Deploy directory
COMMAND="mirror -R ${SOURCE} ${TARGET_DIR} $ONLY_NEWER $IGNORES $EXCLUDES $INCLUDES $VERBOSE $OPTIONS"
elif [ "$METHOD" = 'put' ]; then
# Deploy single file/archive
COMMAND="put -O ${TARGET_DIR} ${SOURCE} $OPTIONS"
fi
# Execute the deployment
echo "✨ Uploading '${SOURCE}' to '${HOSTNAME}${TARGET_DIR}'"
lftp $DEBUG -e "set xfer:log 1; \
$PROTOCOL_OPTIONS \
set net:max-retries $MAX_RETRIES; \
mkdir -pf $TARGET_DIR; \
$PRE_COMMANDS; \
$CLEAN_DIR; \
$COMMAND; \
$CHMOD; \
$POST_COMMANDS; \
bye" \
-u $USERNAME,$PASSWORD $HOST
echo "🎉 Files uploaded successfully."