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