Deploy to Cloud Platforms
Automate deployments to Vercel, Railway, and Fly.io — preview deployments on every PR, production on merge
Skills taught: cicd.vercel, cicd.railway, cicd.flyio, cicd.preview-deploys, cicd.cloud-deploy
Preview deployments — the game-changer
A preview deployment is a unique, temporary URL for every PR. When you open a PR, CI builds your app and deploys it to a real environment at a URL like my-app-pr-42.vercel.app. Reviewers can click the link and test the actual running app — not look at code and imagine what it does. When the PR is closed, the preview is automatically deleted. This workflow has become standard in modern web development.
Vercel — zero-config deployment
name: Vercel Deploy
on:
push:
branches: [main]
pull_request:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy to Vercel
uses: amondnet/vercel-action@v25
with:
vercel-token: ${{ secrets.VERCEL_TOKEN }}
vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
# On PRs: deploys to a preview URL
# On main: deploys to production with --prod
vercel-args: ${{ github.ref == 'refs/heads/main' && '--prod' || '' }}
# Post the preview URL as a PR comment
- name: Comment PR with preview URL
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const url = '${{ steps.deploy.outputs.preview-url }}';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 Preview deployed: ${url}`
});
Railway — deploy Docker or code directly
- name: Install Railway CLI
run: npm install -g @railway/cli
- name: Deploy to Railway
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
run: |
# Deploy to a specific environment
railway up --service my-api --environment production
# Railway auto-detects the framework and builds it
# Or deploy a Docker image:
railway up --image myapp:${{ github.sha }}
Fly.io — global edge deployment
- name: Deploy to Fly.io
uses: superfly/flyctl-actions/setup-flyctl@master
- name: Deploy
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
run: |
# fly.toml must exist in the repo (run 'fly launch' once locally)
flyctl deploy --remote-only
# --remote-only: builds the Docker image on Fly's servers
# No local Docker needed — fast even on small runners
# fly.toml specifies regions, VM size, auto-scaling, health checks
# Fly automatically deploys to multiple regions for low latency globally
Smoke test after deployment
- name: Deploy
id: deploy
run: |
flyctl deploy --remote-only
# Get the app URL
echo "url=$(flyctl info --json | jq -r .hostname)" >> $GITHUB_OUTPUT
- name: Smoke test
run: |
URL="https://${{ steps.deploy.outputs.url }}"
echo "Testing $URL"
# Wait for the deployment to be ready (max 60s)
for i in $(seq 1 12); do
STATUS=$(curl -s -o /dev/null -w '%{http_code}' $URL/health)
if [ $STATUS -eq 200 ]; then
echo "✓ App is up and healthy"
exit 0
fi
echo "Attempt $i: got $STATUS, retrying..."
sleep 5
done
echo "✗ App failed to become healthy"
exit 1
Tip: Always smoke test after deploying. A successful deployment step means the files were transferred and the container started — it doesn't mean the app is actually responding. A smoke test (HTTP request to /health or the homepage) catches configuration errors, missing environment variables, and startup crashes that only manifest in production. It takes 10 seconds to add and has caught real production outages before anyone noticed.
Frequently Asked Questions
How do I avoid deploying preview environments on every trivial commit (docs, README fixes)?
Use paths filtering: 'on: pull_request: paths: [src/**, package.json, ...]'. Preview deployments only trigger when source code changes, not docs or config. You can also add a label-based gate: only deploy if the PR has a 'preview' label, using 'if: contains(github.event.pull_request.labels.*.name, "preview")'.