Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ jobs:
uses: actions/upload-pages-artifact@v3
with:
path: .vitepress/dist
# The default is 1 day: a fork PR preview may wait longer than that
# for a maintainer to approve the deploy
retention-days: 30
13 changes: 9 additions & 4 deletions .github/workflows/notify-telegram-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@ on:
types: [opened, reopened]
issues:
types: [opened, reopened]

permissions: {}

jobs:
build:
name: Notify Telegram
runs-on: ubuntu-latest
steps:
# The Telegram API sometimes resets the connection from the GitHub
# runners: a missed notification must not mark the PR or issue as failed
- name: send telegram message on PR opened
if: github.event.pull_request
uses: appleboy/telegram-action@master
continue-on-error: true
uses: appleboy/[email protected]
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
if: github.event.issue
message: |
${{ github.actor }} created PR ${{ github.event.pull_request.title }}
See changes: ${{ github.event.pull_request.html_url }}
- name: send telegram message on issue opened
if: github.event.issue
uses: appleboy/telegram-action@master
continue-on-error: true
uses: appleboy/[email protected]
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
if: github.event.issue
message: |
${{ github.actor }} created issue ${{ github.event.issue.title }}
See issue: ${{ github.event.issue.html_url }}
96 changes: 96 additions & 0 deletions .github/workflows/preview-cleanup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Delete the old PR previews from Cloudflare Pages. Each PR is published on
# its own branch (pr-<number>, see preview-deploy.yaml): only the newest
# deployment of each open PR is kept, everything else is deleted (older
# deployments of open PRs, closed PRs, previews of the old shared "preview"
# branch). It never runs the PR code, so pull_request_target is safe here.
name: preview-cleanup

on:
# A PR is closed or merged: its preview is no longer needed
pull_request_target:
types: [closed]
# A new preview is published: the previous ones of the same PR are obsolete
workflow_run:
workflows: [preview-deploy]
types: [completed]
# Catches up anything the events above missed
schedule:
- cron: "17 3 * * 0"
workflow_dispatch:
inputs:
dry-run:
description: Only list the deployments that would be deleted
type: boolean
default: false

permissions:
contents: read

# Never run two cleanups at the same time, but don't drop the queued one
concurrency:
group: preview-cleanup
cancel-in-progress: false

jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
pull-requests: read
env:
GH_TOKEN: ${{ github.token }}
CF_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CF_ACCOUNT: ${{ secrets.CLOUDFLARE_ACCOUNT }}
PROJECT: hack-gpon-preview
DRY_RUN: ${{ inputs.dry-run && 'true' || 'false' }}
steps:
- name: Delete old deployments
run: |
set -euo pipefail
api="https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT/pages/projects/$PROJECT"
cf() { curl -sS --fail-with-body -H "Authorization: Bearer $CF_TOKEN" "$@"; }

# The deployment served on the project's main URL can't be deleted
canonical=$(cf "$api" | jq -r '.result.canonical_deployment.id // empty')
open_prs=$(gh pr list --repo "$GITHUB_REPOSITORY" --state open --limit 1000 \
--json number --jq '[.[].number]')
echo "Open PRs: $open_prs"

# Collect every deployment first: deleting while paging shifts the pages
: > deployments.jsonl
page=1
while :; do
resp=$(cf "$api/deployments?per_page=25&page=$page")
jq -c '.result[]' <<<"$resp" >> deployments.jsonl
total=$(jq -r '.result_info.total_pages // 1' <<<"$resp")
(( page >= total )) && break
page=$((page + 1))
done
echo "Found $(wc -l < deployments.jsonl) deployments"

# Newest first: keep the first deployment seen for each open PR
jq -rs --arg canonical "$canonical" --argjson open "$open_prs" '
sort_by(.created_on) | reverse
| reduce .[] as $d ({seen: {}, delete: []};
($d.deployment_trigger.metadata.branch // "-") as $branch
| ([$branch | capture("^pr-(?<n>[0-9]+)$") | .n | tonumber][0]) as $pr
| if $d.id == $canonical then .
elif $pr != null and any($open[]; . == $pr) and (.seen[$branch] | not)
then .seen[$branch] = true
else .delete += ["\($d.id) \($branch) \($d.created_on)"]
end)
| .delete[]' deployments.jsonl > to-delete.txt

echo "Deleting $(wc -l < to-delete.txt) deployments"
failed=0
while read -r id branch created; do
echo "- $id ($branch, $created)"
[ "$DRY_RUN" = "true" ] && continue
# force: also delete deployments that still have a branch alias
cf -X DELETE "$api/deployments/$id?force=true" > /dev/null \
|| { echo "::warning::Failed to delete deployment $id"; failed=$((failed + 1)); }
done < to-delete.txt

if (( failed > 0 )); then
echo "::error::$failed deployments could not be deleted"
exit 1
fi
21 changes: 17 additions & 4 deletions .github/workflows/preview-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,33 @@ jobs:
[[ "$sha" == "$HEAD_SHA" ]] || { echo "PR #$number is not at $HEAD_SHA"; exit 1; }
echo "number=$number" >> "$GITHUB_OUTPUT"
- name: Extract site
run: mkdir dist && tar -xf github-pages/artifact.tar -C dist
run: |
if [ ! -f github-pages/artifact.tar ]; then
echo "::error::The built site has expired, re-run the preview-pr workflow of the PR"
exit 1
fi
mkdir dist && tar -xf github-pages/artifact.tar -C dist
# One branch per PR: its alias URL always shows the latest preview, and
# preview-cleanup.yaml can tell which deployments belong to which PR
- name: Publish to Cloudflare Pages
id: preview-pages
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT }}
command: pages deploy dist --project-name=hack-gpon-preview --branch=preview
command: >-
pages deploy dist --project-name=hack-gpon-preview
--branch=pr-${{ steps.pr.outputs.number }} --commit-hash=${{ env.HEAD_SHA }}
- name: Comment on the PR
env:
URL: ${{ steps.preview-pages.outputs.deployment-url }}
ALIAS_URL: ${{ steps.preview-pages.outputs.deployment-alias-url }}
run: |
gh pr comment "${{ steps.pr.outputs.number }}" --repo "$GITHUB_REPOSITORY" \
--body "Preview of the website obtained from the PR: $URL"
body="Preview of the website obtained from the PR at $HEAD_SHA: $URL"
if [ -n "$ALIAS_URL" ]; then
body+=$'\n\n'"Always the latest preview of this PR: $ALIAS_URL"
fi
gh pr comment "${{ steps.pr.outputs.number }}" --repo "$GITHUB_REPOSITORY" --body "$body"
- name: Report deploy status
if: always()
env:
Expand Down
Loading