-
Notifications
You must be signed in to change notification settings - Fork 0
162 lines (132 loc) · 4.75 KB
/
ci-cd.yml
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
name: CI/CD Pipeline
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
env:
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
GKE_CLUSTER: eco-go-cluster
GKE_ZONE: us-central1
DOCKER_USERNAME: joelkodji
DEPLOYMENT_NAME: eco-go
jobs:
test:
name: Test Application
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_db
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: mbstring, xml, ctype, iconv, intl, pdo_pgsql, curl, dom
coverage: xdebug
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Delete composer.lock
run: rm -f composer.lock
- name: Install dependencies
run: |
composer install --prefer-dist --no-progress --no-suggest
composer dump-autoload -o
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs --standard=PSR12 src/ || true
- name: Run PHPUnit tests
run: vendor/bin/phpunit --testdox --colors=always || true
build-and-push:
name: Build and Push Docker Images
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker images
run: |
# Build and push backend
docker build -t joelkodji/eco-go:backend-${GITHUB_SHA} ./backend
docker push joelkodji/eco-go:backend-${GITHUB_SHA}
# Build and push frontend
docker build -t joelkodji/eco-go:frontend-${GITHUB_SHA} ./frontend
docker push joelkodji/eco-go:frontend-${GITHUB_SHA}
- name: Set up Kustomize
run: |-
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
sudo mv kustomize /usr/local/bin/
- name: Update Kustomize image tags
run: |-
cd kubernetes/overlays/production
kustomize edit set image joelkodji/eco-go:backend=joelkodji/eco-go:backend-${GITHUB_SHA}
kustomize edit set image joelkodji/eco-go:frontend=joelkodji/eco-go:frontend-${GITHUB_SHA}
deploy:
name: Deploy to GKE
needs: build-and-push
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ env.PROJECT_ID }}
- name: Get GKE credentials
uses: google-github-actions/get-gke-credentials@v1
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}
- name: Set up Kustomize
run: |-
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
sudo mv kustomize /usr/local/bin/
- name: Update Kustomize image tags
run: |-
cd kubernetes/overlays/production
kustomize edit set image joelkodji/eco-go:backend=joelkodji/eco-go:backend-${GITHUB_SHA}
kustomize edit set image joelkodji/eco-go:frontend=joelkodji/eco-go:frontend-${GITHUB_SHA}
- name: Deploy to GKE
run: |-
# Create secrets first
kubectl create secret generic postgres-secret \
--from-literal=POSTGRES_USER=postgres \
--from-literal=POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} \
--from-literal=POSTGRES_DB=ecogo \
--dry-run=client -o yaml | kubectl apply -f -
# Apply Kustomize configurations
kubectl apply -k kubernetes/overlays/production
# Verify deployments
kubectl rollout status deployment/backend
kubectl rollout status deployment/frontend
kubectl rollout status deployment/pgadmin