From 394cecdd7e90e8ac2da0b13d2d304e194ff0c481 Mon Sep 17 00:00:00 2001 From: Hamik Hambardzumyan <150047343+hamikhambardzumyan@users.noreply.github.com> Date: Wed, 21 Feb 2024 14:01:46 +0400 Subject: [PATCH] feat(Publish): add canary publish functional for `release/*.0` branches (#82) --- .github/workflows/notify-reviewers.yml | 2 +- .github/workflows/publish-canary.yml | 28 ++++++++++++++++++++ scripts/addVersionHistory.js | 8 +++--- scripts/build.js | 20 +++++++++++++- scripts/utils.js | 36 ++++++++++++++++++++++---- 5 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/publish-canary.yml diff --git a/.github/workflows/notify-reviewers.yml b/.github/workflows/notify-reviewers.yml index 92c59e6a..9475729c 100644 --- a/.github/workflows/notify-reviewers.yml +++ b/.github/workflows/notify-reviewers.yml @@ -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 - \ No newline at end of file + diff --git a/.github/workflows/publish-canary.yml b/.github/workflows/publish-canary.yml new file mode 100644 index 00000000..6417dd98 --- /dev/null +++ b/.github/workflows/publish-canary.yml @@ -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 }} diff --git a/scripts/addVersionHistory.js b/scripts/addVersionHistory.js index 26d0373a..6eaa0d76 100644 --- a/scripts/addVersionHistory.js +++ b/scripts/addVersionHistory.js @@ -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)); diff --git a/scripts/build.js b/scripts/build.js index e40a1414..ea6e6e83 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -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', { @@ -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!' @@ -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, diff --git a/scripts/utils.js b/scripts/utils.js index ce2f9d3c..bce724a6 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -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. @@ -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 +};