-
Notifications
You must be signed in to change notification settings - Fork 0
84 lines (70 loc) · 2.97 KB
/
main.yml
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
name: CI/CD Pipeline for DigitalOcean Droplet
on:
push:
branches:
- cicd
jobs:
redeploy:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the code
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Set up SSH for DigitalOcean access
- name: Set up SSH
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_KEY }}
# Step 3: Determine which servers had changes
- name: Check changes in files
id: check_files
run: |
CHATBOT_MAIN_CHANGED=false
CHATBOT_PROCESS_CHANGED=false
if git diff --name-only HEAD^ HEAD | grep -q 'chatbotmain/'; then
CHATBOT_MAIN_CHANGED=true
fi
if git diff --name-only HEAD^ HEAD | grep -q 'chatbotprocess/'; then
CHATBOT_PROCESS_CHANGED=true
fi
echo "::set-output name=main_changed::${CHATBOT_MAIN_CHANGED}"
echo "::set-output name=process_changed::${CHATBOT_PROCESS_CHANGED}"
# Step 4: Deploy to the affected servers (both chatbotmain and chatbotprocess if needed)
- name: Deploy to DigitalOcean
run: |
ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.SSH_HOST }} << EOF
# Check if chatbotmain needs to be redeployed
if [ "${{ steps.check_files.outputs.main_changed }}" = "true" ]; then
CONTAINER_ID=\$(sudo docker ps -q --filter "name=chatbotmain")
if [ "\$CONTAINER_ID" ]; then
echo "Stopping and removing container chatbotmain..."
sudo docker stop \$CONTAINER_ID
sudo docker rm \$CONTAINER_ID
IMAGE_ID=\$(sudo docker images -q chatbotmain)
if [ "\$IMAGE_ID" ]; then
echo "Removing image chatbotmain..."
sudo docker rmi -f \$IMAGE_ID
fi
fi
echo "Rebuilding and running container chatbotmain..."
sudo docker build -t chatbotmain .
sudo docker run -d -p 8000:8000 chatbotmain
fi
# Check if chatbotprocess needs to be redeployed
if [ "${{ steps.check_files.outputs.process_changed }}" = "true" ]; then
CONTAINER_ID=\$(sudo docker ps -q --filter "name=chatbotprocess")
if [ "\$CONTAINER_ID" ]; then
echo "Stopping and removing container chatbotprocess..."
sudo docker stop \$CONTAINER_ID
sudo docker rm \$CONTAINER_ID
IMAGE_ID=\$(sudo docker images -q chatbotprocess)
if [ "\$IMAGE_ID" ]; then
echo "Removing image chatbotprocess..."
sudo docker rmi -f \$IMAGE_ID
fi
fi
echo "Rebuilding and running container chatbotprocess..."
sudo docker build -t chatbotprocess .
sudo docker run -d -p 9000:9000 chatbotprocess
fi
EOF