forked from TapTap21/docker-remote-deployment-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.sh
executable file
·82 lines (63 loc) · 2.28 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
#!/bin/sh
set -eu
execute_ssh(){
echo "Execute Over SSH: $@"
ssh -q -t -i "$HOME/.ssh/id_rsa" \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no "$INPUT_REMOTE_DOCKER_HOST" -p "$INPUT_SSH_PORT" "$@"
}
if [ -z "$INPUT_REMOTE_DOCKER_HOST" ]; then
echo "Input remote_docker_host is required!"
exit 1
fi
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
if [ -z "$INPUT_ARGS" ]; then
echo "Input input_args is required!"
exit 1
fi
if [ -z "$INPUT_STACK_FILE_NAME" ]; then
DEPLOYMENT_COMMAND="docker-compose"
else
# convert comma seperated filenames to docker-compose -f
# 'a,b,c' -> '-f a -f b -f c'
DOCKER_COMPOSE_FILE_FLAGS="$(echo "$INPUT_STACK_FILE_NAME" | sed 's/^/-f /' | sed 's/,/ -f /')"
DEPLOYMENT_COMMAND="docker-compose $DOCKER_COMPOSE_FILE_FLAGS"
fi
if [ -z "$INPUT_SSH_PORT" ]; then
INPUT_SSH_PORT=22
fi
DEPLOYMENT_COMMAND_OPTIONS="--host ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_SSH_PORT"
SSH_HOST=${INPUT_REMOTE_DOCKER_HOST#*@}
echo "Registering SSH keys..."
# register the private key with the agent.
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
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 staging --docker "host=ssh://$INPUT_REMOTE_DOCKER_HOST:$INPUT_SSH_PORT"
docker context use staging
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
echo "Command: ${DEPLOYMENT_COMMAND} pull"
${DEPLOYMENT_COMMAND} ${DEPLOYMENT_COMMAND_OPTIONS} pull
echo "Command: ${DEPLOYMENT_COMMAND} ${INPUT_ARGS}"
${DEPLOYMENT_COMMAND} ${DEPLOYMENT_COMMAND_OPTIONS} ${INPUT_ARGS}