166 lines
5.8 KiB
YAML
166 lines
5.8 KiB
YAML
name: Build & Deploy stripe-to-invoice (with DB secrets)
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
- feature/**
|
|
- release/**
|
|
tags:
|
|
- "*"
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
# --------------------------------------------------
|
|
# BUILD IMAGE
|
|
# --------------------------------------------------
|
|
build:
|
|
runs-on: ubuntu-22.04
|
|
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Inject slug variables
|
|
uses: rlespinasse/github-slug-action@v4
|
|
|
|
- name: Login to Docker Hub
|
|
uses: docker/login-action@v3
|
|
with:
|
|
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
|
password: ${{ secrets.DOCKER_HUB_TOKEN }}
|
|
|
|
- name: Build image
|
|
run: |
|
|
docker build \
|
|
-f stripe_to_invoice/deployment/Dockerfile \
|
|
-t docker.io/kimjunte/stripe_to_invoice:$GITHUB_REF_SLUG \
|
|
.
|
|
|
|
- name: Push image
|
|
run: |
|
|
docker push docker.io/kimjunte/stripe_to_invoice:$GITHUB_REF_SLUG
|
|
|
|
# --------------------------------------------------
|
|
# APPLY DB SECRETS
|
|
# --------------------------------------------------
|
|
secrets:
|
|
name: Apply runtime DB secret
|
|
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 "RUNTIME_SECRET=postgres-prod" >> $GITHUB_ENV
|
|
echo "POSTGRES_HOST=postgres-prod.default.svc.cluster.local" >> $GITHUB_ENV
|
|
echo "POSTGRES_DB=stripe_invoice" >> $GITHUB_ENV
|
|
else
|
|
echo "ENV=dev" >> $GITHUB_ENV
|
|
echo "NAMESPACE=dev" >> $GITHUB_ENV
|
|
echo "RUNTIME_SECRET=postgres-dev" >> $GITHUB_ENV
|
|
echo "POSTGRES_HOST=postgres-dev.dev.svc.cluster.local" >> $GITHUB_ENV
|
|
echo "POSTGRES_DB=stripe_invoice_dev" >> $GITHUB_ENV
|
|
fi
|
|
|
|
- name: Load DB creds from db/.env and apply secret
|
|
run: |
|
|
set -a
|
|
source db/.env
|
|
set +a
|
|
|
|
if [[ "$ENV" == "prod" ]]; then
|
|
USER="$PROD_POSTGRES_USER"
|
|
PASS="$PROD_POSTGRES_PASSWORD"
|
|
else
|
|
USER="$DEV_POSTGRES_USER"
|
|
PASS="$DEV_POSTGRES_PASSWORD"
|
|
fi
|
|
|
|
DATABASE_URL="postgres://${USER}:${PASS}@${POSTGRES_HOST}:5432/${POSTGRES_DB}?sslmode=disable"
|
|
|
|
kubectl create secret generic $RUNTIME_SECRET \
|
|
--namespace $NAMESPACE \
|
|
--from-literal=DATABASE_URL="$DATABASE_URL" \
|
|
--dry-run=client -o yaml | kubectl apply -f -
|
|
|
|
# --------------------------------------------------
|
|
# DEPLOY APP
|
|
# --------------------------------------------------
|
|
deploy:
|
|
runs-on: mealcraft-runners
|
|
needs:
|
|
- build
|
|
- secrets
|
|
|
|
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
|
|
NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)
|
|
|
|
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 --namespace="$NAMESPACE"
|
|
kubectl config use-context runner-context
|
|
|
|
- name: Inject slug variables
|
|
uses: rlespinasse/github-slug-action@v4
|
|
|
|
- name: Decide environment
|
|
run: |
|
|
if [[ "$GITHUB_REF" == refs/heads/release/* || "$GITHUB_REF" == refs/tags/* ]]; then
|
|
echo "NAMESPACE=default" >> $GITHUB_ENV
|
|
echo "DB_ENV=prod" >> $GITHUB_ENV
|
|
echo "HOSTNAME=stripe-to-invoice.juntekim.com" >> $GITHUB_ENV
|
|
else
|
|
echo "NAMESPACE=dev" >> $GITHUB_ENV
|
|
echo "DB_ENV=dev" >> $GITHUB_ENV
|
|
echo "HOSTNAME=stripe-to-invoice.dev.juntekim.com" >> $GITHUB_ENV
|
|
fi
|
|
|
|
- name: Deploy application
|
|
run: |
|
|
export IMAGE="docker.io/kimjunte/stripe_to_invoice:$GITHUB_REF_SLUG"
|
|
export NAMESPACE DB_ENV HOSTNAME
|
|
envsubst < stripe_to_invoice/deployment/deployment.yaml | kubectl apply -f -
|