Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created January 23, 2024 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/129ae63127f396c9f4c0447aee4de368 to your computer and use it in GitHub Desktop.
Save sibelius/129ae63127f396c9f4c0447aee4de368 to your computer and use it in GitHub Desktop.
create a feature-production/ branch to release to production, also create a new tag
import { exec as execCb } from 'child_process';
import dotenvSafe from 'dotenv-safe';
import fs from 'fs';
import moment from 'moment';
import path from 'path';
import semver from 'semver';
import util from 'util';
// eslint-disable-next-line
import git from 'simple-git';
// eslint-disable-next-line
import changelog from 'generate-changelog';
// eslint-disable-next-line
import { Octokit } from '@octokit/rest';
// eslint-disable-next-line
const argv = require('minimist')(process.argv.slice(1));
const exec = util.promisify(execCb);
const root = path.join.bind(this, __dirname, '../');
dotenvSafe.config({
path: root('.env'),
sample: root('.env.example'),
});
const owner = 'entria';
const repo = 'woovi';
const githubToken = process.env.GITHUB_TOKEN || process.env.GITHUB_API_TOKEN;
const createPullRequest = async (branchName, tag) => {
if (!githubToken) {
return;
}
const octokit = new Octokit({
auth: githubToken,
});
const now = moment().format('YYYY-MM-DD');
// https://octokit.github.io/rest.js/#api-Repos-getReleases
// https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
const latestReleases = await octokit.repos.listReleases({
owner,
repo,
per_page: 1,
});
const latestReleaseTag =
latestReleases && latestReleases.data && latestReleases.data.length
? latestReleases.data[0].tag_name
: 'main';
await octokit.pulls.create({
owner,
repo,
title: `Deploy Production - ${tag} - ${now}`,
head: branchName,
base: 'main',
body: `https://github.com/${owner}/${repo}/compare/${latestReleaseTag}...main`,
});
};
(async () => {
try {
const resultTag = await git().tags();
const latestTag = resultTag.latest;
const currentChangelog = fs.readFileSync('./CHANGELOG.md');
const diffPattern = `${latestTag}..main`;
const changelogContent = await changelog.generate({
tag: diffPattern,
});
const rxVersion = /\d+\.\d+\.\d+/;
const latestVersion =
argv.version || changelogContent.match(rxVersion)?.[0];
const getReleaseType = () => {
if (argv.major) {
return 'major';
}
if (argv.minor) {
return 'minor';
}
return 'patch';
};
const newVersion = semver.inc(latestVersion, getReleaseType());
const newChangelogContent =
changelogContent.replace(rxVersion, newVersion) + currentChangelog;
fs.writeFileSync('./CHANGELOG.md', newChangelogContent);
await exec(`npm version --no-git-tag-version ${newVersion}`);
await exec(`yarn lerna version --no-git-tag-version --yes ${newVersion}`);
// docker package.json
await exec(
`cd packages/jobs/docker/worker && npm version --no-git-tag-version ${newVersion}`,
);
await exec(
`cd packages/main/docker/server && npm version --no-git-tag-version ${newVersion}`,
);
await exec(
`cd packages/migration/docker/migration && npm version --no-git-tag-version ${newVersion}`,
);
const tag = `v${newVersion}`;
const today = new Date();
const branchName = `feature-production/${today.getFullYear()}${
today.getMonth() + 1
}${today.getDate()}${today.getUTCHours()}${today.getUTCMinutes()}`;
await git().checkout(['-B', branchName]);
// add more as needed
await git().add([
'package.json',
'CHANGELOG.md',
'lerna.json',
'packages/*/package.json',
// "server/*/package.json",
// "core/*/package.json",
// "web/*/package.json",
]);
await git().commit(`build(change-log): ${tag}`, [], '-n');
await git().addAnnotatedTag(`${tag}`, `build(tag): ${tag}`);
await git().push(['--follow-tags', '-u', 'origin', branchName]);
await createPullRequest(branchName, tag);
} catch (err) {
// eslint-disable-next-line
console.log('err: ', err);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment