From 68436545bedf53b4fae8b264ae070cace2f7add6 Mon Sep 17 00:00:00 2001 From: Vojtech Mares Date: Thu, 27 Jun 2024 21:01:12 +0200 Subject: [PATCH] feat(deploy): add job to run migrations --- .gitlab-ci.yml | 1 + deploy/templates/migration-job.yaml | 36 +++++++++++++++++++++++++++++ deploy/values.staging.yaml | 5 ++++ deploy/values.yaml | 14 +++++++++++ 4 files changed, 56 insertions(+) create mode 100644 deploy/templates/migration-job.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f26e7ee..b73d37d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -78,6 +78,7 @@ deploy:staging: api ./deploy \ --values deploy/values.staging.yaml \ --set image.tag=$TAG \ + --set migrations.image.tag=$TAG \ --set imageCredentials.username=$REGISTRY_USERNAME \ --set imageCredentials.password=$REGISTRY_PASSWORD \ --set config.database.url=$DATABASE_URL diff --git a/deploy/templates/migration-job.yaml b/deploy/templates/migration-job.yaml new file mode 100644 index 0000000..c0db104 --- /dev/null +++ b/deploy/templates/migration-job.yaml @@ -0,0 +1,36 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: {{ .Release.Name }}-migrations-{{ now | unixEpoch }} + labels: + app.kubernetes.io/name: {{ .Chart.Name }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/component: "database-migrations" + batch.kubernetes.io/job-name: {{ .Release.Name }}-migrations-{{ now | unixEpoch }} +spec: + ttlSecondsAfterFinished: 604800 # 1 week in seconds + template: + metadata: + labels: + app.kubernetes.io/name: {{ .Chart.Name }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/component: "database-migrations" + batch.kubernetes.io/job-name: {{ .Release.Name }}-migrations-{{ now | unixEpoch }} + spec: + containers: + - name: migrations + image: "{{ .Values.migrations.image.repository }}:{{ .Values.migrations.image.tag }}" + command: ["/usr/local/bin/migrate"] + args: ["-path", "/srv/migrations/", "-database", "$MIGRATE_DATABASE_URL", "up"] + env: + - name: MIGRATE_DATABASE_URL + valueFrom: + secretKeyRef: + name: {{ .Release.Name }} + key: "database_url.txt" + {{- with .Values.migrations.resources }} + resources: + {{- toYaml . | nindent 12 }} + {{- end }} + restartPolicy: Never + backoffLimit: 0 diff --git a/deploy/values.staging.yaml b/deploy/values.staging.yaml index 2fa7bba..594be36 100644 --- a/deploy/values.staging.yaml +++ b/deploy/values.staging.yaml @@ -9,3 +9,8 @@ replicas: 2 image: pullSecrets: - name: mareshq-gitlab-registry + +migrations: + image: + pullSecrets: + - name: mareshq-gitlab-registry diff --git a/deploy/values.yaml b/deploy/values.yaml index d445df3..f5afb52 100644 --- a/deploy/values.yaml +++ b/deploy/values.yaml @@ -31,3 +31,17 @@ config: port: 8080 database: url: + +migrations: + image: + pullSecrets: [] + repository: registry.mareshq.com/hq/yggdrasil/migrations + tag: latest + + resources: + limits: + cpu: 250m + memory: 512Mi + requests: + cpu: 100m + memory: 128Mi