-
Notifications
You must be signed in to change notification settings - Fork 0
137 lines (120 loc) · 3.91 KB
/
cd.yml
File metadata and controls
137 lines (120 loc) · 3.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
# Continuous Deployment Pipeline
# Deploys to production when pushing to main or creating a release
name: CD
on:
push:
branches: [main]
release:
types: [published]
workflow_dispatch:
inputs:
environment:
description: 'Deployment environment'
required: true
default: 'production'
type: choice
options:
- staging
- production
env:
NODE_VERSION: '20'
DOCKERHUB_USERNAME: mwmsoftware
jobs:
# Build and push Docker images to Docker Hub
build-and-push:
name: Build and Push to Docker Hub
runs-on: ubuntu-latest
outputs:
backend-image: ${{ env.DOCKERHUB_USERNAME }}/mwm-backend:${{ github.sha }}
frontend-image: ${{ env.DOCKERHUB_USERNAME }}/mwm-frontend:${{ github.sha }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (backend)
id: meta-backend
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKERHUB_USERNAME }}/mwm-backend
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=sha,prefix=
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Extract metadata (frontend)
id: meta-frontend
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKERHUB_USERNAME }}/mwm-frontend
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=sha,prefix=
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Build and push backend
uses: docker/build-push-action@v5
with:
context: .
file: ./backend/Dockerfile.prod
push: true
tags: ${{ steps.meta-backend.outputs.tags }}
labels: ${{ steps.meta-backend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
- name: Build and push frontend
uses: docker/build-push-action@v5
with:
context: .
file: ./frontend/Dockerfile.prod
push: true
tags: ${{ steps.meta-frontend.outputs.tags }}
labels: ${{ steps.meta-frontend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
build-args: |
NEXT_PUBLIC_API_URL=${{ secrets.API_URL }}
NEXT_PUBLIC_SITE_URL=${{ secrets.SITE_URL }}
# Deploy to environment
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: build-and-push
environment:
name: ${{ github.event.inputs.environment || 'production' }}
url: ${{ vars.SITE_URL }}
steps:
- name: Deploy to server
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /opt/mwm
docker-compose -f docker-compose.prod.yml pull
docker-compose -f docker-compose.prod.yml up -d
docker system prune -f
- name: Health check
run: |
sleep 30
curl -f ${{ vars.SITE_URL }}/health || exit 1
curl -f ${{ vars.API_URL }}/api/v1/health || exit 1
- name: Notify on success
if: success()
run: |
echo "✅ Deployment successful!"
echo "🌐 Site: ${{ vars.SITE_URL }}"
echo "🔗 API: ${{ vars.API_URL }}"
- name: Notify on failure
if: failure()
run: |
echo "❌ Deployment failed!"
exit 1