Skip to content

Commit

Permalink
feat(Publish): add canary publish functional for release/*.0 branch…
Browse files Browse the repository at this point in the history
…es (#82)
  • Loading branch information
hamikhambardzumyan committed Feb 21, 2024
1 parent 2ebdb52 commit 394cecd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/notify-reviewers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
MESSAGE="@channel\nPull Request: [#$PR_ID]($PR_URL) by @$PR_AUTHOR\nRequested Reviewers: $REVIEWERS"
curl -X POST -H "Content-Type: application/json" -d "{\"text\": \"$MESSAGE\", \"username\": \"GitHub\", \"icon_url\": \"https://github.githubassets.com/favicons/favicon.png\"}" $INTERNAL_NOTIFICATION_CHANNEL_API_ENDPOINT
28 changes: 28 additions & 0 deletions .github/workflows/publish-canary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Publish canary version to NPM
on:
push:
branches:
- 'release/*.0'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- name: 📥 Install dependencies
run: npm ci
- name: 🔧 Build
run: |
BRANCH_NAME=${{ github.ref }}
COMMIT_SHA=${{ github.sha }}
npm run build -- --pure --canary $BRANCH_NAME --commitSHA $COMMIT_SHA
- name: 📦 Publish package on NPM
run: cd dist && npm publish --access public --tag canary
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTOMATION_ACCESS_TOKEN }}
8 changes: 5 additions & 3 deletions scripts/addVersionHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ const addVersionToHistory = async () => {
try {
const libVersionsFromRepo = await execCommand('npm view @geneui/components versions');

const libVersionsFromRepoArr = libVersionsFromRepo
const libStabileVersionsFromRepo = libVersionsFromRepo
.split(',')
.map((version) => `v${version.replace(/[\[\]\\n']/g, '').trim()}`);
.map((version) => `${version.replace(/[\[\]\\n']/g, '').trim()}`)
.filter((version) => version.match(/^\d+\.\d+\.\d+$/))
.map((version) => `v${version}`);
// .filter((version) => version.endsWith('0'));

const libVersions = {
versions: libVersionsFromRepoArr
versions: libStabileVersionsFromRepo
};

await fs.writeFile('./.storybook/lib-versions.json', JSON.stringify(libVersions));
Expand Down
20 changes: 19 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import yargs from 'yargs';

import { rmSync } from 'fs';
import { resolve } from 'path';
import { execCommand, copyStaticFilesToDist } from './utils';
import { execCommand, copyStaticFilesToDist, replaceCanaryVersionInDistPGK } from './utils';

// In case of true the build script should skip lint and semver steps
const { pure: isBuildRunInPureMode } = yargs.option('pure', {
Expand All @@ -14,6 +14,20 @@ const { pure: isBuildRunInPureMode } = yargs.option('pure', {
default: false
}).argv;

// The canary version branch name. If provided package json file version will be replaced
const { canary: canaryVersion } = yargs.option('canary', {
describe: 'If --canary argument is specified then package json version will be generated as canary version.',
type: 'string',
default: null
}).argv;

// The commit SHA. It should be provided for canary versions only
const { commitSHA } = yargs.option('commitSHA', {
describe: 'If --canary version is provided this argument should be provided too --commitSHA.',
type: 'string',
default: null
}).argv;

const spinner = ora({
color: 'yellow',
fail: 'Something went wrong please see errors bellow!'
Expand All @@ -30,6 +44,10 @@ const build = async () => {
await execCommand('rollup -c ./configs/rollup.config.js --bundleConfigAsCjs', 'rollup.config');

await copyStaticFilesToDist();

if (canaryVersion && commitSHA) {
await replaceCanaryVersionInDistPGK(canaryVersion, commitSHA);
}
} catch (error) {
return {
hasError: true,
Expand Down
36 changes: 31 additions & 5 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import chalk from 'chalk';
import { lstatSync, readdirSync } from 'fs';
import { copyFile, stat } from 'fs/promises';
import { join } from 'path';
import { copyFile, stat, readFile, writeFile } from 'fs/promises';
import { join, resolve } from 'path';
import { exec } from 'child_process';
import dayjs from 'dayjs';

/**
* Executes a shell command and return it as a Promise.
Expand Down Expand Up @@ -56,10 +57,35 @@ const isFileExists = async (filePath) => {
} catch (error) {
if (error.code === 'ENOENT') {
return false;
} else {
}
throw error;
}

}
};

export { execCommand, isDirectory, isFile, isFileExists, getDirectories, getFiles, copyStaticFilesToDist };
const replaceCanaryVersionInDistPGK = async (canaryVersion, commitSHA) => {
try {
const [version] = canaryVersion.split('/').reverse();
const packageJsonFile = await readFile(resolve(__dirname, '../dist/package.json'), 'utf8');
const packageJson = JSON.parse(packageJsonFile);
packageJson.version = `${version}-canary-${commitSHA}-${dayjs().format('DDMMYYYY')}`;
await writeFile(resolve(__dirname, '../dist/package.json'), JSON.stringify(packageJson, null, 4), 'utf8');
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;

}
};

export {
execCommand,
isDirectory,
isFile,
isFileExists,
getDirectories,
getFiles,
copyStaticFilesToDist,
replaceCanaryVersionInDistPGK
};

0 comments on commit 394cecd

Please sign in to comment.