Skip to content

ci: add vercel preview GitHub worflowk #3938

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

yomybaby
Copy link
Member

resolves #NNN (FR-MMM)

Checklist: (if applicable)

  • Documentation
  • Minium required manager version
  • Specific setting for review (eg., KB link, endpoint or how to setup)
  • Minimum requirements to check during review
  • Test case(s) to demonstrate the difference of before/after

@github-actions github-actions bot added the size:M 30~100 LoC label Jul 11, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • flow:merge-queue - adds this PR to the back of the merge queue
  • flow:hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has required the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Comment on lines 9 to 95
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- name: Install Vercel CLI
run: npm install -g vercel@latest

- name: Handle PR Events
env:
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
BRANCH_NAME=${{ github.event.pull_request.head.ref }}
LABELS=$(echo "${{ toJson(github.event.pull_request.labels) }}" | jq -r '.[].name')

# Check for 'preview' label
HAS_PREVIEW_LABEL=false
for label in $LABELS; do
if [ "$label" == "preview" ]; then
HAS_PREVIEW_LABEL=true
break
fi
done

if [ "${{ github.event.action }}" == "closed" ]; then
echo "PR closed. Attempting to remove deployment..."
# List deployments and remove those matching the branch
DEPLOYMENTS=$(vercel ls --token=$VERCEL_TOKEN --meta gitBranch=$BRANCH_NAME --json | jq -r '.[].url' 2>/dev/null || echo "")
if [ -n "$DEPLOYMENTS" ]; then
echo "$DEPLOYMENTS" | while read -r deployment; do
if [ -n "$deployment" ]; then
echo "Removing deployment: $deployment"
vercel remove $deployment --yes --token=$VERCEL_TOKEN || echo "Failed to remove $deployment"
fi
done
else
echo "No deployments found for branch: $BRANCH_NAME"
fi
elif [ "$HAS_PREVIEW_LABEL" == "true" ]; then
echo "Preview label found. Deploying preview..."

# Pull Vercel configuration
vercel pull --yes --environment=preview --token=$VERCEL_TOKEN

# Build the project
echo "Building project..."
vercel build --token=$VERCEL_TOKEN

# Deploy the built project
echo "Deploying to Vercel..."
DEPLOYMENT_URL=$(vercel deploy --prebuilt --token=$VERCEL_TOKEN)

if [ $? -eq 0 ] && [ -n "$DEPLOYMENT_URL" ]; then
echo "✅ Deployment successful!"
echo "🔗 Preview URL: $DEPLOYMENT_URL"

# Add deployment URL as a comment to PR
echo "DEPLOYMENT_URL=$DEPLOYMENT_URL" >> $GITHUB_ENV
else
echo "❌ Deployment failed"
exit 1
fi
else
echo "No 'preview' label. Skipping deployment."
fi

- name: Comment PR with deployment URL
if: env.DEPLOYMENT_URL
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 **Preview deployment ready!**\n\n📎 **Preview URL:** ${process.env.DEPLOYMENT_URL}\n\n*This preview will be automatically updated when you push new commits to this PR.*`
})

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}

Copilot Autofix

AI 10 days ago

To fix the issue, we will add a permissions block at the root of the workflow file. This block will define the minimal permissions required for the workflow to function correctly. Based on the workflow's operations, it needs contents: read to access repository contents and pull-requests: write to comment on pull requests. These permissions will be explicitly set to ensure the workflow adheres to the principle of least privilege.


Suggested changeset 1
.github/workflows/vercel-preview.yml

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/.github/workflows/vercel-preview.yml b/.github/workflows/vercel-preview.yml
--- a/.github/workflows/vercel-preview.yml
+++ b/.github/workflows/vercel-preview.yml
@@ -2,2 +2,6 @@
 
+permissions:
+  contents: read
+  pull-requests: write
+
 on:
EOF
@@ -2,2 +2,6 @@

permissions:
contents: read
pull-requests: write

on:
Copilot is powered by AI and may make mistakes. Always verify output.
@yomybaby yomybaby added the preview Trigger preview deployment for this pull request. label Jul 11, 2025
@yomybaby yomybaby force-pushed the ci/vercel-preview branch from 25054da to f4e4091 Compare July 11, 2025 00:51
@yomybaby yomybaby force-pushed the ci/vercel-preview branch from f4e4091 to caa062c Compare July 11, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Trigger preview deployment for this pull request. size:M 30~100 LoC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant