Repository navigation
78 lines (65 loc) · 2.21 KB
/
Copy pathdeploy-backend.yml
File metadata and controls
78 lines (65 loc) · 2.21 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
name: Deploy Backend
# Required GitHub secrets:
# GCP_SA_KEY - Service account JSON key (base64 encoded)
# GCP_VM_IP - External IP of the GCP VM
# GCP_VM_SSH_PRIVATE_KEY - SSH private key for connecting to the VM
on:
push:
branches: [main]
paths: [backend/**]
workflow_dispatch:
concurrency:
group: deploy-backend
cancel-in-progress: false
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Run tests
run: go test ./backend/... -cover
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Build static binary
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o smolterms ./backend/cmd/server/main.go
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.GCP_VM_SSH_PRIVATE_KEY }}" > ~/.ssh/id_deploy
chmod 600 ~/.ssh/id_deploy
ssh-keyscan -H ${{ secrets.GCP_VM_IP }} >> ~/.ssh/known_hosts
- name: Copy binary to VM
run: |
scp -i ~/.ssh/id_deploy smolterms deploy@${{ secrets.GCP_VM_IP }}:/tmp/smolterms
- name: Deploy and restart service
run: |
ssh -i ~/.ssh/id_deploy deploy@${{ secrets.GCP_VM_IP }} << 'EOF'
sudo mv /tmp/smolterms /opt/smolterms/smolterms
sudo chown smolterms:smolterms /opt/smolterms/smolterms
sudo chmod 755 /opt/smolterms/smolterms
sudo systemctl restart smolterms
EOF
- name: Health check
run: |
sleep 5
for i in $(seq 1 10); do
echo "Attempt $i/10..."
HTTP_CODE=$(ssh -i ~/.ssh/id_deploy deploy@${{ secrets.GCP_VM_IP }} "curl -s -o /dev/null -w '%{http_code}' --max-time 5 http://localhost:8080/api/v1/health" || true)
echo "HTTP status: $HTTP_CODE"
if [ "$HTTP_CODE" = "200" ]; then
echo "Health check passed"
exit 0
fi
sleep 5
done
echo "Health check failed"
exit 1