-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·50 lines (40 loc) · 1.53 KB
/
deploy.sh
File metadata and controls
executable file
·50 lines (40 loc) · 1.53 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
#!/bin/bash
# Variables
DOCKER_HUB_USERNAME="manjunathdc"
IMAGE_NAME="devops-app"
TAG="dev"
SERVER_IP="34.215.200.114"
# Jenkins provides the SSH key as an environment
SSH_KEY="$SSH_KEY" # Path to the SSH key provided by Jenkins
# Log the SSH key path
echo "Using SSH key: $SSH_KEY"
# Add server to known hosts
echo "Adding server to known_hosts..."
mkdir -p ~/.ssh
ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
# SSH into the server and deploy the application
echo "Deploying application to server..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no ubuntu@$SERVER_IP << EOF
echo "Updating system packages..."
sudo apt update -y
sudo apt upgrade -y
# Pull the latest Docker image (force pull)
echo "Pulling the latest Docker image..."
sudo docker pull $DOCKER_HUB_USERNAME/$IMAGE_NAME:$TAG --no-cache
# Stop and remove the existing container if it exists
echo "Stopping and removing existing container..."
if sudo docker ps -a --format '{{.Names}}' | grep -q '^devops-app$'; then
sudo docker stop devops-app || true
sudo docker rm devops-app || true
else
echo "No existing container named 'devops-app' found."
fi
# Run the new container
echo "Starting new container..."
sudo docker run -d -p 80:80 --name devops-app $DOCKER_HUB_USERNAME/$IMAGE_NAME:$TAG
echo "Deployment completed successfully."
EOF
# Check if the container is running
echo "Verifying container status..."
ssh -i "$SSH_KEY" ubuntu@$SERVER_IP "sudo docker ps -a | grep devops-app"