Skip to content

Commit 0170e6c

Browse files
author
Sunita Prajapati
committed
fix(config): dynamically detect the current branch at runtime
1 parent 75d48df commit 0170e6c

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

multi-release.config.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
const { execSync } = require('child_process');
2+
3+
// Detect current branch: GITHUB_REF_NAME (Actions) → GITHUB_REF → git
4+
function getCurrentBranch() {
5+
if (process.env.GITHUB_REF_NAME) return process.env.GITHUB_REF_NAME;
6+
if (process.env.GITHUB_REF)
7+
return process.env.GITHUB_REF.replace('refs/heads/', '');
8+
return execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
9+
}
10+
11+
const currentBranch = getCurrentBranch();
12+
13+
// Sanitize branch name for semver: replace any non-alphanumeric/hyphen char with '-'
14+
const prerelease = currentBranch
15+
.replace(/[^a-zA-Z0-9-]/g, '-')
16+
.replace(/^-+|-+$/g, '');
17+
18+
const isStableBranch =
19+
currentBranch === 'master' || /^\d+(\.\d+)*\.x$/.test(currentBranch);
20+
const isBetaBranch = currentBranch === 'beta';
21+
22+
const branches = [
23+
'master',
24+
{ name: '+([0-9])?(.{+([0-9]),x}).x', prerelease: true }, // support branches (e.g., 1.x, 1.2.x)
25+
{ name: 'beta', prerelease: 'beta' }, // explicit beta channel
26+
];
27+
28+
// Add current branch explicitly with a sanitized (semver-safe) prerelease identifier
29+
if (!isStableBranch && !isBetaBranch) {
30+
branches.push({ name: currentBranch, prerelease });
31+
}
32+
133
module.exports = {
2-
branches: [
3-
'master',
4-
{ name: '+([0-9])?(.{+([0-9]),x}).x', prerelease: true }, // support branches (e.g., 1.x, 1.2.x)
5-
{ name: 'beta', prerelease: true }, // explicit beta channel
6-
{ name: 'fix/*', prerelease: true }, // fix branches → x.x.x-fix.N
7-
{ name: 'feat/*', prerelease: true }, // feature branches → x.x.x-feat.N
8-
],
34+
branches,
935
tagFormat: '${name}-v${version}',
1036
deps: {
1137
bump: 'satisfy', // Do not trigger a release for every package if the only change is a minor/patch upgrade of dependencies

release.config.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
const { execSync } = require('child_process');
2+
3+
// Detect current branch: GITHUB_REF_NAME (Actions) → GITHUB_REF → git
4+
function getCurrentBranch() {
5+
if (process.env.GITHUB_REF_NAME) return process.env.GITHUB_REF_NAME;
6+
if (process.env.GITHUB_REF)
7+
return process.env.GITHUB_REF.replace('refs/heads/', '');
8+
return execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
9+
}
10+
11+
const currentBranch = getCurrentBranch();
12+
13+
// Sanitize branch name for semver: replace any non-alphanumeric/hyphen char with '-'
14+
const prerelease = currentBranch
15+
.replace(/[^a-zA-Z0-9-]/g, '-')
16+
.replace(/^-+|-+$/g, '');
17+
18+
const isStableBranch =
19+
currentBranch === 'master' || /^\d+(\.\d+)*\.x$/.test(currentBranch);
20+
const isBetaBranch = currentBranch === 'beta';
21+
22+
const branches = [
23+
'master',
24+
{ name: '+([0-9])?(.{+([0-9]),x}).x', prerelease: true }, // support branches (e.g., 1.x, 1.2.x)
25+
{ name: 'beta', prerelease: 'beta' }, // explicit beta channel
26+
];
27+
28+
// Add current branch explicitly with a sanitized (semver-safe) prerelease identifier
29+
if (!isStableBranch && !isBetaBranch) {
30+
branches.push({ name: currentBranch, prerelease });
31+
}
32+
133
module.exports = {
2-
branches: [
3-
'master',
4-
{ name: '+([0-9])?(.{+([0-9]),x}).x', prerelease: true }, // support branches (e.g., 1.x, 1.2.x)
5-
{ name: 'beta', prerelease: true }, // explicit beta channel
6-
{ name: 'fix/*', prerelease: true }, // fix branches → x.x.x-fix.N
7-
{ name: 'feat/*', prerelease: true }, // feature branches → x.x.x-feat.N
8-
],
34+
branches,
935
tagFormat: '${name}-v${version}',
1036
plugins: [
1137
['@semantic-release/commit-analyzer', { preset: 'conventionalcommits' }],

0 commit comments

Comments
 (0)