-
Notifications
You must be signed in to change notification settings - Fork 2
194 lines (169 loc) · 9.56 KB
/
mainServer.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
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
name: mainServer
on:
push:
branches:
- main
workflow_dispatch:
concurrency:
group: "gcife-${{ github.head_ref }}"
cancel-in-progress: true
jobs:
mainServer:
strategy:
matrix:
node-version: [20.x]
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
env:
NODE_ENV: production
MICROSERVICE_HOST: 0.0.0.0
USERS_SERVICE_NAME: klowhub-users-api
USERS_SERVICE_PORT: 3001
COURSE_SERVICE_NAME: klowhub-courses-api
COURSES_SERVICE_PORT: 3002
GATEWAY_SERVICE_NAME: klowhub-gateway-api
GATEWAY_SERVICE_PORT: 3000
UPLOAD_SERVICE_NAME: klowhub-upload-api
UPLOAD_SERVICE_PORT: 3003
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
# 1. Autenticarse con google cloud
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
project_id: "${{ secrets.GCLOUD_PROJECT_ID }}"
credentials_json: "${{ secrets.GCLOUD_SERVICE_ACCOUNT_KEY }}"
# 2. Set up Google Cloud SDK
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
# 3. Exportar la región como variable de entorno
- name: Export region
run: echo "GCLOUD_REGION=${{ secrets.GCLOUD_REGION }}" >> $GITHUB_ENV
# 4. Configurar kubectl
- name: Set up GKE
uses: google-github-actions/get-gke-credentials@v2
with:
cluster_name: ${{ secrets.GKE_CLUSTER_NAME }}
location: ${{ secrets.GCLOUD_REGION }}
# 5. Agregar dinamicamente BACKEND_SA_KEY
- name: Create service account key file
working-directory: ./server/apps/uploadGCould
run: echo "${{ secrets.GCC_BACKEND_SA_KEY }}" > ./gccKey.json
# 6. Autenticarse con Artifact Registry
- name: Authenticate with Artifact Registry
working-directory: ./server
run: gcloud auth configure-docker $GCLOUD_REGION-docker.pkg.dev
# 6.5. Valido el accedo a Artifact Registry
- name: Validate Docker authentication
run: docker login $GCLOUD_REGION-docker.pkg.dev
# 7. Construir la imagen Docker para User Service
- name: Users Service - Build Docker image
working-directory: ./server
run: docker build --progress plain --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} --build-arg SMTP_USER=${{ secrets.SMTP_USER }} --build-arg SMTP_PASS=${{ secrets.SMTP_PASS }} --build-arg SMTP_HOST=${{ secrets.SMTP_HOST }} --build-arg SMTP_PORT=${{ secrets.SMTP_PORT }} --build-arg SMTP_SECURE=${{ secrets.SMTP_SECURE }} --build-arg GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }} --build-arg GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }} --build-arg GOOGLE_CALLBACK_URL=${{ secrets.GOOGLE_CALLBACK_URL }} --build-arg DATABASE_URL=${{ secrets.DATABASE_URL }} --build-arg DATABASE_URL_UNPOOLED=${{ secrets.DATABASE_URL_UNPOOLED }} --build-arg PGHOST=${{ secrets.PGHOST }} --build-arg PGHOST_UNPOOLED=${{ secrets.PGHOST_UNPOOLED }} --build-arg PGUSER=${{ secrets.PGUSER }} --build-arg PGDATABASE=${{ secrets.PGDATABASE }} --build-arg PGPASSWORD=${{ secrets.PGPASSWORD }} --build-arg POSTGRES_URL=${{ secrets.POSTGRES_URL }} --build-arg POSTGRES_URL_NON_POOLING=${{ secrets.POSTGRES_URL_NON_POOLING }} --build-arg POSTGRES_USER=${{ secrets.POSTGRES_USER }} --build-arg POSTGRES_HOST=${{ secrets.POSTGRES_HOST }} --build-arg POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} --build-arg POSTGRES_DATABASE=${{ secrets.POSTGRES_DATABASE }} --build-arg POSTGRES_URL_NO_SSL=${{ secrets.POSTGRES_URL_NO_SSL }} --build-arg POSTGRES_PRISMA_URL=${{ secrets.POSTGRES_PRISMA_URL }} -f ./apps/users/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.USERS_SERVICE_NAME }}:${{ github.sha }} ./apps/users
# 8. Construir la imagen Docker para Courses Service
- name: Courses Service - Build Docker image
working-directory: ./server
run: docker build --progress plain --build-arg MONGO_URI=${{ secrets.MONGO_URI }} --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} -f ./apps/courses/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.COURSE_SERVICE_NAME }}:${{ github.sha }} ./apps/courses
# 8. Construir la imagen Docker para Gateway Service
- name: Gateway Service - Build Docker image
working-directory: ./server
run: docker build --progress plain --build-arg FRONTEND_URL=${{ secrets.FRONTEND_URL }} --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} -f ./apps/gateway/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.GATEWAY_SERVICE_NAME }}:${{ github.sha }} ./apps/gateway
# 8. Construir la imagen Docker para Upload Service
- name: Upload Service - Build Docker image
working-directory: ./server
run: docker build --progress plain --build-arg PROJECT_ID=${{ secrets.PROJECT_ID }} --build-arg BUCKET_NAME=${{ secrets.BUCKET_NAME }} -f ./apps/uploadGCould/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.UPLOAD_SERVICE_NAME }}:${{ github.sha }} ./apps/uploadGCould
# 9. Subir la imagen de User Service a Artifact Registry
- name: Users Service - Push Docker image
working-directory: ./server
run: docker push ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.USERS_SERVICE_NAME }}:${{ github.sha }}
# 10. Subir la imagen de Courses Service a Artifact Registry
- name: Courses Service - Push Docker image
working-directory: ./server
run: docker push ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.COURSE_SERVICE_NAME }}:${{ github.sha }}
# 10. Subir la imagen de Upload Service a Artifact Registry
- name: Upload Service - Push Docker image
working-directory: ./server
run: docker push ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.UPLOAD_SERVICE_NAME }}:${{ github.sha }}
# 11. Subir la imagen de Gateway Service a Artifact Registry
- name: Gateway Service - Push Docker image
working-directory: ./server
run: docker push ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.GATEWAY_SERVICE_NAME }}:${{ github.sha }}
# 12. Setear un .env para la susticucion de variables
- name: Create env file for substitution
working-directory: ./server
run: |
cat << EOF > .env
GCP_PROJECT_ID=${{ secrets.GCP_PROJECT_ID }}
IMAGE_TAG=${{ github.sha }}
JWT_SECRET=${{ secrets.JWT_SECRET }}
SMTP_USER=${{ secrets.SMTP_USER }}
SMTP_PASS=${{ secrets.SMTP_PASS }}
SMTP_HOST=${{ secrets.SMTP_HOST }}
SMTP_PORT=${{ secrets.SMTP_PORT }}
SMTP_SECURE=${{ secrets.SMTP_SECURE }}
GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }}
GOOGLE_CALLBACK_URL=${{ secrets.GOOGLE_CALLBACK_URL }}
DATABASE_URL=${{ secrets.DATABASE_URL }}
PGHOST=${{ secrets.PGHOST }}
PGHOST_UNPOOLED=${{ secrets.PGHOST_UNPOOLED }}
PGUSER=${{ secrets.PGUSER }}
PGDATABASE=${{ secrets.PGDATABASE }}
PGPASSWORD=${{ secrets.PGPASSWORD }}
POSTGRES_URL=${{ secrets.POSTGRES_URL }}
POSTGRES_URL_NON_POOLING=${{ secrets.POSTGRES_URL_NON_POOLING }}
POSTGRES_USER=${{ secrets.POSTGRES_USER }}
POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}
POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_DATABASE=${{ secrets.POSTGRES_DATABASE }}
POSTGRES_URL_NO_SSL=${{ secrets.POSTGRES_URL_NO_SSL }}
POSTGRES_PRISMA_URL=${{ secrets.POSTGRES_PRISMA_URL }}
PROJECT_ID=${{ secrets.PROJECT_ID }}
BUCKET_NAME=${{ secrets.BUCKET_NAME }}
MONGO_URI=${{ secrets.MONGO_URI }}
EOF
# 13. Sustituir variables en los manifiestos en K8s
- name: Substitute variables in K8s manifests
working-directory: ./server
run: |
mkdir -p k8s-rendered
for file in k8s/*.{yml,yaml}; do
if [ -f "$file" ]; then
echo "Processing $file..."
envsubst < "$file" > "k8s-rendered/$(basename $file)"
fi
done
env:
DOLLAR: '$'
# 14. Primera - Verificando manifiestos
- name: Verify rendered manifests
working-directory: ./server
run: |
for file in k8s-rendered/*.{yml,yaml}; do
if [ -f "$file" ]; then
echo "Validating $file..."
kubectl apply --dry-run=client -f "$file"
fi
done
# 14.5. Triple - Verificación de los manifiestos
- name: Verify file extensions
working-directory: ./server
run: |
for file in k8s-rendered/*; do
if [[ ! "$file" =~ \.yaml$|\.yml$ ]]; then
echo "Invalid file extension: $file"
exit 1
fi
done
# 15. Aplicar manifiestos a los kubernetes
- name: Apply Kubernetes manifests
working-directory: ./server
run: |
kubectl apply -f k8s-rendered/
kubectl wait --for=condition=available deployment --all --timeout=300s