Skip to content

Commit

Permalink
feat: add init container to inject db init script
Browse files Browse the repository at this point in the history
  • Loading branch information
amber committed Oct 24, 2024
1 parent fd2e606 commit 1ab8598
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
27 changes: 27 additions & 0 deletions helm/nshm/templates/account/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ spec:
labels:
app: account
spec:
initContainers:
- name: git-sync
image: alpine/git:latest
command: ["git", "clone", "https://github.com/SE-Row-1/nus-secondhand-market.git", "/scripts"]
volumeMounts:
- name: init-script
mountPath: /scripts

- name: init-db
image: postgres:13
envFrom:
- secretRef:
name: account-secret
command: ["/bin/sh", "-c"]
args:
- |
export PGPASSWORD=$POSTGRES_PASSWORD && \
psql $POSTGRES_URL -U $POSTGRES_USERNAME -f /scripts/services/account/database/1-schema.sql && \
psql $POSTGRES_URL -U $POSTGRES_USERNAME -f /scripts/services/account/database/2-seed.sql
volumeMounts:
- name: init-script
mountPath: /scripts

containers:
- name: account
image: {{ .Values.account.image.repository }}:{{ .Values.account.image.tag }}
Expand Down Expand Up @@ -41,3 +64,7 @@ spec:
initialDelaySeconds: 30
periodSeconds: 30
failureThreshold: 3

volumes:
- name: init-script
emptyDir: {}
17 changes: 16 additions & 1 deletion helm/nshm/templates/item-mongo/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -14,6 +13,14 @@ spec:
labels:
app: item-mongo
spec:
initContainers:
- name: git-sync
image: alpine/git:latest
command: ["git", "clone", "https://github.com/SE-Row-1/nus-secondhand-market.git", "/scripts"]
volumeMounts:
- name: init-script
mountPath: /scripts

containers:
- name: item-mongo
image: bitnami/mongodb:latest
Expand All @@ -34,3 +41,11 @@ spec:
initialDelaySeconds: 30
periodSeconds: 10
failureThreshold: 3
volumeMounts:
- name: init-script
mountPath: /docker-entrypoint-initdb.d
subPath: services/item/database/production

volumes:
- name: init-script
emptyDir: {}
3 changes: 3 additions & 0 deletions helm/nshm/templates/rabbitmq-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ spec:
limits:
cpu: "512m"
memory: "512Mi"
persistence:
storageClassName: gp3
storage: 10Gi
16 changes: 16 additions & 0 deletions helm/nshm/templates/wishlist-mongo/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ spec:
labels:
app: wishlist-mongo
spec:
initContainers:
- name: git-sync
image: alpine/git:latest
command: ["git", "clone", "https://github.com/SE-Row-1/nus-secondhand-market.git", "/scripts"]
volumeMounts:
- name: init-script
mountPath: /scripts

containers:
- name: wishlist-mongo
image: bitnami/mongodb:latest
Expand All @@ -33,3 +41,11 @@ spec:
initialDelaySeconds: 30
periodSeconds: 10
failureThreshold: 3
volumeMounts:
- name: init-script
mountPath: /docker-entrypoint-initdb.d
subPath: services/wishlist/database/dev

volumes:
- name: init-script
emptyDir: {}
21 changes: 21 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# List of services
services=("account" "item" "notification" "wishlist" "web")

# Loop through each service
for service in "${services[@]}"; do
echo "Building and pushing Docker image for $service..."

# Build the Docker image with two tags
docker build -t z1yoon/nshm-${service}:latest \
--build-arg NEXT_PUBLIC_API_BASE_URL=${NEXT_PUBLIC_API_BASE_URL} \
./services/${service}

# Push both tags
docker push z1yoon/nshm-${service}:${GITHUB_SHA}
docker push z1yoon/nshm-${service}:latest

echo "$service Docker image pushed!"
done

0 comments on commit 1ab8598

Please sign in to comment.