This repository was archived by the owner on Apr 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
243 lines (198 loc) · 9.14 KB
/
Copy pathdeployment.yml
File metadata and controls
243 lines (198 loc) · 9.14 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
name: Kraya AI Inventory Manager Deployment
on:
# pull_request:
# branches: [dev, prod]
push:
branches: [dev, prod]
env:
IMAGE_NAME: kraya_ai_backend:dev
R2_BUCKET: vanguard-artifacts
DEPLOY_PATH: /home/ubuntu/vanguard_ai/AI-Inventory-Manager-3.0
jobs:
build:
runs-on: ubuntu-latest
environment: dev
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Compose
run: |
sudo curl -L "https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
- name: Configure AWS CLI for R2
run: |
aws configure set aws_access_key_id ${{ secrets.R2_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.R2_SECRET_ACCESS_KEY }}
aws configure set region auto
aws configure set endpoint_url https://${{ secrets.R2_ACCOUNT_ID }}.r2.cloudflarestorage.com
- name: Fetch .env from Doppler
run: |
curl --fail-with-body --request GET \
--url 'https://api.doppler.com/v3/configs/config/secrets/download?format=env' \
--header "Authorization: Bearer ${{ secrets.DOPPLER_TOKEN }}" \
| sed 's/"//g' > .env
# Verify .env file was created and has content
ls -la .env
- name: Build with docker-compose
run: |
docker-compose -f docker-compose.dev.yml build
- name: Save and compress backend image
run: |
# Save the backend image and compress it with maximum compression
docker save ${{ env.IMAGE_NAME }} | gzip -1 > backend.tar.gz
- name: Upload compressed image to R2
run: |
aws s3 cp backend.tar.gz s3://${{ env.R2_BUCKET }}/deployments/${{ github.sha }}/backend.tar.gz --checksum-algorithm CRC32
deploy-dev:
needs: build
environment: dev
if: github.ref == 'refs/heads/dev'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Deploy to VM
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEV_VM_HOST }}
username: ${{ secrets.DEV_VM_USERNAME }}
key: ${{ secrets.DEV_VM_KEY }}
port: ${{ secrets.DEV_VM_PORT }}
debug: true
script: |
set -x # Enable bash debugging
echo "=== Starting Deployment Process ==="
# Check AWS CLI installation
if ! command -v aws &> /dev/null; then
echo "Installing AWS CLI..."
sudo apt-get update
sudo apt-get install -y unzip curl
# Remove any existing AWS CLI installation
sudo rm -rf /usr/local/aws-cli /usr/local/bin/aws
# Download and install AWS CLI v2
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update
# Clean up installation files
rm -rf aws awscliv2.zip
# Verify installation
aws --version || {
echo "AWS CLI installation failed"
exit 1
}
fi
echo "=== AWS CLI Version ==="
aws --version
# Setup AWS CLI for R2
echo "Configuring AWS CLI..."
aws configure set aws_access_key_id ${{ secrets.R2_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.R2_SECRET_ACCESS_KEY }}
aws configure set region auto
aws configure set endpoint_url https://${{ secrets.R2_ACCOUNT_ID }}.r2.cloudflarestorage.com
# Check/Create deployment directory
echo "Setting up deployment directory..."
mkdir -p ${{ env.DEPLOY_PATH }}
cd ${{ env.DEPLOY_PATH }}
pwd
ls -la
# Check Docker and Docker Compose installation
echo "=== Docker Version ==="
docker --version
docker-compose --version
# Pull the code
git pull https://oauth2:${{ secrets.GH_TOKEN }}@github.com/dudegladiator/AI-Inventory-Manager-3.0.git dev
# Fetch .env file from Doppler
echo "Fetching .env from Doppler..."
curl --fail-with-body --request GET \
--url 'https://api.doppler.com/v3/configs/config/secrets/download?format=env' \
--header "Authorization: Bearer ${{ secrets.DOPPLER_TOKEN }}" \
| sed 's/"//g' > .env
chmod 644 .env
# Run Traefik container
echo "Starting Traefik container..."
chmod 600 traefik-config/config/acme.json
docker network create backend
docker-compose -f traefik-config/docker-compose.yml up -d || {
echo "Failed to start Traefik container"
docker-compose -f traefik-config/docker-compose.yml logs
exit 1
}
# Download and load backend image
echo "Downloading and loading backend image..."
aws s3 ls s3://${{ env.R2_BUCKET }}/deployments/${{ github.sha }}/ || {
echo "Failed to list deployment files"
exit 1
}
aws s3 cp s3://${{ env.R2_BUCKET }}/deployments/${{ github.sha }}/backend.tar.gz - | gunzip | docker load || {
echo "Failed to download and load backend image"
exit 1
}
# Verify images
echo "=== Available Docker Images ==="
docker images
# Stop current services
echo "Stopping current services..."
docker-compose -f docker-compose.dev.yml down || true
# Show running containers
echo "=== Currently Running Containers ==="
docker ps
# Deploy new services
echo "Deploying new services..."
docker-compose -f docker-compose.dev.yml up -d || {
echo "Failed to start services"
docker-compose -f docker-compose.dev.yml logs
exit 1
}
# Verify deployment
echo "=== Deployment Status ==="
docker-compose -f docker-compose.dev.yml ps
docker-compose -f docker-compose.dev.yml logs
# Cleanup
echo "Performing cleanup..."
docker system prune -af --volumes
echo "=== Deployment Complete ==="
cleanup:
needs: [deploy-dev]
if: always()
runs-on: ubuntu-latest
environment: dev
steps:
- name: Configure AWS CLI for R2
run: |
aws configure set aws_access_key_id ${{ secrets.R2_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.R2_SECRET_ACCESS_KEY }}
aws configure set region auto
aws configure set endpoint_url https://${{ secrets.R2_ACCOUNT_ID }}.r2.cloudflarestorage.com
- name: List and Cleanup old deployments
run: |
echo "=== Current Deployments in R2 ==="
aws s3 ls s3://${{ env.R2_BUCKET }}/deployments/
# Get all deployment directories (removing the PRE prefix and trailing slash)
DEPLOYMENTS=$(aws s3 ls s3://${{ env.R2_BUCKET }}/deployments/ | awk '{print $2}' | sed 's/\/$//')
# Convert to array and sort
readarray -t SORTED_DEPLOYMENTS < <(echo "$DEPLOYMENTS" | sort -r)
# Count total deployments
TOTAL_DEPLOYMENTS=${#SORTED_DEPLOYMENTS[@]}
echo "Total number of deployments: $TOTAL_DEPLOYMENTS"
# Keep only the 2 most recent deployments
if [ "$TOTAL_DEPLOYMENTS" -gt 0 ]; then
echo "More than 2 deployments found. Cleaning up old ones..."
# Loop through all except the first two
for ((i=0; i<TOTAL_DEPLOYMENTS; i++)); do
DIR_TO_REMOVE="${SORTED_DEPLOYMENTS[$i]}"
if [ ! -z "$DIR_TO_REMOVE" ]; then
echo "Removing deployment: $DIR_TO_REMOVE"
# List and remove all files in the directory
FILES=$(aws s3 ls "s3://${{ env.R2_BUCKET }}/deployments/$DIR_TO_REMOVE/" | awk '{print $4}')
for FILE in $FILES; do
echo "Removing file: $FILE"
aws s3 rm "s3://${{ env.R2_BUCKET }}/deployments/$DIR_TO_REMOVE/$FILE"
done
# Remove the empty directory
aws s3 rm "s3://${{ env.R2_BUCKET }}/deployments/$DIR_TO_REMOVE/"
echo "Removal complete for: $DIR_TO_REMOVE"
fi
done
else
echo "No cleanup needed. Total deployments: $TOTAL_DEPLOYMENTS"
fi