gitea/manifests/bin/deploy.sh

77 lines
2.4 KiB
Bash
Executable File

#!/bin/bash -e
set -o pipefail
function kapply() {
for f in "$@"; do
kubectl apply -f \
<(envsubst "$(env | xargs printf '$%s ')" < "manifests/$f")
done
}
function kcreatesec() {
kubectl create secret generic --save-config --dry-run=client -oyaml "$@" | kubectl apply -f-
}
function kcreatecm() {
kubectl create configmap --dry-run=client -oyaml "$@" | kubectl apply -f-
}
function kgseckey() {
local sec="$1"; shift
local key="$1"; shift
kubectl get secret "$sec" -o jsonpath="{.data.$key}" | base64 -d
}
function kgcmkey() {
local cm="$1"; shift
local key="$1"; shift
kubectl get configmap "$cm" -o jsonpath="{.data.$key}"
}
kapply common/db.yaml
export REDIS_HOST=redis
export REDIS_DB=0
export REDIS_PORT=6379
export POSTGRES_HOST; POSTGRES_HOST="$(kgseckey postgres-app host)"
export POSTGRES_PORT; POSTGRES_PORT="$(kgseckey postgres-app port)"
export POSTGRES_DB; POSTGRES_DB="$(kgseckey postgres-app dbname)"
export POSTGRES_USER; POSTGRES_USER="$(kgseckey postgres-app user)"
export POSTGRES_PASSWORD; POSTGRES_PASSWORD="$(kgseckey postgres-app password)"
GITEA_USERNAME="$(kgseckey gitea-admin username || echo gitea)"
GITEA_PASSWORD="$(kgseckey gitea-admin password || openssl rand -hex 32)"
kcreatesec gitea-admin \
--from-literal=email="gitea@$BASE_URL" \
--from-literal=username="$GITEA_USERNAME" \
--from-literal=password="$GITEA_PASSWORD"
kcreatesec gitea \
--from-literal=secret_key="$(kgseckey gitea secret_key || openssl rand -hex 32)" \
--from-literal=internal_token="$(kgseckey gitea internal_token || openssl rand -hex 32)"
kcreatecm gitea \
--from-file=app.ini=<(envsubst "$(env | xargs printf '$%s ')" < config/app.ini)
kapply common/job.yaml \
common/redis.yaml \
common/app.yaml
kubectl rollout restart statefulset app
kubectl wait --timeout=5m --for=condition=complete job/createadminuser
kcreatesec runner \
--from-literal=token="$(kgseckey runner token || kubectl exec app-0 -- gitea actions generate-runner-token)"
kcreatesec renovate \
--from-literal=token="$(kgseckey renovate token || kubectl exec app-0 -- gitea admin user generate-access-token --username "$GITEA_USERNAME" --token-name RENOVATE --scopes 'write:repository,read:user,write:issue,read:organization' | grep -o '[a-f0-9]\+$')"
kapply common/runner.yaml common/renovate.yaml
kubectl rollout restart statefulset runner