-
Notifications
You must be signed in to change notification settings - Fork 0
155 lines (131 loc) · 5.91 KB
/
deploy.yml
File metadata and controls
155 lines (131 loc) · 5.91 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
name: '🚀 Deploy NestJS API Docker App'
permissions:
contents: write
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: self-hosted
name: '🐳 Build & Deploy'
steps:
- name: '🔍 Checkout Code'
uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: '📦 Setup Node.js'
uses: actions/setup-node@v4
with:
node-version: 22.x
- name: '🧩 Install dependencies (npm)'
run: npm install --legacy-peer-deps
- name: '🧩 Install semver for version bump'
run: npm install semver --legacy-peer-deps
- name: '🔄 Auto-bump version (main only)'
if: github.ref == 'refs/heads/main'
run: node scripts/bumpVersion.js
- name: '🔧 Configure Git for Automation'
if: github.ref == 'refs/heads/main'
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "actions@github.com"
- name: '💾 Commit Version Update'
if: github.ref == 'refs/heads/main'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git add version.json package.json
git commit -m "chore: Auto-increment version [skip ci]" || echo "No changes to commit"
git push
- name: '🧹 Version bump cleanup'
run: rm -rf node_modules
- name: '�🔒 Verify Secrets Exist'
run: |
if [ -z "${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}" ]; then
echo "❌ Critical error: GOOGLE_SERVICES_JSON_BASE64 secret missing!"
exit 1
fi
echo "✅ All secrets present"
- name: '📁 Create google-services.json'
run: |
echo "$GOOGLE_SERVICES_JSON_BASE64" | base64 --decode > google-services.json
echo "🔄 Validating JSON..."
if ! jq empty google-services.json; then
echo "❌ JSON validation failed!"
exit 1
fi
env:
GOOGLE_SERVICES_JSON_BASE64: ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}
- name: '⚙️ Create .env File'
run: |
echo "${{ secrets.ENV_FILE_CONTENT }}" > .env
echo "" >> .env
# =======================================================
# 🐳 Docker Operations
# =======================================================
- name: '� Debug: List workspace files and show package.json'
run: |
echo '--- DEBUG: Listing workspace files ---'
ls -alh
echo '--- DEBUG: Showing package.json ---'
cat package.json
echo '--- DEBUG: Listing node_modules/.bin if exists ---'
if [ -d node_modules/.bin ]; then ls -l node_modules/.bin; else echo "node_modules/.bin does not exist"; fi
# Ensure the Postgres data volume exists before starting services
- name: '🔧 Ensure Postgres data volume exists'
run: |
if [ -z "$(docker volume ls -q -f name=codebuilder-postgres-data)" ]; then
echo "Volume 'codebuilder-postgres-data' not found. Creating it..."
docker volume create codebuilder-postgres-data
else
echo "Volume 'codebuilder-postgres-data' already exists. Skipping creation."
fi
- name: '�🚀 Build, Launch, and Update Services'
run: |
# Step 1: Ensure the Docker network exists.
if ! docker network ls | grep -q "codebuilder-net"; then
echo "Network 'codebuilder-net' not found. Creating it..."
docker network create codebuilder-net
else
echo "Network 'codebuilder-net' already exists. Skipping creation."
fi
# Step 2: Ensure the database container is running.
DB_CONTAINER_NAME="codebuilder-postgres-db"
if [ $(docker ps -a -q -f name=^/${DB_CONTAINER_NAME}$) ]; then
if ! [ $(docker ps -q -f name=^/${DB_CONTAINER_NAME}$) ]; then
echo "Database container exists but is stopped. Starting it..."
docker start ${DB_CONTAINER_NAME}
fi
else
echo "Database container not found. Creating it..."
# Use 'codebuilder' as the stack prefix
docker compose -p codebuilder up -d db
fi
# Step 3: Wait for the database to be healthy.
echo "Waiting for database to become available on localhost:5434..."
while ! nc -z localhost 5434; do sleep 1; done
echo "✅ Database is healthy."
# =====================================================================
# THE FIX: Force the build to run in default server mode.
# This overrides any conflicting environment variables.
# =====================================================================
echo "Ensuring build runs in default server mode..."
export NEXT_OUTPUT_MODE='standalone'
# Step 4: Build the latest api image.
echo "Building the latest api image..."
# Get the current commit hash of the prisma submodule
# This ensures Docker rebuilds the prisma layer when the submodule is updated
PRISMA_COMMIT=$(git submodule status prisma | awk '{print $1}' | sed 's/^+//')
echo "Using Prisma submodule commit: $PRISMA_COMMIT"
# Force a complete rebuild with no cache to ensure fresh prisma schema
docker compose -p codebuilder build --no-cache api
# Step 5: Forcefully remove the old api container to prevent conflicts.
echo "Forcefully removing old api container if it exists..."
docker rm -f codebuilder-api || true
# Step 6: Deploy the new api container.
echo "Deploying the new api container..."
# Use 'codebuilder' as the stack prefix
docker compose -p codebuilder up -d --no-deps api
- name: '🗑 Prune Old Docker Images'
if: always()
run: docker image prune -af