Merged in fix/deploy-script (pull request #3389)
chore(deploy): fixed trim in deploy script * chore(deploy): fixed trim in deploy script Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -3,10 +3,10 @@ import { createInterface } from "node:readline";
|
|||||||
|
|
||||||
// Configuration
|
// Configuration
|
||||||
const ENV_BRANCH_MAP: Record<string, string> = {
|
const ENV_BRANCH_MAP: Record<string, string> = {
|
||||||
test: "test",
|
test: "test",
|
||||||
stage: "stage",
|
stage: "stage",
|
||||||
preprod: "prod",
|
preprod: "prod",
|
||||||
prod: "release",
|
prod: "release",
|
||||||
};
|
};
|
||||||
|
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
@@ -14,115 +14,115 @@ const targetEnv = args[0];
|
|||||||
|
|
||||||
// Validate input
|
// Validate input
|
||||||
if (!targetEnv) {
|
if (!targetEnv) {
|
||||||
console.error("❌ Error: Please provide an environment.");
|
console.error("❌ Error: Please provide an environment.");
|
||||||
console.log(
|
console.log(
|
||||||
`Usage: yarn dlx tsx deploy.ts <${Object.keys(ENV_BRANCH_MAP).join("|")}>`
|
`Usage: yarn dlx tsx deploy.ts <${Object.keys(ENV_BRANCH_MAP).join("|")}>`
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetBranch = ENV_BRANCH_MAP[targetEnv];
|
const targetBranch = ENV_BRANCH_MAP[targetEnv];
|
||||||
|
|
||||||
if (!targetBranch) {
|
if (!targetBranch) {
|
||||||
console.error(`❌ Error: Invalid environment '${targetEnv}'.`);
|
console.error(`❌ Error: Invalid environment '${targetEnv}'.`);
|
||||||
console.log(
|
console.log(
|
||||||
`Available environments: ${Object.keys(ENV_BRANCH_MAP).join(", ")}`
|
`Available environments: ${Object.keys(ENV_BRANCH_MAP).join(", ")}`
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Git helper
|
// Git helper
|
||||||
const runGit = (
|
const runGit = (
|
||||||
command: string,
|
command: string,
|
||||||
options: { stdio?: "inherit" | "ignore" | "pipe" } = {}
|
options: { stdio?: "inherit" | "ignore" | "pipe" } = {}
|
||||||
) => {
|
) => {
|
||||||
return execSync(command, { encoding: "utf-8", ...options }).trim();
|
return execSync(command, { encoding: "utf-8", ...options });
|
||||||
};
|
};
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
// Get current branch
|
// Get current branch
|
||||||
const currentBranch = runGit("git rev-parse --abbrev-ref HEAD");
|
const currentBranch = runGit("git rev-parse --abbrev-ref HEAD").trim();
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`\n🚀 Preparing to deploy branch '${currentBranch}' to '${targetEnv}' (target branch: '${targetBranch}')`
|
`\n🚀 Preparing to deploy branch '${currentBranch}' to '${targetEnv}' (target branch: '${targetBranch}')`
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check for tagging requirement
|
// Check for tagging requirement
|
||||||
let tagToCreate: string | null = null;
|
let tagToCreate: string | null = null;
|
||||||
const releaseBranchRegex = /^release-(v\d+\.\d+\.\d+)$/;
|
const releaseBranchRegex = /^release-(v\d+\.\d+\.\d+)$/;
|
||||||
const releaseBranchMatch = currentBranch.match(releaseBranchRegex);
|
const releaseBranchMatch = currentBranch.match(releaseBranchRegex);
|
||||||
|
|
||||||
if (releaseBranchMatch) {
|
if (releaseBranchMatch) {
|
||||||
const tagName = releaseBranchMatch[1];
|
const tagName = releaseBranchMatch[1];
|
||||||
try {
|
try {
|
||||||
// Check if tag already exists locally or remote
|
// Check if tag already exists locally or remote
|
||||||
// Checking local first
|
// Checking local first
|
||||||
runGit(`git rev-parse ${tagName}`, { stdio: "ignore" });
|
runGit(`git rev-parse ${tagName}`, { stdio: "ignore" });
|
||||||
console.log(`ℹ️ Tag '${tagName}' already exists.`);
|
console.log(`ℹ️ Tag '${tagName}' already exists.`);
|
||||||
} catch {
|
} catch {
|
||||||
// Tag doesn't exist
|
// Tag doesn't exist
|
||||||
tagToCreate = tagName;
|
tagToCreate = tagName;
|
||||||
console.log(`✨ Will create and push tag: '${tagName}'`);
|
console.log(`✨ Will create and push tag: '${tagName}'`);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (!releaseBranchMatch && targetEnv === "prod") {
|
|
||||||
console.warn(
|
|
||||||
"⚠️ Warning: Deploying to prod from a non-release branch. No version tag will be created."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Confirmation prompt
|
|
||||||
const rl = createInterface({
|
|
||||||
input: process.stdin,
|
|
||||||
output: process.stdout,
|
|
||||||
});
|
|
||||||
|
|
||||||
const confirmed = await new Promise<boolean>((resolve) => {
|
|
||||||
rl.question(
|
|
||||||
`\n❓ Are you sure you want to deploy ${currentBranch} to ${targetEnv}? (y/n) `,
|
|
||||||
(answer) => {
|
|
||||||
rl.close();
|
|
||||||
resolve(answer.toLowerCase() === "y");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!confirmed) {
|
|
||||||
console.log("🚫 Deployment aborted.");
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("\n🔄 Starting deployment...");
|
|
||||||
|
|
||||||
// 1. Create and push tag if needed
|
|
||||||
if (tagToCreate) {
|
|
||||||
console.log(`🏷️ Creating tag ${tagToCreate}...`);
|
|
||||||
runGit(`git tag ${tagToCreate}`);
|
|
||||||
|
|
||||||
console.log(`⬆️ Pushing tag ${tagToCreate}...`);
|
|
||||||
runGit(`git push origin ${tagToCreate}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Force push to target branch
|
|
||||||
console.log(
|
|
||||||
`🔥 Force pushing '${currentBranch}' to 'origin/${targetBranch}'...`
|
|
||||||
);
|
|
||||||
runGit(
|
|
||||||
`git push origin ${currentBranch}:${targetBranch} --force-with-lease`,
|
|
||||||
{
|
|
||||||
stdio: "inherit",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(`\n✅ Deployment to ${targetEnv} completed successfully!`);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("\n❌ Deployment failed.");
|
|
||||||
if (error instanceof Error) {
|
|
||||||
console.error(error.message);
|
|
||||||
}
|
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
if (!releaseBranchMatch && targetEnv === "prod") {
|
||||||
|
console.warn(
|
||||||
|
"⚠️ Warning: Deploying to prod from a non-release branch. No version tag will be created."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirmation prompt
|
||||||
|
const rl = createInterface({
|
||||||
|
input: process.stdin,
|
||||||
|
output: process.stdout,
|
||||||
|
});
|
||||||
|
|
||||||
|
const confirmed = await new Promise<boolean>((resolve) => {
|
||||||
|
rl.question(
|
||||||
|
`\n❓ Are you sure you want to deploy ${currentBranch} to ${targetEnv}? (y/n) `,
|
||||||
|
(answer) => {
|
||||||
|
rl.close();
|
||||||
|
resolve(answer.toLowerCase() === "y");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!confirmed) {
|
||||||
|
console.log("🚫 Deployment aborted.");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("\n🔄 Starting deployment...");
|
||||||
|
|
||||||
|
// 1. Create and push tag if needed
|
||||||
|
if (tagToCreate) {
|
||||||
|
console.log(`🏷️ Creating tag ${tagToCreate}...`);
|
||||||
|
runGit(`git tag ${tagToCreate}`);
|
||||||
|
|
||||||
|
console.log(`⬆️ Pushing tag ${tagToCreate}...`);
|
||||||
|
runGit(`git push origin ${tagToCreate}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Force push to target branch
|
||||||
|
console.log(
|
||||||
|
`🔥 Force pushing '${currentBranch}' to 'origin/${targetBranch}'...`
|
||||||
|
);
|
||||||
|
runGit(
|
||||||
|
`git push origin ${currentBranch}:${targetBranch} --force-with-lease`,
|
||||||
|
{
|
||||||
|
stdio: "inherit",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(`\n✅ Deployment to ${targetEnv} completed successfully!`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("\n❌ Deployment failed.");
|
||||||
|
if (error instanceof Error) {
|
||||||
|
console.error(error.message);
|
||||||
|
}
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|||||||
Reference in New Issue
Block a user