|
1 | 1 | name: Build, Tag & Deploy to Dev |
2 | 2 |
|
3 | 3 | on: |
4 | | - push: |
5 | | - branches: |
6 | | - - dev |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - dev |
7 | 7 |
|
8 | 8 | jobs: |
9 | | - build-and-deploy: |
10 | | - runs-on: ubuntu-latest |
11 | | - |
12 | | - steps: |
13 | | - - name: Checkout code |
14 | | - uses: actions/checkout@v4 |
15 | | - |
16 | | - - name: Get latest tag from Docker Hub |
17 | | - id: get_tag |
18 | | - run: | |
19 | | - # Fetch tags from Docker Hub |
20 | | - REPO="${{ secrets.DOCKER_USERNAME }}/api-yapper-backend" |
21 | | - |
22 | | - echo "🔍 Fetching tags from Docker Hub for $REPO..." |
23 | | - |
24 | | - # Get Docker Hub token for authenticated API access |
25 | | - TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \ |
26 | | - -d '{"username": "${{ secrets.DOCKER_USERNAME }}", "password": "${{ secrets.DOCKER_PASSWORD }}"}' \ |
27 | | - https://hub.docker.com/v2/users/login/ | jq -r .token) |
28 | | - |
29 | | - if [ "$TOKEN" = "null" ] || [ -z "$TOKEN" ]; then |
30 | | - echo "❌ Failed to authenticate with Docker Hub" |
31 | | - exit 1 |
32 | | - fi |
33 | | - |
34 | | - # Get all tags from Docker Hub API |
35 | | - TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" \ |
36 | | - "https://hub.docker.com/v2/repositories/$REPO/tags/?page_size=100" | \ |
37 | | - jq -r '.results[].name' | \ |
38 | | - grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | \ |
39 | | - sort -V | \ |
40 | | - tail -1) |
41 | | - |
42 | | - # If no tags found, start with v0.0.0 |
43 | | - if [ -z "$TAGS" ]; then |
44 | | - LATEST_TAG="v0.0.0" |
45 | | - echo "⚠️ No version tags found on Docker Hub, starting from v0.0.0" |
46 | | - else |
47 | | - LATEST_TAG="$TAGS" |
48 | | - echo "📦 Latest tag found on Docker Hub: $LATEST_TAG" |
49 | | - fi |
50 | | - |
51 | | - echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
52 | | - |
53 | | - # Parse version components |
54 | | - VERSION=${LATEST_TAG#v} |
55 | | - IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" |
56 | | - |
57 | | - # Increment patch version |
58 | | - PATCH=$((PATCH + 1)) |
59 | | - NEW_TAG="v${MAJOR}.${MINOR}.${PATCH}" |
60 | | - |
61 | | - echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT |
62 | | - echo "🚀 New version: $NEW_TAG" |
63 | | -
|
64 | | - - name: Log in to Docker Hub |
65 | | - uses: docker/login-action@v3 |
66 | | - with: |
67 | | - username: ${{ secrets.DOCKER_USERNAME }} |
68 | | - password: ${{ secrets.DOCKER_PASSWORD }} |
69 | | - |
70 | | - - name: Build and push Docker image |
71 | | - uses: docker/build-push-action@v6 |
72 | | - with: |
73 | | - context: . |
74 | | - file: ./docker/Dockerfile |
75 | | - |
76 | | - push: true |
77 | | - tags: | |
78 | | - ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:${{ steps.get_tag.outputs.new_tag }} |
79 | | - ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:dev |
80 | | -
|
81 | | - - name: Confirm image push |
82 | | - run: | |
83 | | - echo "✅ Image pushed: ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:${{ steps.get_tag.outputs.new_tag }}" |
84 | | - echo "✅ Image tagged: ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:dev" |
85 | | -
|
86 | | - - name: SSH into Dev VM and deploy |
87 | | - uses: appleboy/ssh-action@v1.0.0 |
88 | | - env: |
89 | | - IMG_TAG: ${{ steps.get_tag.outputs.new_tag }} |
90 | | - with: |
91 | | - host: ${{ secrets.DEV_SERVER_HOST }} |
92 | | - username: ${{ secrets.DEV_SERVER_USER }} |
93 | | - key: ${{ secrets.DEV_SERVER_SSH_KEY }} |
94 | | - envs: IMG_TAG |
95 | | - script: | |
96 | | - set -e |
97 | | - cd ~/yapper # path to your app on VM |
98 | | -
|
99 | | - echo "🧭 Getting current running image tag..." |
100 | | - CURRENT_TAG=$(IMG_TAG="" docker compose ps -q api | xargs docker inspect -f '{{ .Config.Image }}' | cut -d':' -f2 || echo "unknown") |
101 | | - echo "CURRENT_TAG: $CURRENT_TAG" |
102 | | - echo "🐳 Setting new image tag for deployment..." |
103 | | - echo "Deploying version: $IMG_TAG" |
104 | | -
|
105 | | - echo "🔄 Pulling new image and restarting app container..." |
106 | | - IMG_TAG=$IMG_TAG docker compose pull api api-local api-test |
107 | | - IMG_TAG=$IMG_TAG docker compose up -d api api-local api-test |
108 | | -
|
109 | | - echo "⏳ Waiting for health check..." |
110 | | - sleep 10 |
111 | | -
|
112 | | - HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" ${{ secrets.DEV_HEALTHCHECK_URL }}) |
113 | | - |
114 | | - if [ "$HTTP_CODE" = "200" ]; then |
115 | | - echo "✅ Dev deployment successful for version $IMG_TAG" |
116 | | - |
117 | | - echo "🔄 Running migrations for all services..." |
118 | | - MIGRATION_FAILED=0 |
119 | | - |
120 | | - echo "📦 Running migration for api..." |
121 | | - docker compose exec -T api npm run migration:run:prod 2>&1 | tee /tmp/api-migration.log |
122 | | - if grep -q "Error during migration" /tmp/api-migration.log || grep -q "Cannot find module" /tmp/api-migration.log; then |
123 | | - echo "❌ Migration for api failed!" |
124 | | - cat /tmp/api-migration.log |
125 | | - MIGRATION_FAILED=1 |
126 | | - elif grep -q "No migrations are pending" /tmp/api-migration.log || grep -q "No pending migrations" /tmp/api-migration.log; then |
127 | | - echo "ℹ️ No pending migrations for api" |
128 | | - else |
129 | | - echo "✅ Migration for api completed successfully" |
130 | | - fi |
131 | | - |
132 | | - echo "📦 Running migration for api-local..." |
133 | | - docker compose exec -T api-local npm run migration:run:prod 2>&1 | tee /tmp/api-local-migration.log |
134 | | - if grep -q "Error during migration" /tmp/api-local-migration.log || grep -q "Cannot find module" /tmp/api-local-migration.log; then |
135 | | - echo "❌ Migration for api-local failed!" |
136 | | - cat /tmp/api-local-migration.log |
137 | | - MIGRATION_FAILED=1 |
138 | | - elif grep -q "No migrations are pending" /tmp/api-local-migration.log || grep -q "No pending migrations" /tmp/api-local-migration.log; then |
139 | | - echo "ℹ️ No pending migrations for api-local" |
140 | | - else |
141 | | - echo "✅ Migration for api-local completed successfully" |
142 | | - fi |
143 | | - |
144 | | - echo "📦 Running migration for api-test..." |
145 | | - docker compose exec -T api-test npm run migration:run:prod 2>&1 | tee /tmp/api-test-migration.log |
146 | | - if grep -q "Error during migration" /tmp/api-test-migration.log || grep -q "Cannot find module" /tmp/api-test-migration.log; then |
147 | | - echo "❌ Migration for api-test failed!" |
148 | | - cat /tmp/api-test-migration.log |
149 | | - MIGRATION_FAILED=1 |
150 | | - elif grep -q "No migrations are pending" /tmp/api-test-migration.log || grep -q "No pending migrations" /tmp/api-test-migration.log; then |
151 | | - echo "ℹ️ No pending migrations for api-test" |
152 | | - else |
153 | | - echo "✅ Migration for api-test completed successfully" |
154 | | - fi |
155 | | - |
156 | | - if [ "$MIGRATION_FAILED" = "1" ]; then |
157 | | - echo "❌ One or more migrations failed! Rolling back..." |
158 | | - if [ "$CURRENT_TAG" != "unknown" ]; then |
159 | | - IMG_TAG=$CURRENT_TAG docker compose up -d api api-local api-test |
160 | | - echo "🔙 Rolled back to $CURRENT_TAG" |
161 | | - fi |
162 | | - exit 1 |
163 | | - fi |
164 | | - |
165 | | - echo "✅ All migrations completed successfully" |
166 | | - else |
167 | | - echo "❌ Health check failed (HTTP $HTTP_CODE)! Rolling back app container..." |
168 | | - if [ "$CURRENT_TAG" != "unknown" ]; then |
169 | | - IMG_TAG=$CURRENT_TAG docker compose up -d api api-local api-test |
170 | | - echo "🔙 Rolled back to $CURRENT_TAG" |
171 | | - fi |
172 | | - exit 1 |
173 | | - fi |
174 | | -
|
175 | | - - name: Deployment summary |
176 | | - if: success() |
177 | | - run: | |
178 | | - echo "🎉 Deployment Complete!" |
179 | | - echo "Version: ${{ steps.get_tag.outputs.new_tag }}" |
180 | | - echo "Branch: dev" |
181 | | - echo "Commit: ${{ github.sha }}" |
| 9 | + build-and-deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - name: Get latest tag from Docker Hub |
| 17 | + id: get_tag |
| 18 | + run: | |
| 19 | + # Fetch tags from Docker Hub |
| 20 | + REPO="${{ secrets.DOCKER_USERNAME }}/api-yapper-backend" |
| 21 | +
|
| 22 | + echo "🔍 Fetching tags from Docker Hub for $REPO..." |
| 23 | +
|
| 24 | + # Get Docker Hub token for authenticated API access |
| 25 | + TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \ |
| 26 | + -d '{"username": "${{ secrets.DOCKER_USERNAME }}", "password": "${{ secrets.DOCKER_PASSWORD }}"}' \ |
| 27 | + https://hub.docker.com/v2/users/login/ | jq -r .token) |
| 28 | +
|
| 29 | + if [ "$TOKEN" = "null" ] || [ -z "$TOKEN" ]; then |
| 30 | + echo "❌ Failed to authenticate with Docker Hub" |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | +
|
| 34 | + # Get all tags from Docker Hub API |
| 35 | + TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" \ |
| 36 | + "https://hub.docker.com/v2/repositories/$REPO/tags/?page_size=100" | \ |
| 37 | + jq -r '.results[].name' | \ |
| 38 | + grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | \ |
| 39 | + sort -V | \ |
| 40 | + tail -1) |
| 41 | +
|
| 42 | + # If no tags found, start with v0.0.0 |
| 43 | + if [ -z "$TAGS" ]; then |
| 44 | + LATEST_TAG="v0.0.0" |
| 45 | + echo "⚠️ No version tags found on Docker Hub, starting from v0.0.0" |
| 46 | + else |
| 47 | + LATEST_TAG="$TAGS" |
| 48 | + echo "📦 Latest tag found on Docker Hub: $LATEST_TAG" |
| 49 | + fi |
| 50 | +
|
| 51 | + echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + # Parse version components |
| 54 | + VERSION=${LATEST_TAG#v} |
| 55 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" |
| 56 | +
|
| 57 | + # Increment patch version |
| 58 | + PATCH=$((PATCH + 1)) |
| 59 | + NEW_TAG="v${MAJOR}.${MINOR}.${PATCH}" |
| 60 | +
|
| 61 | + echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT |
| 62 | + echo "🚀 New version: $NEW_TAG" |
| 63 | +
|
| 64 | + - name: Log in to Docker Hub |
| 65 | + uses: docker/login-action@v3 |
| 66 | + with: |
| 67 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 68 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 69 | + |
| 70 | + - name: Build and push Docker image |
| 71 | + uses: docker/build-push-action@v6 |
| 72 | + with: |
| 73 | + context: . |
| 74 | + file: ./docker/Dockerfile |
| 75 | + |
| 76 | + push: true |
| 77 | + tags: | |
| 78 | + ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:${{ steps.get_tag.outputs.new_tag }} |
| 79 | + ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:dev |
| 80 | +
|
| 81 | + - name: Confirm image push |
| 82 | + run: | |
| 83 | + echo "✅ Image pushed: ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:${{ steps.get_tag.outputs.new_tag }}" |
| 84 | + echo "✅ Image tagged: ${{ secrets.DOCKER_USERNAME }}/api-yapper-backend:dev" |
| 85 | +
|
| 86 | + - name: SSH into Dev VM and deploy |
| 87 | + uses: appleboy/ssh-action@v1.0.0 |
| 88 | + env: |
| 89 | + IMG_TAG: ${{ steps.get_tag.outputs.new_tag }} |
| 90 | + with: |
| 91 | + host: ${{ secrets.DEV_SERVER_HOST }} |
| 92 | + username: ${{ secrets.DEV_SERVER_USER }} |
| 93 | + key: ${{ secrets.DEV_SERVER_SSH_KEY }} |
| 94 | + envs: IMG_TAG |
| 95 | + script: | |
| 96 | + set -e |
| 97 | + cd ~/yapper # path to your app on VM |
| 98 | +
|
| 99 | + echo "🧭 Getting current running image tag..." |
| 100 | + CURRENT_TAG=$(IMG_TAG="" docker compose ps -q api | xargs docker inspect -f '{{ .Config.Image }}' | cut -d':' -f2 || echo "unknown") |
| 101 | + echo "CURRENT_TAG: $CURRENT_TAG" |
| 102 | + echo "🐳 Setting new image tag for deployment..." |
| 103 | + echo "Deploying version: $IMG_TAG" |
| 104 | +
|
| 105 | + echo "🔄 Pulling new image and restarting app container..." |
| 106 | + IMG_TAG=$IMG_TAG docker compose pull api api-local api-test |
| 107 | + IMG_TAG=$IMG_TAG docker compose up -d api api-local api-test |
| 108 | +
|
| 109 | + echo "⏳ Waiting for health check..." |
| 110 | + sleep 10 |
| 111 | +
|
| 112 | + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" ${{ secrets.DEV_HEALTHCHECK_URL }}) |
| 113 | +
|
| 114 | + # if [ "$HTTP_CODE" = "200" ]; then |
| 115 | + # echo "✅ Dev deployment successful for version $IMG_TAG" |
| 116 | + |
| 117 | + # echo "🔄 Running migrations for all services..." |
| 118 | + # MIGRATION_FAILED=0 |
| 119 | + |
| 120 | + # echo "📦 Running migration for api..." |
| 121 | + # docker compose exec -T api npm run migration:run:prod 2>&1 | tee /tmp/api-migration.log |
| 122 | + # if grep -q "Error during migration" /tmp/api-migration.log || grep -q "Cannot find module" /tmp/api-migration.log; then |
| 123 | + # echo "❌ Migration for api failed!" |
| 124 | + # cat /tmp/api-migration.log |
| 125 | + # MIGRATION_FAILED=1 |
| 126 | + # elif grep -q "No migrations are pending" /tmp/api-migration.log || grep -q "No pending migrations" /tmp/api-migration.log; then |
| 127 | + # echo "ℹ️ No pending migrations for api" |
| 128 | + # else |
| 129 | + # echo "✅ Migration for api completed successfully" |
| 130 | + # fi |
| 131 | + |
| 132 | + # echo "📦 Running migration for api-local..." |
| 133 | + # docker compose exec -T api-local npm run migration:run:prod 2>&1 | tee /tmp/api-local-migration.log |
| 134 | + # if grep -q "Error during migration" /tmp/api-local-migration.log || grep -q "Cannot find module" /tmp/api-local-migration.log; then |
| 135 | + # echo "❌ Migration for api-local failed!" |
| 136 | + # cat /tmp/api-local-migration.log |
| 137 | + # MIGRATION_FAILED=1 |
| 138 | + # elif grep -q "No migrations are pending" /tmp/api-local-migration.log || grep -q "No pending migrations" /tmp/api-local-migration.log; then |
| 139 | + # echo "ℹ️ No pending migrations for api-local" |
| 140 | + # else |
| 141 | + # echo "✅ Migration for api-local completed successfully" |
| 142 | + # fi |
| 143 | + |
| 144 | + # echo "📦 Running migration for api-test..." |
| 145 | + # docker compose exec -T api-test npm run migration:run:prod 2>&1 | tee /tmp/api-test-migration.log |
| 146 | + # if grep -q "Error during migration" /tmp/api-test-migration.log || grep -q "Cannot find module" /tmp/api-test-migration.log; then |
| 147 | + # echo "❌ Migration for api-test failed!" |
| 148 | + # cat /tmp/api-test-migration.log |
| 149 | + # MIGRATION_FAILED=1 |
| 150 | + # elif grep -q "No migrations are pending" /tmp/api-test-migration.log || grep -q "No pending migrations" /tmp/api-test-migration.log; then |
| 151 | + # echo "ℹ️ No pending migrations for api-test" |
| 152 | + # else |
| 153 | + # echo "✅ Migration for api-test completed successfully" |
| 154 | + # fi |
| 155 | + |
| 156 | + # if [ "$MIGRATION_FAILED" = "1" ]; then |
| 157 | + # echo "❌ One or more migrations failed! Rolling back..." |
| 158 | + # if [ "$CURRENT_TAG" != "unknown" ]; then |
| 159 | + # IMG_TAG=$CURRENT_TAG docker compose up -d api api-local api-test |
| 160 | + # echo "🔙 Rolled back to $CURRENT_TAG" |
| 161 | + # fi |
| 162 | + # exit 1 |
| 163 | + # fi |
| 164 | + |
| 165 | + # echo "✅ All migrations completed successfully" |
| 166 | + # else |
| 167 | + # echo "❌ Health check failed (HTTP $HTTP_CODE)! Rolling back app container..." |
| 168 | + # if [ "$CURRENT_TAG" != "unknown" ]; then |
| 169 | + # IMG_TAG=$CURRENT_TAG docker compose up -d api api-local api-test |
| 170 | + # echo "🔙 Rolled back to $CURRENT_TAG" |
| 171 | + # fi |
| 172 | + # exit 1 |
| 173 | + # fi |
| 174 | +
|
| 175 | + - name: Deployment summary |
| 176 | + if: success() |
| 177 | + run: | |
| 178 | + echo "🎉 Deployment Complete!" |
| 179 | + echo "Version: ${{ steps.get_tag.outputs.new_tag }}" |
| 180 | + echo "Branch: dev" |
| 181 | + echo "Commit: ${{ github.sha }}" |
0 commit comments