forked from JMPJNS/docker-remote-deploy-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·85 lines (66 loc) · 2.33 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh
set -eu
if [ -z "$INPUT_REMOTE_HOST" ]; then
echo "Input remote_host is required!"
exit 1
fi
if [ $INPUT_DOCKER_REGISTRY ] && [ $INPUT_DOCKER_USERNAME ] && [ $INPUT_DOCKER_PASSWORD ]; then
echo $INPUT_DOCKER_PASSWORD | docker login $INPUT_DOCKER_REGISTRY -u $INPUT_DOCKER_USERNAME --password-stdin
fi
# Extra handling for SSH-based connections.
if [ ${INPUT_REMOTE_HOST#"ssh://"} != "$INPUT_REMOTE_HOST" ]; then
SSH_HOST=${INPUT_REMOTE_HOST#"ssh://"}
SSH_HOST=${SSH_HOST#*@}
if [ -z "$INPUT_SSH_PRIVATE_KEY" ]; then
echo "Input ssh_private_key is required for SSH hosts!"
exit 1
fi
if [ -z "$INPUT_SSH_PUBLIC_KEY" ]; then
echo "Input ssh_public_key is required for SSH hosts!"
exit 1
fi
echo "Registering SSH keys..."
# Save private key to a file and register it with the agent.
mkdir -p "$HOME/.ssh"
echo "$INPUT_SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/docker
chmod 600 "$HOME/.ssh/docker"
eval $(ssh-agent)
ssh-add "$HOME/.ssh/docker"
# Add public key to known hosts.
echo "$INPUT_SSH_PUBLIC_KEY" | tr -d '\r' >> /etc/ssh/ssh_known_hosts
fi
echo "Connecting to $INPUT_REMOTE_HOST..."
# Check if the environment variable is set
if [ -z "$INPUT_ENV" ]; then
echo "Environment variable \$INPUT_ENV is not set."
else
echo "Environment variable \$INPUT_ENV is set. Exporting..."
# Create a temporary file
temp_file=$(mktemp)
# Save the content of INPUT_ENV to the temporary file
echo "$INPUT_ENV" > "$temp_file"
# Read the temporary file line by line
while IFS= read -r line; do
# Skip comments and empty lines
if [ -z "$line" ] || [ "${line# }" = "#" ]; then
continue
fi
# Export the variable
export "$line"
done < "$temp_file"
# Remove the temporary file
rm "$temp_file"
echo "Environment variables exported successfully."
fi
# Check if INPUT_RUN_NUMBER is set and not empty
if [ -z "$INPUT_RUN_NUMBER" ]; then
echo "ERROR: The environment variable INPUT_RUN_NUMBER is not set."
exit 1
else
export GITHUB_RUN_NUMBER="$INPUT_RUN_NUMBER"
echo "GITHUB_RUN_NUMBER is set to $GITHUB_RUN_NUMBER"
fi
docker --log-level debug --host "$INPUT_REMOTE_HOST" "$@" 2>&1
if [ $INPUT_DOCKER_REGISTRY ]; then
docker logout $INPUT_DOCKER_REGISTRY
fi