-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.sh
33 lines (25 loc) · 1.19 KB
/
deploy.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
#!/bin/sh
# Stop script on first error
set -e
IMAGE_NAME="maximization/autodeploy-docker"
IMAGE_TAG=$(git rev-parse --short HEAD) # first 7 characters of the current commit hash
echo "Building Docker image ${IMAGE_NAME}:${IMAGE_TAG}, and tagging as latest"
docker build -t "${IMAGE_NAME}:${IMAGE_TAG}" .
docker tag "${IMAGE_NAME}:${IMAGE_TAG}" "${IMAGE_NAME}:latest"
echo "Authenticating and pushing image to Docker Hub"
echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
docker push "${IMAGE_NAME}:${IMAGE_TAG}"
docker push "${IMAGE_NAME}:latest"
# Decode SSH key
echo "${SSH_KEY}" | base64 -d > ssh_key
chmod 600 ssh_key # private keys need to have strict permission to be accepted by SSH agent
# Add production server to known hosts
echo "${SERVER_PUBLIC_KEY}" | base64 -d >> ~/.ssh/known_hosts
echo "Deploying via remote SSH"
ssh -i ssh_key "root@${SERVER_IP}" \
"docker pull ${IMAGE_NAME}:${IMAGE_TAG} \
&& docker stop autodeploy-docker \
&& docker rm autodeploy-docker \
&& docker run --init -d --name autodeploy-docker -p 80:3000 ${IMAGE_NAME}:${IMAGE_TAG} \
&& docker system prune -af" # remove unused images to free up space
echo "Successfully deployed, hooray!"