forked from rezhajulio/PythonID-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
99 lines (84 loc) · 3.05 KB
/
deploy-dev.yml
File metadata and controls
99 lines (84 loc) · 3.05 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
90
91
92
93
94
95
96
97
98
99
name: Deploy to Dev Server
on:
push:
branches: [main]
workflow_dispatch:
jobs:
checks:
uses: ./.github/workflows/python-checks.yml
build-and-push:
needs: checks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
hendisantika/jvmid-bot:latest
hendisantika/jvmid-bot:${{ github.sha }}
hendisantika/jvmid-bot:${{ github.run_number }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.DEV_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo "${{ secrets.DEV_SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
- name: Create app directory on server
run: |
ssh deployer@103.31.204.189 'mkdir -p ~/jvmid-bot/data'
- name: Write .env file to server
run: |
ssh deployer@103.31.204.189 "echo '${{ vars.DEV_ENV_FILE }}' > ~/jvmid-bot/.env"
- name: Write groups.json to server
run: |
echo '${{ vars.DEV_GROUPS_JSON }}' > /tmp/groups.json
scp /tmp/groups.json deployer@103.31.204.189:~/jvmid-bot/groups.json
rm /tmp/groups.json
- name: Copy docker-compose.prod.yml to server
run: |
scp docker-compose.prod.yml deployer@103.31.204.189:~/jvmid-bot/docker-compose.prod.yml
- name: Deploy on server
run: |
ssh deployer@103.31.204.189 '
cd ~/jvmid-bot
export DOCKER_UID=$(id -u) DOCKER_GID=$(id -g)
# Pull image with retry (up to 5 attempts, 15s between retries)
MAX_RETRIES=5
for i in $(seq 1 $MAX_RETRIES); do
echo "Pull attempt $i/$MAX_RETRIES..."
if IMAGE_TAG=${{ github.run_number }} docker compose -f docker-compose.prod.yml pull 2>&1; then
echo "Pull succeeded"
break
fi
if [ "$i" -eq "$MAX_RETRIES" ]; then
echo "Pull failed after $MAX_RETRIES attempts"
exit 1
fi
echo "Pull failed, retrying in 15s..."
sleep 15
done
IMAGE_TAG=${{ github.run_number }} docker compose -f docker-compose.prod.yml up -d
docker image prune -f
echo "Deployment complete (tag: ${{ github.run_number }}). Running containers:"
docker ps --filter "name=jvmid-bot"
'