Skip to content

Instantly share code, notes, and snippets.

@srph
Created October 7, 2024 05:51
Show Gist options
  • Save srph/5a09ff3f944bcfa35ae9e2c0ef894313 to your computer and use it in GitHub Desktop.
Save srph/5a09ff3f944bcfa35ae9e2c0ef894313 to your computer and use it in GitHub Desktop.
Vercel Deploy Script

Vercel Deploy Script

This takes advantage of deployment hooks on Vercel. This simply removes the deploy file if it exists. Otherwise, it creates it. Then pushes it to the repository.

Usage

Add to your package.json

{
  "scripts": {
    "release": "node ./scripts/release.js"
  }
}

Afterwards you're good to go

npm run release
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const root = path.resolve(__dirname, "..");
const deployFilePath = path.join(root, "deploy");
// This script automates the deployment process:
// 1. Checks if the current branch is 'main'
// 2. Creates or removes a 'deploy' file in the root directory
// 3. Commits all changes with the message "deploy"
// 4. Pushes the changes to the remote repository
// This script relies that on Vercel's deploy on push hook.
console.log("[deploy] Checking current branch...");
try {
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", {
encoding: "utf8",
cwd: root,
}).trim();
if (currentBranch !== "main") {
console.error(
"[deploy] Warning: Current branch is not main. Current branch:",
currentBranch,
);
}
console.log("[deploy] Confirmed current branch is main");
} catch (error) {
console.error("[deploy] Error checking git branch:", error.message);
process.exit(1);
}
try {
console.log("[deploy] Starting deployment process...");
if (fs.existsSync(deployFilePath)) {
fs.unlinkSync(deployFilePath);
} else {
fs.writeFileSync(deployFilePath, "");
}
console.log("[deploy] Committing...");
// Run git commands
execSync("git add .", { stdio: "inherit", cwd: root });
execSync('git commit -m "deploy"', { stdio: "inherit", cwd: root });
console.log("[deploy] Pushed.");
execSync("git push", { stdio: "inherit", cwd: root });
} catch (error) {
console.error("[deploy] Error triggering deployment:", error.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment