forked from cssnr/stack-deploy-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
89 lines (70 loc) · 2.67 KB
/
main.sh
File metadata and controls
89 lines (70 loc) · 2.67 KB
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
#!/usr/bin/env bash
set -e
echo "Running: ${0} as: $(whoami) in: $(pwd)"
function cleanup_trap() {
_ST="$?"
if [[ "${_ST}" != "0" ]]; then
echo -e "\u001b[31;1mScript Exited with Error: ${_ST}"
fi
if [ -z "${INPUT_SSH_KEY}" ];then
echo -e "\u001b[35mCleaning Up authorized_keys on: ${INPUT_HOST}"
ssh -p "${INPUT_PORT}" "${INPUT_USER}@${INPUT_HOST}" \
"sed -i '/docker-stack-deploy-action/d' ~/.ssh/authorized_keys"
fi
if [[ "${_ST}" == "0" ]]; then
echo -e "\u001b[32;1mFinished Success."
fi
exit "${_ST}"
}
mkdir -p /root/.ssh
chmod 0700 /root/.ssh
ssh-keyscan -p "${INPUT_PORT}" -H "${INPUT_HOST}" >> /root/.ssh/known_hosts
if [ -z "${INPUT_SSH_KEY}" ];then
echo -e "\u001b[36mCreating and Copying SSH Key to: ${INPUT_HOST}"
ssh-keygen -q -f /root/.ssh/id_rsa -N "" -C "docker-stack-deploy-action"
eval "$(ssh-agent -s)"
ssh-add /root/.ssh/id_rsa
sshpass -p "${INPUT_PASS}" \
ssh-copy-id -p "${INPUT_PORT}" -i /root/.ssh/id_rsa \
"${INPUT_USER}@${INPUT_HOST}"
else
echo -e "\u001b[36mAdding SSH Key to SSH Agent"
echo "${INPUT_SSH_KEY}" > /root/.ssh/id_rsa
chmod 0600 /root/.ssh/id_rsa
eval "$(ssh-agent -s)"
ssh-add /root/.ssh/id_rsa
fi
trap cleanup_trap EXIT HUP INT QUIT PIPE TERM
echo -e "\u001b[36mVerifying Docker and Setting Context."
ssh -p "${INPUT_PORT}" "${INPUT_USER}@${INPUT_HOST}" "docker info" > /dev/null
if [[ -n "${INPUT_REGISTRY}" ]]; then
echo -e "\u001b[36mLogging into Docker Registry: \u001b[37;1m${INPUT_REGISTRY}"
echo "${INPUT_REGISTRY_PASSWORD}" | docker login --username "${INPUT_REGISTRY_USERNAME}" "${INPUT_REGISTRY}" --password-stdin
fi
docker context create remote --docker "host=ssh://${INPUT_USER}@${INPUT_HOST}:${INPUT_PORT}"
docker context ls
docker context use remote
if [ -n "${INPUT_ENV_FILE}" ];then
echo -e "\u001b[36mSourcing Environment File: ${INPUT_ENV_FILE}"
stat "${INPUT_ENV_FILE}"
set -a
# shellcheck disable=SC1090
source "${INPUT_ENV_FILE}"
# echo TRAEFIK_HOST: "${TRAEFIK_HOST}"
# export ENV_FILE="${INPUT_ENV_FILE}"
fi
echo -e "\u001b[36mDeploying Stack: \u001b[37;1m${INPUT_NAME}"
deploy_command="docker stack deploy"
if [[ "${INPUT_REGISTRY_AUTH}" == "true" ]]; then
deploy_command+=" --with-registry-auth"
fi
if [[ "${INPUT_PRUNE}" == "true" ]]; then
deploy_command+=" --prune"
fi
deploy_command+=" -c \"${INPUT_FILE}\" \"${INPUT_NAME}\""
echo -e "\u001b[36mDeployment Command: \u001b[37;1m$deploy_command"
eval "$deploy_command"
if [[ -n "${INPUT_REGISTRY}" ]]; then
echo -e "\u001b[36mLogin out of docker: \u001b[37;1m${INPUT_NAME}"
docker logout "${INPUT_REGISTRY}"
fi