name: Deploy on: push: branches: [main, next] workflow_dispatch: permissions: actions: read contents: read concurrency: group: deploy-${{ github.ref_name }} cancel-in-progress: true jobs: changes: if: github.repository == 'udecode/plate' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/next') runs-on: ubuntu-24.04 timeout-minutes: 5 outputs: build: ${{ steps.changes.outputs.build }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Decide whether the site changed id: changes env: GH_TOKEN: ${{ github.token }} run: | build=true if [[ "$GITHUB_EVENT_NAME" != workflow_dispatch ]]; then base="$(gh api "repos/$GITHUB_REPOSITORY/actions/workflows/deploy.yml/runs?branch=$GITHUB_REF_NAME&status=success&per_page=1" --jq '.workflow_runs[0].head_sha // empty' || true)" if [[ -n "$base" ]] && git fetch --no-tags --depth=1 origin "$base"; then if git diff --quiet "$base" "$GITHUB_SHA" -- .github/workflows/deploy.yml; then set +e VERCEL_GIT_COMMIT_MESSAGE="$(git log -1 --format=%B "$GITHUB_SHA")" VERCEL_GIT_PREVIOUS_SHA="$base" VERCEL_GIT_COMMIT_SHA="$GITHUB_SHA" node apps/www/scripts/vercel-ignore-build.mjs result=$? set -e case "$result" in 0) build=false ;; 1) build=true ;; *) exit "$result" ;; esac fi fi fi echo "build=$build" >> "$GITHUB_OUTPUT" deploy: needs: changes if: needs.changes.outputs.build == 'true' runs-on: ubuntu-24.04 timeout-minutes: 55 environment: name: ${{ github.ref_name == 'main' && 'Production' || 'Beta' }} url: ${{ github.ref_name == 'main' && 'https://platejs.org' || 'https://beta.platejs.org' }} env: VERCEL_ORG_ID: ${{ vars.VERCEL_ORG_ID }} VERCEL_PROJECT_ID: ${{ vars.VERCEL_PROJECT_ID }} VERCEL_CLI_VERSION: 64.5.0 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Check deployment configuration env: VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} run: | if [[ -z "$VERCEL_TOKEN" || -z "$VERCEL_ORG_ID" || -z "$VERCEL_PROJECT_ID" ]]; then echo '::error::Configure VERCEL_TOKEN, VERCEL_ORG_ID and VERCEL_PROJECT_ID in this deployment environment.' exit 1 fi - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 with: bun-version: 1.4.4 - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 22 - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 with: cache: true - name: Restore build cache id: build-cache uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: | apps/www/.next/cache .vercel/cache key: www-${{ github.ref_name }}-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.sha }} restore-keys: | www-${{ github.ref_name }}-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}- - name: Install Vercel CLI run: npm install --global "vercel@$VERCEL_CLI_VERSION" - name: Pull branch deployment settings run: | if [[ "$GITHUB_REF_NAME" == main ]]; then vercel pull --yes --environment=production else vercel pull --yes --environment=preview --git-branch=next fi env: VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} - name: Build deployment id: build env: NEXT_PUBLIC_SITE_URL: ${{ github.ref_name == 'main' && 'https://platejs.org' || 'https://beta.platejs.org' }} PLATE_BUILD_BASIC: '0' run: | nproc free -m uptime if [[ "$GITHUB_REF_NAME" == main ]]; then vercel build --prod else vercel build fi - name: Upload deployment without assigning domains id: deploy run: | git checkout --detach "$GITHUB_SHA" options=() if [[ "$GITHUB_REF_NAME" == main ]]; then options+=(--prod --skip-domain) fi url="$(vercel deploy --prebuilt --archive=tgz "${options[@]}" --meta plateCommitRef="$GITHUB_REF_NAME" --meta githubCommitSha="$GITHUB_SHA")" DEPLOYMENT_URL="$url" node --input-type=module -e ' const value = process.env.DEPLOYMENT_URL; const url = new URL(value); if (url.protocol !== "https:" || url.origin !== value || !url.hostname.endsWith(".vercel.app")) process.exit(1); ' echo "url=$url" >> "$GITHUB_OUTPUT" env: VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} - name: Check deployed routes env: DEPLOYMENT_URL: ${{ steps.deploy.outputs.url }} run: | curl --fail --silent --show-error --retry 2 --max-time 180 "$DEPLOYMENT_URL/" > /dev/null curl --fail --silent --show-error --retry 2 --max-time 180 "$DEPLOYMENT_URL/docs/installation/react" > /dev/null curl --fail --silent --show-error --retry 2 --max-time 180 "$DEPLOYMENT_URL/api/search?query=controlled&locale=en" | jq --exit-status 'type == "array" and length > 0' > /dev/null curl --fail --silent --show-error --retry 2 --max-time 180 "$DEPLOYMENT_URL/r/editor-basic.json" | jq --exit-status '.files | length > 0' > /dev/null - name: Publish the current branch deployment shell: bash env: GH_TOKEN: ${{ github.token }} VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} DEPLOYMENT_URL: ${{ steps.deploy.outputs.url }} run: | if [[ "$GITHUB_REF_NAME" == main ]]; then domain=platejs.org else domain=beta.platejs.org fi # A [skip ci] or path-ignored push moves the branch head but starts no newer run, # so compare with the commit the domain serves, not the head. live="$(curl --fail --silent --show-error --max-time 60 \ --header "Authorization: Bearer $VERCEL_TOKEN" \ "https://api.vercel.com/v4/aliases/$domain?teamId=$VERCEL_ORG_ID" | jq --exit-status --raw-output .deploymentId)" live_sha="$(curl --fail --silent --show-error --max-time 60 \ --header "Authorization: Bearer $VERCEL_TOKEN" \ "https://api.vercel.com/v13/deployments/$live?teamId=$VERCEL_ORG_ID" | jq --raw-output '.meta.githubCommitSha // empty')" if [[ -n "$live_sha" ]]; then order="$(gh api "repos/$GITHUB_REPOSITORY/compare/$live_sha...$GITHUB_SHA" --jq .status)" if [[ "$order" != ahead && "$order" != identical ]]; then echo "::error::https://$domain serves $live_sha, which $GITHUB_SHA does not follow ($order); leaving domains unchanged." exit 1 fi fi deployment="$(curl --fail --silent --show-error --max-time 60 \ --header "Authorization: Bearer $VERCEL_TOKEN" \ "https://api.vercel.com/v13/deployments/${DEPLOYMENT_URL#https://}?teamId=$VERCEL_ORG_ID" \ | jq --exit-status --raw-output '.id | select(startswith("dpl_"))')" if [[ "$GITHUB_REF_NAME" == main ]]; then curl --fail --silent --show-error --max-time 60 \ --header "Authorization: Bearer $VERCEL_TOKEN" \ --request POST \ "https://api.vercel.com/v10/projects/$VERCEL_PROJECT_ID/promote/$deployment?teamId=$VERCEL_ORG_ID" > /dev/null matched=false for attempt in {1..12}; do live="$(curl --fail --silent --show-error --max-time 15 \ --header "Authorization: Bearer $VERCEL_TOKEN" \ "https://api.vercel.com/v4/aliases/platejs.org?teamId=$VERCEL_ORG_ID" | jq --raw-output .deploymentId)" if [[ "$live" == "$deployment" ]]; then matched=true break fi sleep 5 done [[ "$matched" == true ]] else curl --fail --silent --show-error --max-time 60 \ --header "Authorization: Bearer $VERCEL_TOKEN" \ --json '{"alias":"beta.platejs.org"}' \ "https://api.vercel.com/v2/deployments/$deployment/aliases?teamId=$VERCEL_ORG_ID" \ | jq --exit-status '.alias == "beta.platejs.org"' > /dev/null fi echo "Deployed $GITHUB_SHA to https://$domain" >> "$GITHUB_STEP_SUMMARY" - name: Save build cache if: always() && steps.build.outcome == 'success' && steps.build-cache.outputs.cache-hit != 'true' uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: | apps/www/.next/cache .vercel/cache key: ${{ steps.build-cache.outputs.cache-primary-key }}