-
Notifications
You must be signed in to change notification settings - Fork 1
181 lines (151 loc) Β· 7.58 KB
/
deploy-dev.yml
File metadata and controls
181 lines (151 loc) Β· 7.58 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
name: Build, Tag & Deploy to Dev
on:
push:
branches:
- dev
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get latest tag from Docker Hub
id: get_tag
run: |
# Fetch tags from Docker Hub
REPO="${{ secrets.DOCKER_USERNAME }}/api-yapper-backend"
echo "π Fetching tags from Docker Hub for $REPO..."
# Get Docker Hub token for authenticated API access
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \
-d '{"username": "${{ secrets.DOCKER_USERNAME }}", "password": "${{ secrets.DOCKER_PASSWORD }}"}' \
https://hub.docker.com/v2/users/login/ | jq -r .token)
if [ "$TOKEN" = "null" ] || [ -z "$TOKEN" ]; then
echo "β Failed to authenticate with Docker Hub"
exit 1
fi
# Get all tags from Docker Hub API
TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" \
"https://hub.docker.com/v2/repositories/$REPO/tags/?page_size=100" | \
jq -r '.results[].name' | \
grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | \
sort -V | \
tail -1)
# If no tags found, start with v0.0.0
if [ -z "$TAGS" ]; then
LATEST_TAG="v0.0.0"
echo "β οΈ No version tags found on Docker Hub, starting from v0.0.0"
else
LATEST_TAG="$TAGS"
echo "π¦ Latest tag found on Docker Hub: $LATEST_TAG"
fi
echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
# Parse version components
VERSION=${LATEST_TAG#v}
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
# Increment patch version
PATCH=$((PATCH + 1))
NEW_TAG="v${MAJOR}.${MINOR}.${PATCH}"
echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT
echo "π New version: $NEW_TAG"
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/Dockerfile
push: true
tags: |
${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:${{ steps.get_tag.outputs.new_tag }}
${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:dev
- name: Confirm image push
run: |
echo "β
Image pushed: ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:${{ steps.get_tag.outputs.new_tag }}"
echo "β
Image tagged: ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:dev"
- name: SSH into Dev VM and deploy
uses: appleboy/ssh-action@v1.0.0
env:
IMG_TAG: ${{ steps.get_tag.outputs.new_tag }}
with:
host: ${{ secrets.DEV_SERVER_HOST }}
username: ${{ secrets.DEV_SERVER_USER }}
key: ${{ secrets.DEV_SERVER_SSH_KEY }}
envs: IMG_TAG
script: |
set -e
cd ~/yapper # path to your app on VM
echo "π§ Getting current running image tag..."
CURRENT_TAG=$(IMG_TAG="" docker compose ps -q api | xargs docker inspect -f '{{ .Config.Image }}' | cut -d':' -f2 || echo "unknown")
echo "CURRENT_TAG: $CURRENT_TAG"
echo "π³ Setting new image tag for deployment..."
echo "Deploying version: $IMG_TAG"
echo "π Pulling new image and restarting app container..."
IMG_TAG=$IMG_TAG docker compose pull api api-local api-test
IMG_TAG=$IMG_TAG docker compose up -d api api-local api-test
echo "β³ Waiting for health check..."
sleep 10
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" ${{ secrets.DEV_HEALTHCHECK_URL }})
if [ "$HTTP_CODE" = "200" ]; then
echo "β
Dev deployment successful for version $IMG_TAG"
echo "π Running migrations for all services..."
MIGRATION_FAILED=0
echo "π¦ Running migration for api..."
docker compose exec -T api npm run migration:run:prod 2>&1 | tee /tmp/api-migration.log
if grep -q "Error during migration" /tmp/api-migration.log || grep -q "Cannot find module" /tmp/api-migration.log; then
echo "β Migration for api failed!"
cat /tmp/api-migration.log
MIGRATION_FAILED=1
elif grep -q "No migrations are pending" /tmp/api-migration.log || grep -q "No pending migrations" /tmp/api-migration.log; then
echo "βΉοΈ No pending migrations for api"
else
echo "β
Migration for api completed successfully"
fi
echo "π¦ Running migration for api-local..."
docker compose exec -T api-local npm run migration:run:prod 2>&1 | tee /tmp/api-local-migration.log
if grep -q "Error during migration" /tmp/api-local-migration.log || grep -q "Cannot find module" /tmp/api-local-migration.log; then
echo "β Migration for api-local failed!"
cat /tmp/api-local-migration.log
MIGRATION_FAILED=1
elif grep -q "No migrations are pending" /tmp/api-local-migration.log || grep -q "No pending migrations" /tmp/api-local-migration.log; then
echo "βΉοΈ No pending migrations for api-local"
else
echo "β
Migration for api-local completed successfully"
fi
echo "π¦ Running migration for api-test..."
docker compose exec -T api-test npm run migration:run:prod 2>&1 | tee /tmp/api-test-migration.log
if grep -q "Error during migration" /tmp/api-test-migration.log || grep -q "Cannot find module" /tmp/api-test-migration.log; then
echo "β Migration for api-test failed!"
cat /tmp/api-test-migration.log
MIGRATION_FAILED=1
elif grep -q "No migrations are pending" /tmp/api-test-migration.log || grep -q "No pending migrations" /tmp/api-test-migration.log; then
echo "βΉοΈ No pending migrations for api-test"
else
echo "β
Migration for api-test completed successfully"
fi
if [ "$MIGRATION_FAILED" = "1" ]; then
echo "β One or more migrations failed! Rolling back..."
if [ "$CURRENT_TAG" != "unknown" ]; then
IMG_TAG=$CURRENT_TAG docker compose up -d api api-local api-test
echo "π Rolled back to $CURRENT_TAG"
fi
exit 1
fi
echo "β
All migrations completed successfully"
else
echo "β Health check failed (HTTP $HTTP_CODE)! Rolling back app container..."
if [ "$CURRENT_TAG" != "unknown" ]; then
IMG_TAG=$CURRENT_TAG docker compose up -d api api-local api-test
echo "π Rolled back to $CURRENT_TAG"
fi
exit 1
fi
- name: Deployment summary
if: success()
run: |
echo "π Deployment Complete!"
echo "Version: ${{ steps.get_tag.outputs.new_tag }}"
echo "Branch: dev"
echo "Commit: ${{ github.sha }}"