-
Notifications
You must be signed in to change notification settings - Fork 200
103 lines (87 loc) Β· 3.63 KB
/
deploy.yml
File metadata and controls
103 lines (87 loc) Β· 3.63 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
100
101
102
103
name: Auto Deploy to Server
on:
schedule:
- cron: '0 16 */2 * *' # Every 2 days at 00:00 Beijing Time (16:00 UTC)
workflow_dispatch: # Allow manual trigger
jobs:
check-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Check for new Docker image and deploy
uses: appleboy/ssh-action@v1.2.1
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
port: ${{ secrets.SERVER_PORT }}
password: ${{ secrets.SERVER_PASSWORD }}
command_timeout: 30m
envs: DOCKER_USERNAME,DOCKER_PASSWORD
script: |
IMAGE="${DOCKER_USERNAME}/docflow:latest"
# Configure Docker mirrors (only if not configured)
if [ ! -f /etc/docker/daemon.json ] || ! grep -q "registry-mirrors" /etc/docker/daemon.json; then
echo "βοΈ Configuring Docker mirrors..."
sudo tee /etc/docker/daemon.json > /dev/null <<'EOF'
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.1panel.live",
"https://hub.rat.dev"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sleep 5
fi
# Login to Docker Hub
echo "π Logging in to Docker Hub..."
echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin || {
echo "β Docker login failed"
exit 1
}
# Get remote image digest
echo "π Checking for updates..."
REMOTE_DIGEST=$(docker manifest inspect "$IMAGE" 2>/dev/null | grep -m1 '"digest"' | cut -d'"' -f4)
if [ -z "$REMOTE_DIGEST" ]; then
echo "β Failed to fetch remote image info"
exit 1
fi
# Get current running container digest
CURRENT_DIGEST=$(docker inspect docflow 2>/dev/null | grep -m1 '"Image"' | cut -d'"' -f4 | xargs docker inspect 2>/dev/null | grep -m1 '"Id"' | cut -d'"' -f4)
# Compare digests
if [ "$REMOTE_DIGEST" = "$CURRENT_DIGEST" ]; then
echo "β
Already running latest version"
exit 0
fi
echo "π New version available, deploying..."
# Pull new image
docker pull "$IMAGE" || exit 1
# Stop and remove old container
docker stop docflow 2>/dev/null || true
docker rm docflow 2>/dev/null || true
# Start new container
CONTAINER_ID=$(docker run -d \
--name docflow \
--restart unless-stopped \
-p 3000:3000 \
-e NODE_ENV=production \
"$IMAGE")
if [ -z "$CONTAINER_ID" ]; then
echo "β Failed to start container"
exit 1
fi
# Wait and verify
sleep 5
CONTAINER_STATUS=$(docker inspect -f '{{.State.Status}}' docflow 2>/dev/null)
if [ "$CONTAINER_STATUS" = "running" ]; then
echo "β
Deployment successful"
docker logs --tail 20 docflow
else
echo "β Container failed to start"
docker logs docflow 2>&1
exit 1
fi