-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support branch-scoped preview deployments + cleanup #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lustsazeus-lab
wants to merge
5
commits into
ubiquity:main
Choose a base branch
from
lustsazeus-lab:feat/issue-7-branch-previews
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+284
−22
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e18a3d
feat: support branch-scoped preview projects and cleanup
659062f
style(ci): satisfy actionlint shellcheck output grouping
27cbead
fix(workflow): gracefully fall back to shared preview for long projec…
lustsazeus-lab c8e1400
fix(workflow): handle shared strategy in preview mode + align cleanup…
lustsazeus-lab c750009
fix(workflow): validate preview_strategy and guard against production…
lustsazeus-lab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| name: Reusable - Deno Deploy Preview Cleanup | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| project: | ||
| description: Production project name (must end with -ubq-fi) | ||
| required: true | ||
| type: string | ||
| ref_name: | ||
| description: Deleted branch ref name (for example github.event.ref from a delete event) | ||
| required: true | ||
| type: string | ||
| preview_project: | ||
| description: Explicit preview project name to delete (overrides auto-generated name) | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| preview_strategy: | ||
| description: Preview naming strategy when preview_project is not provided (branch|shared) | ||
| required: false | ||
| type: string | ||
| default: "branch" | ||
| prod_branch: | ||
| description: Production branch name (skips deletion when ref_name equals this branch) | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| secrets: | ||
| DENO_DEPLOY_TOKEN: | ||
| required: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| cleanup: | ||
| name: Delete preview project | ||
| runs-on: ubuntu-22.04 | ||
| env: | ||
| PROJECT: ${{ inputs.project }} | ||
| REF_NAME: ${{ inputs.ref_name }} | ||
| PREVIEW_PROJECT: ${{ inputs.preview_project }} | ||
| PREVIEW_STRATEGY: ${{ inputs.preview_strategy }} | ||
| PROD_BRANCH: ${{ inputs.prod_branch != '' && inputs.prod_branch || github.event.repository.default_branch }} | ||
| DENO_DEPLOY_TOKEN: ${{ secrets.DENO_DEPLOY_TOKEN }} | ||
| steps: | ||
| - name: Determine preview project name | ||
| id: target | ||
| run: | | ||
| set -euo pipefail | ||
|
|
||
| slugify() { | ||
| printf '%s' "$1" \ | ||
| | tr '[:upper:]' '[:lower:]' \ | ||
| | sed -E 's#[^a-z0-9]+#-#g; s#-+#-#g; s#^-+##; s#-+$##' | ||
| } | ||
|
|
||
| project="${PROJECT:-}" | ||
| if [[ "$project" != *-ubq-fi ]]; then | ||
| echo "::error::Project name must end with '-ubq-fi'. Got: $project" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ref_name="${REF_NAME:-}" | ||
| if [ -z "$ref_name" ]; then | ||
| echo "::notice::Empty ref_name, skipping cleanup" | ||
| echo "skip=true" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [ "$ref_name" = "$PROD_BRANCH" ]; then | ||
| echo "::notice::Deleted ref matches production branch (${PROD_BRANCH}), skipping cleanup" | ||
| echo "skip=true" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
|
|
||
| base="${project%-ubq-fi}" | ||
| target="${PREVIEW_PROJECT:-}" | ||
| strategy="${PREVIEW_STRATEGY:-branch}" | ||
| if [ "$strategy" != "branch" ] && [ "$strategy" != "shared" ]; then | ||
| echo "::error::Invalid preview_strategy '${strategy}'. Expected 'branch' or 'shared'." | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ -z "$target" ]; then | ||
| if [ "$strategy" = "branch" ]; then | ||
| branch_slug="$(slugify "$ref_name")" | ||
| if [ -z "$branch_slug" ]; then | ||
| branch_slug="preview" | ||
| fi | ||
|
|
||
| suffix="-${base}-ubq-fi" | ||
| max_branch_len=$((26 - ${#suffix})) | ||
| if [ $max_branch_len -lt 6 ]; then | ||
| # Project name is too long for branch-based previews; fall back to | ||
| # shared preview naming so repos with long names (e.g. notifications-ubq-fi) | ||
| # don't break after upgrading without having to explicitly set strategy=shared. | ||
| strategy="shared" | ||
| fi | ||
| if [ "$strategy" = "branch" ]; then | ||
| if [ ${#branch_slug} -gt $max_branch_len ]; then | ||
| hash=$(printf '%s' "$branch_slug" | sha1sum | cut -c1-4) | ||
| head_len=$((max_branch_len - 5)) | ||
| branch_slug="${branch_slug:0:${head_len}}-${hash}" | ||
| fi | ||
| target="${branch_slug}-${base}-ubq-fi" | ||
| fi | ||
| if [ "$strategy" = "shared" ] && [ -z "$target" ]; then | ||
| if [ ${#base} -gt 17 ]; then | ||
| hash=$(printf '%s' "$base" | sha1sum | cut -c1-4) | ||
| base="${base:0:12}-${hash}" | ||
| fi | ||
| target="p-${base}-ubq-fi" | ||
| fi | ||
| if [ "$strategy" = "branch" ] && [ -z "$target" ]; then | ||
| echo "::error::Cannot build branch preview name for base '${base}' within Deno project length limits" | ||
| exit 1 | ||
| fi | ||
| else | ||
| if [ ${#base} -gt 17 ]; then | ||
| hash=$(printf '%s' "$base" | sha1sum | cut -c1-4) | ||
| base="${base:0:12}-${hash}" | ||
| fi | ||
| target="p-${base}-ubq-fi" | ||
| fi | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| fi | ||
|
|
||
| if ! [[ "$target" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]]; then | ||
| echo "::error::Derived preview project name is invalid: $target" | ||
| exit 1 | ||
| fi | ||
| # Guard: refuse to delete the production project | ||
| if [ "$target" = "$project" ]; then | ||
| echo "::error::Refusing to delete production project '${project}' via preview cleanup" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "skip=false" >> "$GITHUB_OUTPUT" | ||
| echo "project=$target" >> "$GITHUB_OUTPUT" | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| - name: Delete preview project | ||
| if: steps.target.outputs.skip != 'true' | ||
| env: | ||
| TARGET_PROJECT: ${{ steps.target.outputs.project }} | ||
| run: | | ||
| set -euo pipefail | ||
| api="https://dash.deno.com/api/projects/${TARGET_PROJECT}" | ||
| code=$(curl -sS -o /tmp/delete-preview-project.json -w "%{http_code}" -X DELETE \ | ||
| -H "Authorization: Bearer ${DENO_DEPLOY_TOKEN}" \ | ||
| "$api" || echo "000") | ||
|
|
||
| if [ "$code" = "404" ]; then | ||
| echo "Preview project ${TARGET_PROJECT} already removed" | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [ "$code" -lt 200 ] || [ "$code" -ge 300 ]; then | ||
| echo "::error::Failed to delete preview project ${TARGET_PROJECT} (HTTP ${code})" | ||
| cat /tmp/delete-preview-project.json || true | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Deleted preview project ${TARGET_PROJECT}" | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.