Skip to content

Commit ba59360

Browse files
committed
fix(ci): update ci
1 parent 708e159 commit ba59360

File tree

2 files changed

+368
-108
lines changed

2 files changed

+368
-108
lines changed

.github/workflows/multi_container_application.yaml

Lines changed: 26 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: Build and Deploy Todo API
33
on:
44
push:
55
branches: [main]
6+
paths:
7+
- 'multi_container_application/**'
68
workflow_dispatch:
79

810
env:
@@ -53,123 +55,39 @@ jobs:
5355
- name: Checkout code
5456
uses: actions/checkout@v4
5557

58+
- name: Copy files to server
59+
uses: appleboy/scp-action@v0.1.7
60+
with:
61+
host: ${{ secrets.SERVER_HOST }}
62+
username: ${{ secrets.SERVER_USER }}
63+
key: ${{ secrets.SSH_PRIVATE_KEY }}
64+
source: "multi_container_application/docker-compose.yaml,multi_container_application/nginx.conf"
65+
target: "~/todo-app"
66+
strip_components: 0
67+
5668
- name: Deploy to remote server
5769
uses: appleboy/ssh-action@v1.0.3
5870
with:
5971
host: ${{ secrets.SERVER_HOST }}
6072
username: ${{ secrets.SERVER_USER }}
6173
key: ${{ secrets.SSH_PRIVATE_KEY }}
6274
script: |
63-
# Create project directory
64-
mkdir -p ~/todo-app
6575
cd ~/todo-app
66-
67-
# Login to GitHub Container Registry
76+
77+
# Login to GHCR
6878
echo ${{ secrets.GITHUB_TOKEN }} | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin
69-
70-
# Stop and remove existing containers
71-
docker-compose down || true
72-
79+
7380
# Pull latest image
7481
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
75-
76-
# Create docker-compose.yml
77-
cat > docker-compose.yml << 'EOF'
78-
version: '3.8'
79-
services:
80-
mongodb:
81-
image: mongo:7-jammy
82-
container_name: todo-mongodb
83-
restart: unless-stopped
84-
environment:
85-
- MONGO_INITDB_DATABASE=todos
86-
volumes:
87-
- mongodb_data:/data/db
88-
networks:
89-
- todo-network
90-
healthcheck:
91-
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
92-
interval: 10s
93-
timeout: 5s
94-
retries: 5
95-
96-
api:
97-
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
98-
container_name: todo-api
99-
restart: unless-stopped
100-
ports:
101-
- "3000:3000"
102-
environment:
103-
- PORT=3000
104-
- MONGODB_URI=mongodb://mongodb:27017/todos
105-
depends_on:
106-
mongodb:
107-
condition: service_healthy
108-
networks:
109-
- todo-network
110-
111-
nginx:
112-
image: nginx:alpine
113-
container_name: todo-nginx
114-
restart: unless-stopped
115-
ports:
116-
- "80:80"
117-
volumes:
118-
- ./nginx.conf:/etc/nginx/nginx.conf:ro
119-
depends_on:
120-
- api
121-
networks:
122-
- todo-network
123-
124-
volumes:
125-
mongodb_data:
126-
driver: local
127-
128-
networks:
129-
todo-network:
130-
driver: bridge
131-
EOF
132-
133-
# Create nginx.conf
134-
cat > nginx.conf << 'EOF'
135-
events {
136-
worker_connections 1024;
137-
}
138-
139-
http {
140-
upstream api {
141-
server api:3000;
142-
}
143-
144-
server {
145-
listen 80;
146-
server_name _;
147-
148-
client_max_body_size 10M;
149-
150-
location / {
151-
proxy_pass http://api;
152-
proxy_http_version 1.1;
153-
proxy_set_header Upgrade $http_upgrade;
154-
proxy_set_header Connection 'upgrade';
155-
proxy_set_header Host $host;
156-
proxy_set_header X-Real-IP $remote_addr;
157-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
158-
proxy_set_header X-Forwarded-Proto $scheme;
159-
proxy_cache_bypass $http_upgrade;
160-
}
161-
162-
location /health {
163-
access_log off;
164-
return 200 "healthy\n";
165-
add_header Content-Type text/plain;
166-
}
167-
}
168-
}
169-
EOF
170-
171-
# Start containers
172-
docker-compose up -d
173-
82+
83+
# Update image reference in compose file
84+
export IMAGE_NAME=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
85+
86+
# Stop old containers
87+
docker-compose -f docker-compose.prod.yml down
88+
89+
# Start new containers
90+
docker-compose -f docker-compose.prod.yml up -d
91+
17492
# Clean up old images
175-
docker image prune -af
93+
docker image prune -af

0 commit comments

Comments
 (0)