Skip to content

Commit f300576

Browse files
authored
Added job for cleaning up key value rows (#718)
ref https://linear.app/ghost/issue/PROD-685 We can now use `yarn run cleanup-db` to cleanup expired rows from the key_value table locally. This image will also be used as a scheduled Cloud Run Job and will be updated on changes in the cleanup-expired-key-value-records directory
1 parent d9445b2 commit f300576

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

.github/workflows/cicd.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,55 @@ jobs:
386386
status: ${{ job.status }}
387387
env:
388388
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
389+
390+
build-cleanup-job:
391+
name: Build & Push cleanup job
392+
runs-on: ubuntu-latest
393+
if: github.ref == 'refs/heads/main'
394+
395+
steps:
396+
- name: Checkout
397+
uses: actions/checkout@v4
398+
399+
- name: Check for changes in cleanup job
400+
id: changes
401+
uses: dorny/paths-filter@v3
402+
with:
403+
filters: |
404+
cleanup:
405+
- 'jobs/cleanup-expired-key-value-records/**'
406+
407+
- name: Authenticate with GCP
408+
if: steps.changes.outputs.cleanup == 'true'
409+
id: gcp-auth
410+
uses: google-github-actions/auth@v2
411+
with:
412+
token_format: access_token
413+
workload_identity_provider: projects/687476608778/locations/global/workloadIdentityPools/github-oidc-activitypub/providers/github-provider-activitypub
414+
service_account: [email protected]
415+
416+
- name: Login to GCP Artifact Registry
417+
if: steps.changes.outputs.cleanup == 'true'
418+
uses: docker/login-action@v3
419+
with:
420+
registry: europe-docker.pkg.dev
421+
username: oauth2accesstoken
422+
password: ${{ steps.gcp-auth.outputs.access_token }}
423+
424+
- name: Docker metadata
425+
if: steps.changes.outputs.cleanup == 'true'
426+
id: cleanup-meta
427+
uses: docker/metadata-action@v5
428+
with:
429+
images: europe-docker.pkg.dev/ghost-activitypub/activitypub/cleanup-expired-key-value-records
430+
tags: |
431+
type=sha
432+
type=edge,branch=main
433+
434+
- name: Build & Push cleanup job image
435+
if: steps.changes.outputs.cleanup == 'true'
436+
uses: docker/build-push-action@v6
437+
with:
438+
context: jobs/cleanup-expired-key-value-records
439+
push: true
440+
tags: ${{ steps.cleanup-meta.outputs.tags }}

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ services:
5555
mysql:
5656
condition: service_healthy
5757

58+
cleanup-expired-key-value-records:
59+
build: jobs/cleanup-expired-key-value-records
60+
environment:
61+
- MYSQL_USER=ghost
62+
- MYSQL_PASSWORD=password
63+
- MYSQL_HOST=mysql
64+
- MYSQL_PORT=3306
65+
- MYSQL_DATABASE=activitypub
66+
5867
scripts:
5968
build: scripts
6069
depends_on:
@@ -157,6 +166,15 @@ services:
157166
mysql-testing:
158167
condition: service_healthy
159168

169+
cleanup-expired-key-value-records-testing:
170+
build: jobs/cleanup-expired-key-value-records
171+
environment:
172+
- MYSQL_USER=ghost
173+
- MYSQL_PASSWORD=password
174+
- MYSQL_HOST=mysql-testing
175+
- MYSQL_PORT=3306
176+
- MYSQL_DATABASE=activitypub
177+
160178
cucumber-tests:
161179
networks:
162180
- test_network
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM mysql:8.3
2+
3+
COPY cleanup-expired-key-value-records /bin
4+
5+
ENTRYPOINT ["cleanup-expired-key-value-records"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
set -e
3+
4+
mysql \
5+
--host="$MYSQL_HOST" \
6+
--port="$MYSQL_PORT" \
7+
--user="$MYSQL_USER" \
8+
--password="$MYSQL_PASSWORD" \
9+
--database="$MYSQL_DATABASE" \
10+
-e "DELETE FROM key_value WHERE expires IS NOT NULL AND expires < UTC_TIMESTAMP();"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"fix": "docker compose down --rmi all activitypub activitypub-testing cucumber-tests nginx",
1616
"logs": "docker compose logs activitypub -f",
1717
"wipe-db": "docker compose down -v mysql && docker compose up -d activitypub nginx && echo 'ActivityPub DB wiped. You must restart Ghost'",
18+
"cleanup-db": "docker compose run --rm cleanup-expired-key-value-records cleanup-expired-key-value-records-testing",
1819
"migration": "docker compose run migrate create",
1920
"migrate": "docker compose up migrate",
2021
"test:cucumber": "./docker/cucumber-tests",

0 commit comments

Comments
 (0)