Repository navigation
publish-discord-backup #7281
This file contains hidden or 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: publish-discord-backup | |
| on: | |
| schedule: | |
| - cron: "*/15 * * * *" | |
| - cron: "17 6 * * *" | |
| workflow_dispatch: | |
| inputs: | |
| reconcile_cloud: | |
| description: Reconcile the configured private identity plan against Discord before publication | |
| type: boolean | |
| default: false | |
| permissions: | |
| contents: read | |
| concurrency: | |
| group: discord-backup-repo | |
| cancel-in-progress: false | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 60 | |
| steps: | |
| - name: Checkout discrawl | |
| uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | |
| - name: Setup Go | |
| uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 | |
| with: | |
| go-version-file: go.mod | |
| cache: true | |
| - name: Restore Discord DB cache | |
| id: restore-discord-db | |
| uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 | |
| with: | |
| path: | | |
| .discrawl-ci/discrawl.db | |
| .discrawl-ci/discrawl.db-shm | |
| .discrawl-ci/discrawl.db-wal | |
| key: discrawl-discord-db-${{ runner.os }}-${{ github.ref_name }}-${{ github.run_id }}-${{ github.run_attempt }} | |
| restore-keys: | | |
| discrawl-discord-db-${{ runner.os }}-${{ github.ref_name }}- | |
| discrawl-discord-db-${{ runner.os }}- | |
| - name: Configure Git identity | |
| run: | | |
| git config --global user.name "discrawl publisher" | |
| git config --global user.email "discrawl-publisher@users.noreply.github.com" | |
| - name: Sync and publish Discord archive | |
| id: git-publish | |
| env: | |
| DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }} | |
| DISCORD_BACKUP_TOKEN: ${{ secrets.DISCORD_BACKUP_TOKEN }} | |
| DISCRAWL_BACKUP_REPOSITORY: ${{ secrets.DISCRAWL_BACKUP_REPOSITORY }} | |
| DISCRAWL_GUILD_ID: ${{ secrets.DISCRAWL_GUILD_ID }} | |
| RECONCILE_CLOUD: ${{ github.event_name == 'workflow_dispatch' && inputs.reconcile_cloud && '1' || '0' }} | |
| DISCRAWL_CLOUD_RECONCILE_PLAN: ${{ github.event_name == 'workflow_dispatch' && inputs.reconcile_cloud && secrets.DISCRAWL_CLOUD_RECONCILE_PLAN || '' }} | |
| DISCRAWL_CLOUD_ARCHIVE: ${{ secrets.DISCRAWL_CLOUD_ARCHIVE }} | |
| CONFIG: ${{ runner.temp }}/discrawl/config.toml | |
| DB: ${{ github.workspace }}/.discrawl-ci/discrawl.db | |
| BACKUP_REPO: ${{ runner.temp }}/discord-backup | |
| run: | | |
| if [ "$RECONCILE_CLOUD" = "1" ] && [ "$GITHUB_REF" != "refs/heads/main" ]; then | |
| echo "::error::Source reconciliation requires the reviewed main branch." | |
| exit 1 | |
| fi | |
| if [ -z "${DISCRAWL_BACKUP_REPOSITORY:-}" ]; then | |
| echo "::notice title=Backup publish skipped::Configure DISCRAWL_BACKUP_REPOSITORY as owner/repo to enable archive publishing." | |
| exit 0 | |
| fi | |
| if [ -z "${DISCORD_BOT_TOKEN:-}" ]; then | |
| echo "::error title=Missing secret::Configure DISCORD_BOT_TOKEN in the discrawl repo secrets." | |
| exit 1 | |
| fi | |
| if [ -z "${DISCORD_BACKUP_TOKEN:-}" ]; then | |
| echo "::error title=Missing secret::Configure DISCORD_BACKUP_TOKEN with write access to the backup repository." | |
| exit 1 | |
| fi | |
| if [ -z "${DISCRAWL_GUILD_ID:-}" ]; then | |
| echo "::error title=Missing secret::Configure DISCRAWL_GUILD_ID with the Discord guild to publish." | |
| exit 1 | |
| fi | |
| GIT_CREDENTIALS="${RUNNER_TEMP}/discord-backup-git-credentials" | |
| printf 'https://x-access-token:%s@github.com\n' "$DISCORD_BACKUP_TOKEN" > "$GIT_CREDENTIALS" | |
| chmod 0600 "$GIT_CREDENTIALS" | |
| git config --global credential.helper "store --file=${GIT_CREDENTIALS}" | |
| trap 'rm -f "$GIT_CREDENTIALS"' EXIT | |
| BACKUP_REMOTE="https://github.com/${DISCRAWL_BACKUP_REPOSITORY}.git" | |
| mkdir -p "$(dirname "$CONFIG")" | |
| mkdir -p "$(dirname "$DB")" | |
| git clone --depth=1 --single-branch --branch main --no-tags \ | |
| -- "$BACKUP_REMOTE" "$BACKUP_REPO" | |
| git -C "$BACKUP_REPO" config --add remote.origin.fetch '^refs/tags/*' | |
| go run ./cmd/discrawl --config "$CONFIG" init --db "$DB" --guild "$DISCRAWL_GUILD_ID" | |
| if [ -f "$BACKUP_REPO/manifest.json" ]; then | |
| if [ -s "$DB" ]; then | |
| echo "Restored Discord DB cache at $DB; skipping pre-sync snapshot import." | |
| else | |
| echo "Discord DB cache missing; importing latest published snapshot before latest-only sync." | |
| go run ./cmd/discrawl --config "$CONFIG" update --repo "$BACKUP_REPO" --remote "$BACKUP_REMOTE" | |
| fi | |
| fi | |
| sync_args=(--guild "$DISCRAWL_GUILD_ID" --skip-members --latest-only) | |
| if [ "${{ github.event.schedule }}" = "17 6 * * *" ]; then | |
| sync_args=(--guild "$DISCRAWL_GUILD_ID" --with-members --latest-only) | |
| echo "Refreshing Discord member roles and profiles." | |
| fi | |
| go run ./cmd/discrawl --config "$CONFIG" sync "${sync_args[@]}" | |
| if [ "$RECONCILE_CLOUD" = "1" ]; then | |
| go run scripts/reconcile_cloud_source.go | |
| fi | |
| git -C "$BACKUP_REPO" pull --ff-only origin main | |
| SOURCE_REVISION="$(git rev-parse HEAD)" | |
| if [ "$SOURCE_REVISION" != "$GITHUB_SHA" ]; then | |
| echo "::error::Checked-out source revision does not match GITHUB_SHA." | |
| exit 1 | |
| fi | |
| DISCRAWL_PRODUCER_REPOSITORY="$GITHUB_REPOSITORY" \ | |
| DISCRAWL_PRODUCER_REVISION="$SOURCE_REVISION" \ | |
| DISCRAWL_PRODUCER_RUN_ID="$GITHUB_RUN_ID" \ | |
| DISCRAWL_PRODUCER_RUN_ATTEMPT="$GITHUB_RUN_ATTEMPT" \ | |
| go run ./cmd/discrawl --config "$CONFIG" publish \ | |
| --repo "$BACKUP_REPO" \ | |
| --remote "$BACKUP_REMOTE" \ | |
| --readme "$BACKUP_REPO/README.md" \ | |
| --message "sync: discord archive" \ | |
| --push | |
| echo "published=true" >> "$GITHUB_OUTPUT" | |
| - name: Save Discord DB cache | |
| uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 | |
| with: | |
| path: | | |
| .discrawl-ci/discrawl.db | |
| .discrawl-ci/discrawl.db-shm | |
| .discrawl-ci/discrawl.db-wal | |
| key: ${{ steps.restore-discord-db.outputs.cache-primary-key }} | |
| - name: Setup Node for cloud publication | |
| if: ${{ vars.DISCRAWL_CLOUD_PUBLISH_ENABLED == '1' && steps.git-publish.outputs.published == 'true' }} | |
| uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 | |
| with: | |
| node-version: '24' | |
| - name: Publish the same current state to D1 and R2 | |
| if: ${{ vars.DISCRAWL_CLOUD_PUBLISH_ENABLED == '1' && steps.git-publish.outputs.published == 'true' }} | |
| timeout-minutes: 40 | |
| env: | |
| DISCRAWL_CLOUD_ENDPOINT: ${{ secrets.DISCRAWL_CLOUD_ENDPOINT }} | |
| DISCRAWL_CLOUD_ARCHIVE: ${{ secrets.DISCRAWL_CLOUD_ARCHIVE }} | |
| DISCRAWL_CLOUD_AUTH_GITHUB_TOKEN: ${{ secrets.DISCORD_BACKUP_TOKEN }} | |
| DISCRAWL_CLOUD_ACCESS_CLIENT_ID: ${{ secrets.DISCRAWL_CLOUD_ACCESS_CLIENT_ID }} | |
| DISCRAWL_CLOUD_ACCESS_CLIENT_SECRET: ${{ secrets.DISCRAWL_CLOUD_ACCESS_CLIENT_SECRET }} | |
| DISCRAWL_CLOUD_SNAPSHOT: ${{ runner.temp }}/discrawl/cloud.db | |
| DISCRAWL_CLOUD_SOURCE_DB: ${{ github.workspace }}/.discrawl-ci/discrawl.db | |
| run: | | |
| umask 077 | |
| trap 'rm -f "$DISCRAWL_CLOUD_SNAPSHOT"' EXIT | |
| DISCRAWL_CLOUD_SOURCE_COMMIT="$(git -C "$RUNNER_TEMP/discord-backup" rev-parse HEAD)" | |
| export DISCRAWL_CLOUD_SOURCE_COMMIT | |
| go run ./cmd/discrawl --config "$RUNNER_TEMP/discrawl/config.toml" cloud publish \ | |
| --export-only "$DISCRAWL_CLOUD_SNAPSHOT" > /dev/null | |
| node scripts/publish-cloud.mjs |