Repository navigation
149 lines (128 loc) · 4.43 KB
/
Copy pathdeploy.yml
File metadata and controls
149 lines (128 loc) · 4.43 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
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
140
141
142
143
144
145
146
147
148
149
name: Deploy z8run
on:
# Automatic production deploys are DISABLED.
# Re-enable by uncommenting the push trigger below.
# push:
# branches: [main]
workflow_dispatch:
concurrency:
group: deploy-production
cancel-in-progress: true
env:
REGISTRY: ghcr.io
IMAGE_Z8RUN: ghcr.io/${{ github.repository }}-api
IMAGE_NGINX: ghcr.io/${{ github.repository }}-nginx
jobs:
build-and-push:
name: Build & Push Images
runs-on: ubuntu-24.04
timeout-minutes: 45
permissions:
contents: read
packages: write
outputs:
image_tag: ${{ github.sha }}
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build and push z8run API image
uses: docker/build-push-action@v7
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: true
tags: |
${{ env.IMAGE_Z8RUN }}:latest
${{ env.IMAGE_Z8RUN }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
- name: Build and push nginx image
uses: docker/build-push-action@v7
with:
context: .
file: ./deploy/Dockerfile.nginx
platforms: linux/amd64
push: true
tags: |
${{ env.IMAGE_NGINX }}:latest
${{ env.IMAGE_NGINX }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
deploy:
name: Deploy to Server
needs: build-and-push
runs-on: ubuntu-24.04
timeout-minutes: 10
environment: production
permissions:
contents: read
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_PATH: /opt/z8run
IMAGE_TAG: ${{ needs.build-and-push.outputs.image_tag }}
steps:
- name: Checkout code
uses: actions/checkout@v7
with:
sparse-checkout: |
docker-compose.yml
deploy/nginx.conf
- name: Setup SSH key
run: |
install -m 600 -D /dev/null ~/.ssh/deploy_key
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key
ssh-keyscan -H ${{ env.DEPLOY_HOST }} >> ~/.ssh/known_hosts 2>/dev/null
- name: Deploy on server
env:
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Sync only compose + nginx config
rsync -avz \
-e "ssh -i ~/.ssh/deploy_key" \
docker-compose.yml deploy/nginx.conf \
${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }}:${{ env.DEPLOY_PATH }}/
ssh -i ~/.ssh/deploy_key ${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }} << 'DEPLOY_EOF'
set -euo pipefail
cd ${{ env.DEPLOY_PATH }}
mkdir -p deploy
mv nginx.conf deploy/nginx.conf 2>/dev/null || true
# Login to GHCR
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# Pull new images
docker compose pull
# Restart with new images
docker compose up -d --remove-orphans
# Clean up only z8run dangling images
docker image prune -f --filter "label=org.opencontainers.image.source=https://github.com/${{ github.repository }}"
echo "✓ z8run deployed successfully (tag: ${{ env.IMAGE_TAG }})"
DEPLOY_EOF
- name: Health check
run: |
for i in 1 2 3 4 5; do
sleep 10
if ssh -i ~/.ssh/deploy_key ${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }} \
"curl -sf http://localhost/api/v1/health > /dev/null"; then
echo "✓ Health check passed (attempt $i)"
exit 0
fi
echo "⏳ Attempt $i/5 failed, retrying..."
done
echo "✗ Health check failed after 5 attempts"
ssh -i ~/.ssh/deploy_key ${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }} \
"docker compose -f ${{ env.DEPLOY_PATH }}/docker-compose.yml logs --tail=50"
exit 1
- name: Cleanup SSH key
if: always()
run: rm -f ~/.ssh/deploy_key