-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.sh
59 lines (51 loc) · 1.62 KB
/
script.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
#!/bin/bash
# Get input parameters
REMOTE_USER=$1
REMOTE_HOST=$2
REMOTE_PATH=$3
SSH_KEY_PATH=$4
WORK_SPACE=$5
# Configure SSH
mkdir -p ~/.ssh
echo "$SSH_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
#Find the only .scps file in the project root directory
SCPS_FILE=$(find $WORK_SPACE -maxdepth 1 -type f -name "*.scps")
if [[ -z "$SCPS_FILE" ]]; then
echo "Error: No .scps file found in the project root."
echo "::set-output name=transfer-status::Error: No .scps file found."
exit 1
fi
if [[ $(echo "$SCPS_FILE" | wc -l) -ne 1 ]]; then
echo "Error: Multiple .scps files found in the project root."
echo "::set-output name=transfer-status::Error: Multiple .scps files found."
exit 1
fi
# Initialize the concatenated file path variable
FILES=""
# Reading .scps files line by line
while IFS= read -r FILE; do
if [[ -f "$WORK_SPACE/$FILE" ]]; then
FILES="$FILES $WORK_SPACE/$FILE"
else
echo "Warning: '$FILE' listed in $SCPS_FILE does not exist or is not a regular file."
fi
done < "$SCPS_FILE"
# Check if there is a valid file
if [[ -z "$FILES" ]]; then
echo "No valid files found to transfer."
echo "::set-output name=transfer-status::Error: No valid files."
exit 1
fi
#Performing an SCP transfer
echo "Transferring files to $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"
scp -o StrictHostKeyChecking=no -i $SSH_KEY_PATH $FILES "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"
# Check if SCP was successful
if [[ $? -eq 0 ]]; then
echo "Files transferred successfully."
echo "::set-output name=transfer-status::Success"
else
echo "SCP transfer failed."
echo "::set-output name=transfer-status::Error: SCP failed."
exit 1
fi