Repository navigation
Expand file tree
/
Copy pathgithub-actions-drift.yml
More file actions
85 lines (81 loc) · 3.36 KB
/
Copy pathgithub-actions-drift.yml
File metadata and controls
85 lines (81 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
name: ClickHouse schema drift
on:
schedule:
- cron: '17 6 * * *'
pull_request:
permissions:
contents: read
jobs:
drift:
# Fork pull requests cannot access repository secrets. Never use
# pull_request_target to expose database credentials to untrusted code.
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
env:
# Set to a pinned ch-migrate-cli requirement with --json support, such as
# ch-migrate-cli==0.8.0 (the first release with it). There is no
# fallback to an older release without the required command.
CH_MIGRATE_PACKAGE: ${{ vars.CH_MIGRATE_PACKAGE }}
CH_MIGRATE_PYTHON: '3.12'
# These name a config.yaml environment and its committed snapshot folder.
CH_MIGRATE_ENV: ${{ vars.CH_MIGRATE_ENV }}
CH_MIGRATE_SNAPSHOT_DIR: ${{ vars.CH_MIGRATE_SNAPSHOT_DIR }}
UV_TOOL_DIR: ${{ github.workspace }}/.ch-migrate-tools
UV_TOOL_BIN_DIR: ${{ github.workspace }}/.ch-migrate-bin
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
- uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
with:
python-version: '3.12'
- name: Install ch-migrate
id: install
run: |
: "${CH_MIGRATE_PACKAGE:?Set a package requirement with JSON support}"
: "${CH_MIGRATE_ENV:?Set the config.yaml environment name}"
: "${CH_MIGRATE_SNAPSHOT_DIR:?Set the committed snapshot path}"
uv tool install --python "$CH_MIGRATE_PYTHON" "$CH_MIGRATE_PACKAGE"
- name: Write scoped database credential
id: credentials
env:
# Password for config.yaml's migration_user in CH_MIGRATE_ENV.
# Use a dedicated schema-inspection account, not an admin credential.
DRIFT_MIGRATION_PASSWORD: ${{ secrets.DRIFT_MIGRATION_PASSWORD }}
run: |
umask 077
python - <<'PY'
import json
import os
from pathlib import Path
password = os.environ['DRIFT_MIGRATION_PASSWORD']
if not password:
raise SystemExit('DRIFT_MIGRATION_PASSWORD is not configured')
key = 'CH_' + os.environ['CH_MIGRATE_ENV'].upper() + '_MIGRATION_PASSWORD'
Path('.env.local').write_text(key + '=' + json.dumps(password) + '\n')
PY
- name: Compare committed schema with live schema
id: compare
continue-on-error: true
run: |
"$UV_TOOL_BIN_DIR/ch-migrate" diff "$CH_MIGRATE_ENV" \
--snapshot-dir "$CH_MIGRATE_SNAPSHOT_DIR" --json > drift.json
- name: Preserve drift report even when comparison fails
if: always()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: schema-drift
path: drift.json
if-no-files-found: warn
- name: Fail on drift or comparison errors
id: gate
if: always()
env:
DRIFT_OUTCOME: ${{ steps.compare.outcome }}
run: |
# compare exits 1 on drift and 2 on errors; neither is a clean result.
test "$DRIFT_OUTCOME" = success
- name: Remove the temporary credential file
id: cleanup
if: always()
run: rm -f .env.local