diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index f46a5ab25..ece4b6a90 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -95,6 +95,8 @@ jobs: --helm-set gateway.listener.hostname=tim-api.${{ env.PREVIEW_NS}}.dev.greybox.chat --helm-set gateway.httpRoute.hostnames[0]=tim-api.${{ env.PREVIEW_NS}}.dev.greybox.chat --helm-set externalSecrets.secrets.planetscale.enabled=false + --helm-set databaseMigration.enabled=true + --helm-set databaseMigration.image.tag=${{ github.event.pull_request.head.sha }} --label environment=preview --label argocd.argoproj.io/instance=tim-api --sync-policy automated @@ -177,10 +179,10 @@ jobs: echo "Done." - uses: actions/checkout@v5 - - name: Run database migrations - uses: docker://amacneil/dbmate:2.28 - with: - args: --wait --url ${{ steps.credentials.outputs.DB_URL }} --schema-file tim-db/gen/schema/schema.sql --migrations-dir tim-db/migrations up + # - name: Run database migrations + # uses: docker://amacneil/dbmate:2.28 + # with: + # args: --wait --url ${{ steps.credentials.outputs.DB_URL }} --schema-file tim-db/gen/schema/schema.sql --migrations-dir tim-db/migrations up - uses: azure/setup-kubectl@v3 - uses: teleport-actions/setup@v1 diff --git a/charts/tim-api/templates/deployment.yaml b/charts/tim-api/templates/deployment.yaml index 4e29a60d4..0132c382a 100644 --- a/charts/tim-api/templates/deployment.yaml +++ b/charts/tim-api/templates/deployment.yaml @@ -26,6 +26,46 @@ spec: imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} + {{- $dbEnvVar := .Values.databaseMigration.databaseUrlEnvVar | default "TIM_API_DATABASE_URL" }} + {{- if .Values.databaseMigration.enabled }} + initContainers: + - name: dbmate-migrate + image: "{{ .Values.databaseMigration.image.repository }}:{{ .Values.databaseMigration.image.tag }}" + imagePullPolicy: {{ .Values.databaseMigration.image.pullPolicy }} + {{- if .Values.databaseMigration.command }} + command: + {{- toYaml .Values.databaseMigration.command | nindent 12 }} + {{- else }} + command: + - /bin/sh + - -c + - | + set -euo pipefail + DB_URL={{ printf "\"${%s:?%s must be set}\"" $dbEnvVar $dbEnvVar }} + SCHEMA_PATH="${SCHEMA_SQL_PATH:-/config/tim-db/gen/schema/schema.sql}" + MIGRATIONS_PATH="${MIGRATIONS_DIR:-/config/tim-db/migrations}" + echo "Running dbmate migrations from ${MIGRATIONS_PATH} ..." + dbmate --wait --url "${DB_URL}" --schema-file "${SCHEMA_PATH}" --migrations-dir "${MIGRATIONS_PATH}" up + {{- end }} + {{- if .Values.databaseMigration.args }} + args: + {{- toYaml .Values.databaseMigration.args | nindent 12 }} + {{- end }} + env: + - name: {{ $dbEnvVar }} + valueFrom: + secretKeyRef: + name: {{ .Values.databaseMigration.databaseUrlSecret.name }} + key: {{ .Values.databaseMigration.databaseUrlSecret.key }} + {{- range $key, $value := .Values.databaseMigration.extraEnv }} + - name: {{ $key }} + value: {{ $value | quote }} + {{- end }} + {{- with .Values.databaseMigration.resources }} + resources: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- end }} containers: - name: {{ include "tim-api.name" . }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" diff --git a/charts/tim-api/values.yaml b/charts/tim-api/values.yaml index 2babbe4a8..beb924a68 100644 --- a/charts/tim-api/values.yaml +++ b/charts/tim-api/values.yaml @@ -175,6 +175,24 @@ secrets: key: segmentApiKey envVarName: TIM_API_ANALYTICS_API_KEY +databaseMigration: + enabled: false + # Image built from tim-db/Dockerfile (inherits schema + migrations from repo) + image: + repository: ghcr.io/greybox-labs/tim-db + tag: "latest" + pullPolicy: IfNotPresent + # Secret that provides the database URL value exposed as TIM_API_DATABASE_URL + databaseUrlSecret: + name: database-url-with-pass + key: dbUrl + databaseUrlEnvVar: TIM_API_DATABASE_URL + # Optional explicit command/args; when empty a sane default runs dbmate up + command: [] + args: [] + extraEnv: {} + resources: {} + podAnnotations: {} # Service Account configuration diff --git a/scripts/just/docker.just b/scripts/just/docker.just index e38bfd776..39dc70fdb 100644 --- a/scripts/just/docker.just +++ b/scripts/just/docker.just @@ -42,13 +42,6 @@ USE_LATEST_TAG := env_var_or_default("USE_LATEST_TAG", AUTO_USE_LATEST) AUTO_USE_PR_TAG := if BUILD_CONTEXT == "pr" { if PR_NUMBER == "" { "false" } else { "true" } } else { "false" } USE_PR_TAG := env_var_or_default("USE_PR_TAG", AUTO_USE_PR_TAG) -# Helper to get discovered components (runs from project root) -[no-cd] -_get-components: - find . -mindepth 2 -maxdepth 2 -type f -name 'Dockerfile' -exec dirname {} \; \ - | sed 's#^\./##' \ - | LC_ALL=C sort -u - # Helper to get discovered component paths (runs from project root) [no-cd] _get-component-paths: @@ -347,7 +340,7 @@ push-summary: if test -s "$PUSHED_FILE"; then echo "### Images Successfully Pushed:" while IFS= read -r image; do - echo "- \`$image\`" + printf -- "- \`%s\`\n" "$image" done < "$PUSHED_FILE" echo "" echo "**Total images:** $(wc -l < "$PUSHED_FILE")" @@ -365,7 +358,7 @@ component-push-summary: if test -s "$PUSHED_FILE"; then echo "### Component Images Successfully Pushed:" while IFS= read -r image; do - echo "- \\`$image\\`" + printf -- "- \`%s\`\n" "$image" done < "$PUSHED_FILE" echo "" echo "**Total component images:** $(wc -l < "$PUSHED_FILE")" diff --git a/tim-db/Dockerfile b/tim-db/Dockerfile new file mode 100644 index 000000000..5baba7749 --- /dev/null +++ b/tim-db/Dockerfile @@ -0,0 +1,10 @@ +# syntax=docker/dockerfile:1-labs + +FROM amacneil/dbmate:2.28 + +ENV CONFIG_ROOT=/config +ENV SCHEMA_SQL_PATH=${CONFIG_ROOT}/tim-db/gen/schema/schema.sql +ENV SCHEMA_MERMAID_PATH=${CONFIG_ROOT}/tim-db/gen/schema/schema.mmd +ENV MIGRATIONS_DIR=${CONFIG_ROOT}/tim-db/migrations + +COPY --from=root --parents tim-db/gen/schema tim-db/migrations tim-db/queries tim-db/queries ${CONFIG_ROOT}