Automated Vercel Deployments
Automate your Vercel deployments with GitHub Actions for preview and production environments.
Vercel Deployment Workflow
name: Deploy to Vercel
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Pull Vercel Environment
run: vercel pull --yes --environment=${{ github.event_name == 'push' && 'production' || 'preview' }} --token=${{ secrets.VERCEL_TOKEN }}
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
- name: Build Project
run: vercel build ${{ github.event_name == 'push' && '--prod' || '' }}
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
- name: Deploy to Vercel
id: deploy
run: |
URL=$(vercel deploy --prebuilt ${{ github.event_name == 'push' && '--prod' || '' }} --token=${{ secrets.VERCEL_TOKEN }})
echo "url=$URL" >> $GITHUB_OUTPUT
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
- name: Comment deployment URL
if: github.event_name == 'pull_request'
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: `🚀 Deployed to Vercel: ${{ steps.deploy.outputs.url }}`
})
Required Secrets
Set these in your repository settings:
VERCEL_TOKEN- Vercel API tokenVERCEL_ORG_ID- Your Vercel organization IDVERCEL_PROJECT_ID- Project ID from Vercel
Multi-Environment Strategy
jobs:
deploy-preview:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
environment: preview
steps:
- name: Deploy Preview
run: vercel deploy --token=${{ secrets.VERCEL_TOKEN }}
deploy-production:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- name: Deploy Production
run: vercel deploy --prod --token=${{ secrets.VERCEL_TOKEN }}
Deployment with Tests
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to Vercel
run: vercel deploy --prod --token=${{ secrets.VERCEL_TOKEN }}
Best Practices
✅ Preview deployments for PRs - Test before merging
✅ Production deploys from main - Controlled releases
✅ Run tests before deploy - Quality gates
✅ Comment deployment URLs - Easy access for reviewers
✅ Use environment secrets - Secure configuration
Key Takeaways
GitHub Actions integrates seamlessly with Vercel for automated deployments. Preview environments for pull requests enable thorough testing before production releases.