Alembic consistency check (comment) #230
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Alembic consistency check (comment) | |
on: | |
workflow_run: | |
workflows: [Alembic consistency check] | |
types: [completed] | |
jobs: | |
comment: | |
runs-on: ubuntu-latest | |
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' | |
steps: | |
# taken from https://securitylab.github.com/research/github-actions-preventing-pwn-requests | |
# hopefully this can be improved at some point; see this issue for details: | |
# https://github.com/actions/download-artifact/issues/60 | |
- name: Download artifact | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{ github.event.workflow_run.id }}, | |
}); | |
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === 'migration-sql-data'); | |
const download = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
const fs = require('fs'); | |
fs.writeFileSync('/tmp/migration-sql-data.zip', Buffer.from(download.data)); | |
- name: Unpack artifact | |
run: | | |
mkdir /tmp/migration-sql-data | |
cd /tmp/migration-sql-data | |
unzip /tmp/migration-sql-data.zip | |
- name: Get data from artifact | |
id: get-data | |
run: | | |
echo "pull_id=$(</tmp/migration-sql-data/pull-request-id)" >> $GITHUB_OUTPUT | |
if [ -f /tmp/migration-sql-data/upgrade.sql ]; then | |
sql_command=$(</tmp/migration-sql-data/upgrade.sql) | |
if [ ! -z "$sql_command" ] ; then | |
echo "sql=$sql_command" >> $GITHUB_OUTPUT | |
fi | |
fi | |
- name: Find previous comment | |
uses: peter-evans/find-comment@v3 | |
id: find-comment | |
with: | |
issue-number: ${{ steps.get-data.outputs.pull_id }} | |
body-includes: This PR contains database changes. | |
comment-author: github-actions[bot] | |
- name: Create comment | |
uses: peter-evans/create-or-update-comment@v4 | |
if: steps.get-data.outputs.sql | |
with: | |
issue-number: ${{ steps.get-data.outputs.pull_id }} | |
comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
edit-mode: replace | |
body: | | |
This PR contains database changes. Before merging it, make sure to apply the migration in production: | |
```sql | |
${{ steps.get-data.outputs.sql }} | |
``` | |
When reviewing the PR, make sure that the changes will not break the previously deployed | |
version, i.e. any new column needs to have a `server_default` or be nullable. | |
- name: Delete comment | |
uses: peter-evans/create-or-update-comment@v4 | |
if: steps.find-comment.outputs.comment-id && !steps.get-data.outputs.sql | |
with: | |
issue-number: ${{ steps.get-data.outputs.pull_id }} | |
comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
edit-mode: replace | |
body: | | |
~~This PR contains database changes.~~ |