mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
178 lines
6.5 KiB
YAML
178 lines
6.5 KiB
YAML
name: Cloudflare Worker Preview Deploy
|
|
|
|
on:
|
|
pull_request:
|
|
paths:
|
|
- 'src/**'
|
|
- 'index.html'
|
|
- 'package.json'
|
|
- 'package-lock.json'
|
|
- 'vite.config.ts'
|
|
- 'tsconfig.json'
|
|
- '.github/workflows/cloudflare-web-preview.yml'
|
|
- '.github/actions/setup/**'
|
|
push:
|
|
branches:
|
|
- dev
|
|
paths:
|
|
- 'src/**'
|
|
- 'index.html'
|
|
- 'package.json'
|
|
- 'package-lock.json'
|
|
- 'vite.config.ts'
|
|
- 'tsconfig.json'
|
|
- '.github/workflows/cloudflare-web-preview.yml'
|
|
- '.github/actions/setup/**'
|
|
|
|
concurrency:
|
|
group: cloudflare-worker-preview-${{ github.event.pull_request.number || github.ref_name }}
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
deploy:
|
|
if: (github.event.pull_request.head.repo.full_name == github.repository && github.head_ref != 'release') || github.event_name == 'push'
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
pull-requests: write
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
with:
|
|
persist-credentials: false
|
|
|
|
- name: Prepare preview metadata
|
|
id: metadata
|
|
shell: bash
|
|
run: |
|
|
preview_message="$(git log -1 --pretty=%s)"
|
|
preview_message="$(printf '%s' "$preview_message" | head -c 100)"
|
|
|
|
{
|
|
echo 'preview_message<<EOF'
|
|
echo "$preview_message"
|
|
echo EOF
|
|
} >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Set Sentry build environment for PR preview
|
|
if: github.event_name == 'pull_request'
|
|
env:
|
|
VITE_SENTRY_DSN: ${{ secrets.VITE_SENTRY_DSN }}
|
|
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
|
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
|
|
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
|
|
shell: bash
|
|
run: |
|
|
echo "VITE_SENTRY_DSN=$VITE_SENTRY_DSN" >> "$GITHUB_ENV"
|
|
echo "VITE_SENTRY_ENVIRONMENT=preview" >> "$GITHUB_ENV"
|
|
echo "VITE_SENTRY_PR=${{ github.event.pull_request.number }}" >> "$GITHUB_ENV"
|
|
echo "SENTRY_AUTH_TOKEN=$SENTRY_AUTH_TOKEN" >> "$GITHUB_ENV"
|
|
echo "SENTRY_ORG=$SENTRY_ORG" >> "$GITHUB_ENV"
|
|
echo "SENTRY_PROJECT=$SENTRY_PROJECT" >> "$GITHUB_ENV"
|
|
|
|
- name: Setup app and build
|
|
uses: ./.github/actions/setup
|
|
with:
|
|
build: 'true'
|
|
|
|
- name: Set deploy alias
|
|
id: alias
|
|
shell: bash
|
|
run: |
|
|
if [ "${{ github.event_name }}" = "pull_request" ]; then
|
|
echo "alias=pr-${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT"
|
|
else
|
|
branch="${GITHUB_REF_NAME}"
|
|
alias="$(echo "$branch" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g')"
|
|
echo "alias=${alias}" >> "$GITHUB_OUTPUT"
|
|
fi
|
|
|
|
- name: Upload Worker preview
|
|
id: deploy
|
|
uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0
|
|
env:
|
|
PREVIEW_MESSAGE: ${{ steps.metadata.outputs.preview_message }}
|
|
with:
|
|
apiToken: ${{ secrets.TF_CLOUDFLARE_API_TOKEN }}
|
|
accountId: ${{ secrets.TF_VAR_ACCOUNT_ID }}
|
|
command: >
|
|
versions upload
|
|
-c dist/wrangler.json
|
|
--preview-alias ${{ steps.alias.outputs.alias }}
|
|
--message "$PREVIEW_MESSAGE"
|
|
|
|
- name: Resolve preview URL
|
|
id: preview
|
|
env:
|
|
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
|
|
COMMAND_OUTPUT: ${{ steps.deploy.outputs.command-output }}
|
|
ALIAS: ${{ steps.alias.outputs.alias }}
|
|
shell: bash
|
|
run: |
|
|
alias_url_pattern="^https?://${ALIAS}-[^[:space:]]+$"
|
|
preview_url=""
|
|
|
|
if printf '%s\n' "$DEPLOYMENT_URL" | grep -Eq "$alias_url_pattern"; then
|
|
preview_url="$DEPLOYMENT_URL"
|
|
else
|
|
preview_url="$(printf '%s\n' "$COMMAND_OUTPUT" | grep -Eo "https?://${ALIAS}-[^[:space:]\"'<>)]+" | head -n 1 || true)"
|
|
fi
|
|
|
|
if ! printf '%s\n' "$preview_url" | grep -Eq "$alias_url_pattern"; then
|
|
echo "Failed to resolve aliased Worker preview URL." >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "preview_url=${preview_url}" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Publish summary and PR comment
|
|
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
|
env:
|
|
MARKER: '<!-- cloudflare-worker-preview -->'
|
|
PREVIEW_URL: ${{ steps.preview.outputs.preview_url }}
|
|
PREVIEW_ALIAS: ${{ steps.alias.outputs.alias }}
|
|
SHORT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
|
|
with:
|
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
script: |
|
|
const marker = process.env.MARKER;
|
|
const previewUrl = process.env.PREVIEW_URL;
|
|
const previewAlias = process.env.PREVIEW_ALIAS;
|
|
const shortSha = process.env.SHORT_SHA?.slice(0, 7);
|
|
const now = new Date().toUTCString().replace(':00 GMT', ' UTC');
|
|
|
|
if (!previewUrl) {
|
|
core.setFailed("Missing preview URL from Cloudflare deploy step.");
|
|
return;
|
|
}
|
|
|
|
const tableRow = "| ✅ Deployment successful! | <a href='" + previewUrl + "'>" + previewUrl + "</a> | " + shortSha + " | `" + previewAlias + "` | " + now + " |";
|
|
const comment = [
|
|
marker,
|
|
`## Deploying with <a href="https://workers.dev"><img alt="Cloudflare Workers" src="https://workers.cloudflare.com/logo.svg" width="16"></a> Cloudflare Workers`,
|
|
``,
|
|
`| Status | Preview URL | Commit | Alias | Updated (UTC) |`,
|
|
`| - | - | - | - | - |`,
|
|
tableRow,
|
|
].join("\n");
|
|
|
|
await core.summary.addRaw(comment.replace(marker + "\n", "")).write();
|
|
|
|
if (context.eventName !== 'pull_request') return;
|
|
|
|
const { owner, repo } = context.repo;
|
|
const issue_number = context.issue.number;
|
|
const comments = await github.paginate(github.rest.issues.listComments, {
|
|
owner, repo, issue_number,
|
|
});
|
|
const existing = comments.find(
|
|
(c) => c.user.type === "Bot" && c.body.includes(marker),
|
|
);
|
|
if (existing) {
|
|
await github.rest.issues.deleteComment({
|
|
owner, repo, comment_id: existing.id,
|
|
});
|
|
}
|
|
|
|
await github.rest.issues.createComment({ owner, repo, issue_number, body: comment });
|