-
Notifications
You must be signed in to change notification settings - Fork 1
139 lines (124 loc) · 4.45 KB
/
deploy.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
name: Deploy to VPS
on:
push:
branches:
- main
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# Checkout code
- name: Checkout code
uses: actions/checkout@v2
# Get Commit Details
- name: Get Commit Details
id: commit_info
run: |
echo "commit_message=$(git log -1 --pretty=%B)" >> $GITHUB_ENV
echo "commit_author=$(git log -1 --pretty=%an)" >> $GITHUB_ENV
# Install dependencies
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y sshpass rsync
# Setup SSH with passphrase
- name: Setup SSH passphrase
env:
SSH_PASSPHRASE: ${{ secrets.SSH_PASSPHRASE }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
ssh-agent -a $SSH_AUTH_SOCK > /dev/null
echo 'echo $SSH_PASSPHRASE' > ~/.ssh_askpass && chmod +x ~/.ssh_askpass
echo "$SSH_PRIVATE_KEY" | tr -d '\r' | DISPLAY=None SSH_ASKPASS=~/.ssh_askpass ssh-add - >/dev/null
echo "SSH key added"
# Verify SSH connection
- name: Verify SSH Connection
env:
VPS_IP: ${{ secrets.VPS_IP }}
run: |
sshpass -p "${{ secrets.SSH_PASSPHRASE }}" ssh -o StrictHostKeyChecking=no docker@${VPS_IP} "echo Connection successful"
# Copy files to VPS
- name: Copy files to VPS
env:
VPS_IP: ${{ secrets.VPS_IP }}
run: |
sshpass -p "${{ secrets.SSH_PASSPHRASE }}" rsync -avz -e "ssh -o StrictHostKeyChecking=no" ./ docker@${VPS_IP}:/backend
echo "Files copied to VPS"
# SSH and deploy with Docker
- name: SSH and Deploy Docker
env:
VPS_IP: ${{ secrets.VPS_IP }}
run: |
sshpass -p "${{ secrets.SSH_PASSPHRASE }}" ssh -o StrictHostKeyChecking=no docker@${VPS_IP} << 'EOF'
cd /backend
# Backup the current running container
if docker ps -q -f name=backend; then
docker tag backend:latest backend:backup || echo "Failed to tag existing image"
fi
# Try to build and deploy the new Docker container
if ! docker-compose down || ! docker-compose up -d --build --remove-orphans; then
echo "Deployment failed. Rolling back..."
docker-compose down
if docker images | grep -q 'backend:backup'; then
docker tag backend:backup backend:latest
docker-compose up -d
echo "Rollback to the previous version was successful."
else
echo "No backup available for rollback. Exiting..."
exit 1
fi
fi
echo "Deployment successful"
EOF
# Notify Slack - Success
- name: Notify Slack - Success
if: success()
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "✅ Backend Deployment to VPS succeeded! 🚀"
attachments:
- color: "36a64f"
fields:
- title: "Commit"
short: true
value: "${{ env.commit_message }}"
- title: "By"
short: true
value: "${{ env.commit_author }}"
- title: "Status"
short: true
value: "Success"
- title: "Deployment Details"
short: true
value: "The latest code was successfully deployed to the VPS."
# Notify Slack - Failure
- name: Notify Slack - Failure
if: failure()
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "❌ Backend Deployment to VPS failed! 😞"
attachments:
- color: "ff0000"
fields:
- title: "Commit"
short: true
value: "${{ env.commit_message }}"
- title: "By"
short: true
value: "${{ github.actor }}"
- title: "Status"
short: true
value: "Failed"
- title: "Error"
short: true
value: "The deployment process encountered an issue. Check the logs for details."