forked from TapTap21/docker-remote-deployment-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker-entrypoint.sh
executable file
·104 lines (89 loc) · 3.24 KB
/
docker-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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh
set -eu
if [ -z "$INPUT_REMOTE_DOCKER_HOST" ]; then
echo "Input remote_docker_host is required!"
exit 1
fi
# Ignore SSH keys when using Tailscale SSH
if [ -n "$INPUT_TAILSCALE_SSH" ];
then
echo "Tailscale SSH mode enabled, Manual SSH keys not required"
else
echo "Normal SSH mode, checking SSH keys"
if [ -z "$INPUT_SSH_PUBLIC_KEY" ]; then
echo "Input ssh_public_key is required!"
exit 1
fi
if [ -z "$INPUT_SSH_PRIVATE_KEY" ]; then
echo "Input ssh_private_key is required!"
exit 1
fi
fi
if [ -z "$INPUT_ARGS" ]; then
echo "Input input_args is required!"
exit 1
fi
if [ -z "$INPUT_COMPOSE_FILE_PATH" ]; then
INPUT_COMPOSE_FILE_PATH=docker-compose.yml
fi
if [ -z "$INPUT_SSH_PORT" ]; then
INPUT_SSH_PORT=22
fi
DOCKER_HOST=ssh://${INPUT_REMOTE_DOCKER_HOST}:${INPUT_SSH_PORT}
SSH_HOST=${INPUT_REMOTE_DOCKER_HOST#*@}
if [ -n "$INPUT_TAILSCALE_SSH" ];
then
echo "Using Tailscale SSH, Skipping Manual SSH key registeration"
mkdir -p ~/.ssh
eval $(ssh-agent)
else
echo "Registering SSH keys..."
# register the private key with the agent, when not using Tailscale
mkdir -p ~/.ssh
ls ~/.ssh
printf '%s\n' "$INPUT_SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
printf '%s\n' "$INPUT_SSH_PUBLIC_KEY" > ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/id_rsa.pub
#chmod 600 "~/.ssh"
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
fi
echo "Add known hosts"
ssh-keyscan -p $INPUT_SSH_PORT "$SSH_HOST" >> ~/.ssh/known_hosts
ssh-keyscan -p $INPUT_SSH_PORT "$SSH_HOST" >> /etc/ssh/ssh_known_hosts
# set context
echo "Create docker context"
docker context create remote --docker "host=ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_SSH_PORT"
docker context use remote
if [ -n "$INPUT_UPLOAD_DIRECTORY" ];
then
echo "upload_directory enabled"
if [ -z "$INPUT_DOCKER_COMPOSE_DIRECTORY" ];
then
echo "Input docker_compose_directory is required when upload_directory is enabled!"
exit 1
fi
tar cjvf - -C "$GITHUB_WORKSPACE" "$INPUT_DOCKER_COMPOSE_DIRECTORY" | ssh -o StrictHostKeyChecking=no "$INPUT_REMOTE_DOCKER_HOST" -p "$INPUT_SSH_PORT" 'tar -xjvf -'
echo "Upload finished"
if [ -n "$INPUT_POST_UPLOAD_COMMAND" ];
then
echo "Upload post command specified, runnig. $INPUT_POST_UPLOAD_COMMAND"
ssh -o StrictHostKeyChecking=no "$INPUT_REMOTE_DOCKER_HOST" -p "$INPUT_SSH_PORT" "eval $INPUT_POST_UPLOAD_COMMAND"
fi
fi
if [ -n "$INPUT_DOCKER_LOGIN_PASSWORD" ] || [ -n "$INPUT_DOCKER_LOGIN_USER" ] || [ -n "$INPUT_DOCKER_LOGIN_REGISTRY" ]; then
echo "Connecting to $INPUT_REMOTE_DOCKER_HOST... Command: docker login"
docker login -u "$INPUT_DOCKER_LOGIN_USER" -p "$INPUT_DOCKER_LOGIN_PASSWORD" "$INPUT_DOCKER_LOGIN_REGISTRY"
fi
if [ -n "$INPUT_DOCKER_SWARM" ];
then
echo "docker swarm mode enabled, using docker stack command"
echo "Command: docker ${INPUT_ARGS} stack deploy --compose-file ${INPUT_COMPOSE_FILE_PATH}"
docker ${INPUT_ARGS} stack deploy --compose-file ${INPUT_COMPOSE_FILE_PATH}
else
echo "Command: docker compose -f ${INPUT_COMPOSE_FILE_PATH} pull"
docker compose -f ${INPUT_COMPOSE_FILE_PATH} pull
echo "Command: docker compose -f ${INPUT_COMPOSE_FILE_PATH} ${INPUT_ARGS}"
docker compose -f ${INPUT_COMPOSE_FILE_PATH} ${INPUT_ARGS}
fi