From c6cb804a0527158c4d9d4569da218785e27375b2 Mon Sep 17 00:00:00 2001 From: Jun-te Kim Date: Wed, 21 Jan 2026 21:24:39 +0000 Subject: [PATCH 1/5] readded postgres deployment --- .github/workflows/stripe-to-invoice.yml | 56 ++++++++++++++- db/k8s/postgres/stripe-to-invoice-db.yaml | 88 +++++++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 db/k8s/postgres/stripe-to-invoice-db.yaml diff --git a/.github/workflows/stripe-to-invoice.yml b/.github/workflows/stripe-to-invoice.yml index 4ba5ae7..b2ac901 100644 --- a/.github/workflows/stripe-to-invoice.yml +++ b/.github/workflows/stripe-to-invoice.yml @@ -40,13 +40,67 @@ jobs: push: true tags: docker.io/kimjunte/stripe_to_invoice:${{ env.GITHUB_REF_SLUG }} + # -------------------------------------------------- + # DEPLOY POSTGRES (DEV + PROD) + # -------------------------------------------------- + deploy-db: + name: Deploy Postgres (PV + PVC + Deployment) + runs-on: mealcraft-runners + needs: build + + steps: + - uses: actions/checkout@v4 + + - name: Install kubectl + run: | + sudo apt-get update + sudo apt-get install -y curl ca-certificates gettext + curl -LO "https://dl.k8s.io/release/$(curl -sL https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" + sudo install -m 0755 kubectl /usr/local/bin/kubectl + + - name: Configure kubeconfig (in-cluster) + run: | + KUBE_HOST="https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT" + SA_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) + CA_CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt + + kubectl config set-cluster microk8s \ + --server="$KUBE_HOST" \ + --certificate-authority="$CA_CERT" + + kubectl config set-credentials runner --token="$SA_TOKEN" + + kubectl config set-context runner-context \ + --cluster=microk8s \ + --user=runner + + kubectl config use-context runner-context + + - name: Decide environment + run: | + if [[ "$GITHUB_REF" == refs/heads/main || "$GITHUB_REF" == refs/tags/* || "$GITHUB_REF" == refs/heads/release/* ]]; then + echo "ENV=prod" >> $GITHUB_ENV + echo "NAMESPACE=default" >> $GITHUB_ENV + echo "PG_VOLUME=stripe_invoice_prod" >> $GITHUB_ENV + else + echo "ENV=dev" >> $GITHUB_ENV + echo "NAMESPACE=dev" >> $GITHUB_ENV + echo "PG_VOLUME=stripe_invoice_dev" >> $GITHUB_ENV + fi + + - name: Apply Postgres manifests + run: | + export ENV NAMESPACE PG_VOLUME + envsubst < db/k8s/postgres/stripe-to-invoice-db.yaml | kubectl apply -f - + + # -------------------------------------------------- # APPLY DB + APP SECRETS # -------------------------------------------------- secrets: name: Apply runtime secrets runs-on: mealcraft-runners - needs: build + needs: deploy-db steps: - uses: actions/checkout@v4 diff --git a/db/k8s/postgres/stripe-to-invoice-db.yaml b/db/k8s/postgres/stripe-to-invoice-db.yaml new file mode 100644 index 0000000..e18ffe8 --- /dev/null +++ b/db/k8s/postgres/stripe-to-invoice-db.yaml @@ -0,0 +1,88 @@ +# -------------------------------------------------- +# PersistentVolume (hostPath on mist) +# -------------------------------------------------- +apiVersion: v1 +kind: PersistentVolume +metadata: + name: postgres-${ENV}-pv +spec: + capacity: + storage: 20Gi + accessModes: + - ReadWriteOnce + persistentVolumeReclaimPolicy: Retain + storageClassName: local-storage + hostPath: + path: /home/kimjunte/k8s_storage/postgres/${PG_VOLUME} + +--- +# -------------------------------------------------- +# PersistentVolumeClaim +# -------------------------------------------------- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: postgres-${ENV}-pvc + namespace: ${NAMESPACE} +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 20Gi + storageClassName: local-storage + +--- +# -------------------------------------------------- +# PostgreSQL Deployment +# -------------------------------------------------- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgres-${ENV} + namespace: ${NAMESPACE} +spec: + replicas: 1 + selector: + matchLabels: + app: postgres + env: ${ENV} + template: + metadata: + labels: + app: postgres + env: ${ENV} + spec: + containers: + - name: postgres + image: postgres:16 + ports: + - containerPort: 5432 + envFrom: + - secretRef: + name: postgres-${ENV} + volumeMounts: + - name: postgres-data + mountPath: /var/lib/postgresql/data + volumes: + - name: postgres-data + persistentVolumeClaim: + claimName: postgres-${ENV}-pvc + +--- +# -------------------------------------------------- +# PostgreSQL Service (internal) +# -------------------------------------------------- +apiVersion: v1 +kind: Service +metadata: + name: postgres-${ENV} + namespace: ${NAMESPACE} +spec: + type: ClusterIP + selector: + app: postgres + env: ${ENV} + ports: + - port: 5432 + targetPort: 5432 From f02c51870c9e8b01a54a80442342ade96f728ce9 Mon Sep 17 00:00:00 2001 From: Jun-te Kim Date: Wed, 21 Jan 2026 21:41:33 +0000 Subject: [PATCH 2/5] readded postgres deployment --- .github/workflows/stripe-to-invoice.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.github/workflows/stripe-to-invoice.yml b/.github/workflows/stripe-to-invoice.yml index b2ac901..3811b7c 100644 --- a/.github/workflows/stripe-to-invoice.yml +++ b/.github/workflows/stripe-to-invoice.yml @@ -155,9 +155,11 @@ jobs: if [[ "$ENV" == "prod" ]]; then USER="$PROD_POSTGRES_USER" PASS="$PROD_POSTGRES_PASSWORD" + RUNTIME_SECRET=postgres-prod else USER="$DEV_POSTGRES_USER" PASS="$DEV_POSTGRES_PASSWORD" + RUNTIME_SECRET=postgres-dev fi DATABASE_URL="postgres://${USER}:${PASS}@${POSTGRES_HOST}:5432/${POSTGRES_DB}?sslmode=disable" @@ -247,6 +249,21 @@ jobs: echo "POSTGRES_DB=stripe_invoice" >> $GITHUB_ENV fi + - name: Wait for Postgres TCP + run: | + set -e + for i in {1..30}; do + if nc -z "$POSTGRES_HOST" 5432; then + echo "Postgres is accepting connections" + exit 0 + fi + echo "Waiting for Postgres ($i/30)..." + sleep 5 + done + echo "Postgres never became ready" + exit 1 + + - name: Run migrations run: | set -e From 5d0ecd69cf2ba7e305c54b52b0152a5da03a354a Mon Sep 17 00:00:00 2001 From: Jun-te Kim Date: Wed, 21 Jan 2026 21:47:56 +0000 Subject: [PATCH 3/5] install --- .github/workflows/stripe-to-invoice.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/stripe-to-invoice.yml b/.github/workflows/stripe-to-invoice.yml index 3811b7c..68ba6ea 100644 --- a/.github/workflows/stripe-to-invoice.yml +++ b/.github/workflows/stripe-to-invoice.yml @@ -237,6 +237,11 @@ jobs: - name: Install Atlas uses: ariga/setup-atlas@v0 + - name: Install netcat + run: | + sudo apt-get update + sudo apt-get install -y netcat-openbsd + - name: Decide environment run: | if [[ "$GITHUB_REF" == refs/heads/main || "$GITHUB_REF" == refs/tags/* || "$GITHUB_REF" == refs/heads/release/* ]]; then From 34e982cd417fb088790433f0ced4fdbe3c83da4c Mon Sep 17 00:00:00 2001 From: Jun-te Kim Date: Wed, 21 Jan 2026 22:10:13 +0000 Subject: [PATCH 4/5] push --- stripe_to_invoice/deployment/secrets/stripe-secrets.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/stripe_to_invoice/deployment/secrets/stripe-secrets.yaml b/stripe_to_invoice/deployment/secrets/stripe-secrets.yaml index d213c85..17a258c 100644 --- a/stripe_to_invoice/deployment/secrets/stripe-secrets.yaml +++ b/stripe_to_invoice/deployment/secrets/stripe-secrets.yaml @@ -17,3 +17,4 @@ stringData: XERO_CLIENT_SECRET: ${XERO_CLIENT_SECRET} XERO_REDIRECT_URI: ${XERO_REDIRECT_URI} STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET} + From 6a305a30dc0fa3aa6d2d670687e18edef3e8ff8e Mon Sep 17 00:00:00 2001 From: Jun-te Kim Date: Wed, 21 Jan 2026 22:28:59 +0000 Subject: [PATCH 5/5] env variables set --- .github/workflows/stripe-to-invoice.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/stripe-to-invoice.yml b/.github/workflows/stripe-to-invoice.yml index 68ba6ea..1a68d10 100644 --- a/.github/workflows/stripe-to-invoice.yml +++ b/.github/workflows/stripe-to-invoice.yml @@ -166,9 +166,13 @@ jobs: kubectl create secret generic $RUNTIME_SECRET \ --namespace $NAMESPACE \ + --from-literal=POSTGRES_USER="$USER" \ + --from-literal=POSTGRES_PASSWORD="$PASS" \ + --from-literal=POSTGRES_DB="$POSTGRES_DB" \ --from-literal=DATABASE_URL="$DATABASE_URL" \ --dry-run=client -o yaml | kubectl apply -f - + - name: Apply app secrets run: | set -e