-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
64 lines (52 loc) · 2 KB
/
entrypoint.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
#!/bin/sh -l
# Exit immediately if a command exits with a non-zero status or if a variable is unset.
set -eu
# Define variables for input parameters
USERNAME="$1"
SERVER="$2"
PORT="$3"
SSH_PRIVATE_KEY="$4"
LOCAL_PATH="$5"
REMOTE_PATH="$6"
SFTP_ARGS="$7"
DELETE_REMOTE_FILES="$8"
PASSWORD="${9}"
# Define temporary file paths
TEMP_SSH_PRIVATE_KEY_FILE='../private_key.pem'
TEMP_SFTP_FILE='../sftp'
# Ensure the remote path is not empty
if [ -z "$REMOTE_PATH" ]; then
echo 'Error: remote_path is empty'
exit 1
fi
# Check if password is provided
if [ -n "$PASSWORD" ]; then
echo 'Using SSH password authentication'
apk add --no-cache sshpass
# Delete remote files if DELETE_REMOTE_FILES is set to true
if [ "$DELETE_REMOTE_FILES" = "true" ]; then
echo 'Deleting remote files...'
sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -p "$PORT" "$USERNAME@$SERVER" rm -rf "$REMOTE_PATH"
fi
# Start SFTP transfer
echo 'Starting SFTP transfer...'
printf "%s" "put -r $LOCAL_PATH $REMOTE_PATH" >"$TEMP_SFTP_FILE"
SSHPASS="$PASSWORD" sshpass -e sftp -oBatchMode=no -b "$TEMP_SFTP_FILE" -P "$PORT" "$SFTP_ARGS" -o StrictHostKeyChecking=no "$USERNAME@$SERVER"
echo 'Upload successful'
exit 0
fi
# Use SSH private key for authentication
echo 'Using SSH private key authentication'
printf "%s" "$SSH_PRIVATE_KEY" >"$TEMP_SSH_PRIVATE_KEY_FILE"
chmod 600 "$TEMP_SSH_PRIVATE_KEY_FILE" # Ensure the private key has the correct permissions
# Delete remote files if DELETE_REMOTE_FILES is set to true
if [ "$DELETE_REMOTE_FILES" = "true" ]; then
echo 'Deleting remote files...'
ssh -o StrictHostKeyChecking=no -p "$PORT" -i "$TEMP_SSH_PRIVATE_KEY_FILE" "$USERNAME@$SERVER" rm -rf "$REMOTE_PATH"
fi
# Start SFTP transfer
echo 'Starting SFTP transfer...'
printf "%s" "put -r $LOCAL_PATH $REMOTE_PATH" >"$TEMP_SFTP_FILE"
sftp -b "$TEMP_SFTP_FILE" -P "$PORT" "$SFTP_ARGS" -o StrictHostKeyChecking=no -i "$TEMP_SSH_PRIVATE_KEY_FILE" "$USERNAME@$SERVER"
echo 'Upload successful'
exit 0