From 59e82f64a19bf148b66ef6c68be230bc028b7f4f Mon Sep 17 00:00:00 2001 From: Kevin Pearson Date: Fri, 10 Nov 2023 14:16:46 -0800 Subject: [PATCH] PR deploy --- .github/workflows/release-github.yml | 50 +++++++++++----------------- src/js/version.mjs | 40 +++++++++++++++++----- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/.github/workflows/release-github.yml b/.github/workflows/release-github.yml index 655e275c5..d4d8764d1 100644 --- a/.github/workflows/release-github.yml +++ b/.github/workflows/release-github.yml @@ -35,43 +35,36 @@ jobs: run: 'echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc' - name: Install dependencies run: npm ci - - name: Set explicit npm dist-tag default - if: github.ref_name == 'main' + - name: Set main tags + if: github.ref_name == 'main' && github.event_name != 'pull_request' run: | - echo "NPM_DIST_TAG=latest" >> $GITHUB_ENV - echo "RELEASE_CHANNEL=latest" >> $GITHUB_ENV - - name: Override npm dist-tag for non-main releases - if: github.ref_name != 'main' + echo 'NPM_DIST_TAG="--tag latest"' >> $GITHUB_ENV + echo "DOCS_TAG=latest" >> $GITHUB_ENV + - name: Set next tags + if: github.ref_name == 'next' && github.event_name != 'pull_request' run: | - echo "Setting NPM_DIST_TAG to '${{ github.ref_name }}'" - echo "NPM_DIST_TAG=${{ github.ref_name }}" >> $GITHUB_ENV - - name: Override release channel for non-main releases - if: github.ref_name != 'main' + echo 'NPM_DIST_TAG="--tag next"' >> $GITHUB_ENV + echo "DOCS_TAG=next" >> $GITHUB_ENV + - name: Set PR tags + if: github.ref_name != 'main' && github.ref_name != 'next' && github.event_name == 'pull_request' run: | - echo "Setting RELEASE_CHANNEL to '$(node ./src/js/version.mjs channel $NPM_DIST_TAG)' using branch name '$NPM_DIST_TAG'" - echo "RELEASE_CHANNEL=$(node ./src/js/version.mjs channel $NPM_DIST_TAG)" >> $GITHUB_ENV - - name: Override release channel for PRs - if: github.event_name == 'pull_request' - run: | - echo "Setting RELEASE_CHANNEL to '$(node ./src/js/version.mjs channel ${{ github.head_ref }})' using branch name ${{ github.head_ref }}" - echo "RELEASE_CHANNEL=$(node ./src/js/version.mjs channel ${{ github.head_ref }})" >> $GITHUB_ENV + echo 'NPM_DIST_TAG=""' >> $GITHUB_ENV + echo "DOCS_TAG=${{ github.head_ref }}" >> $GITHUB_ENV - name: Release mono-artifact to GitHub if: (github.ref_name != 'main' && github.event_name != 'pull_request') || github.event_name == 'workflow_dispatch' env: GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE_BOT_PAT }} # <-- Allows semantic-release-bot to push changes to protected branches - npm_config_tag: ${{ env.NPM_DIST_TAG }} # <-- For main, this is `latest.` Otherwise, `next`, `next-major`, etc. See ./.releaserc for release branch config. run: npx semantic-release - name: Dry-run mono-artifact if: (github.ref_name == 'main' && github.event_name != 'workflow_dispatch') || github.event_name == 'pull_request' env: GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE_BOT_PAT }} # <-- Allows semantic-release-bot to push changes to protected branches - npm_config_tag: ${{ env.NPM_DIST_TAG }} # <-- For main, this is `latest.` Otherwise, `next`, `next-major`, etc. See ./.releaserc for release branch config. run: npx semantic-release --dry-run - name: Build artifacts for feature branches if: github.ref_name != 'main' && github.ref_name != 'next' run: | - # npm version prerelease --preid $RELEASE_CHANNEL --workspaces --git-tag-version false - # npm version prerelease --preid $RELEASE_CHANNEL --workspaces --include-workspace-root --git-tag-version false + $(node ./src/js/version.mjs latest-prerelease ${{ github.ref_name }}) + npm version prerelease --preid ${{ github.ref_name }} --workspaces --git-tag-version false npm run dist - name: Check if semantic-release created a build id: check_build @@ -82,35 +75,30 @@ jobs: if: steps.check_build.outputs.files_exists == 'true' && github.event_name != 'pull_request' && (github.ref_name != 'main' || github.event_name == 'workflow_dispatch') env: NPM_TOKEN: ${{ secrets.SEMANTIC_RELEASE_NPM_TOKEN }} # <-- Allows semantic-release to publish to npm without 2 factor auth. - npm_config_tag: ${{ env.NPM_DIST_TAG }} # <-- For main, this is `latest.` Otherwise, `next`, `next-major`, etc. See ./.releaserc for release branch config. run: | echo "NPM DIST TAG :: " ${{ env.NPM_DIST_TAG }} - echo "Release Channel :: " $RELEASE_CHANNEL - npm publish --tag $RELEASE_CHANNEL --workspaces + npm publish $NPM_DIST_TAG --workspaces - name: Dry-run Firebolt SDKs to NPM if: steps.check_build.outputs.files_exists == 'true' && (github.ref_name == 'main' && github.event_name != 'workflow_dispatch') env: NPM_TOKEN: ${{ secrets.SEMANTIC_RELEASE_NPM_TOKEN }} # <-- Allows semantic-release to publish to npm without 2 factor auth. - npm_config_tag: ${{ env.NPM_DIST_TAG }} # <-- For main, this is `latest.` Otherwise, `next`, `next-major`, etc. See ./.releaserc for release branch config. - run: npm publish --tag $RELEASE_CHANNEL --workspaces --dry-run + run: npm publish $NPM_DIST_TAG --workspaces --dry-run - name: Checkout firebolt GitHub.io repository if: steps.check_build.outputs.files_exists == 'true' env: GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE_BOT_PAT }} # <-- Allows semantic-release-bot to push changes to protected branches - npm_config_tag: ${{ env.NPM_DIST_TAG }} # <-- For main, this is `latest.` Otherwise, `next`, `next-major`, etc. See ./.releaserc for release branch config. run: | cd /tmp git clone https://rdkcentral:$GITHUB_TOKEN@github.com/rdkcentral/firebolt.git ./firebolt-docs - name: Release docs to GitHub.io - if: steps.check_build.outputs.files_exists == 'true' + if: steps.check_build.outputs.files_exists == 'true' && (github.ref_name == 'main' || github.ref_name != 'next' || github.event_name != 'pull_request') env: GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE_BOT_PAT }} # <-- Allows semantic-release-bot to push changes to protected branches - # npm_config_tag: ${{ env.NPM_DIST_TAG }} # <-- For main, this is `latest.` Otherwise, `next`, `next-major`, etc. See ./.releaserc for release branch config. run: | - npm run publish:docs /tmp/firebolt-docs $RELEASE_CHANNEL + npm run publish:docs /tmp/firebolt-docs $DOCS_TAG cd /tmp/firebolt-docs git config --global user.email "you@example.com" git config --global user.name "Your Name" git add --all - git diff-index --quiet HEAD || git commit -m "chore: Publish $RELEASE_CHANNEL" + git diff-index --quiet HEAD || git commit -m "chore: Publish $DOCS_TAG" git push diff --git a/src/js/version.mjs b/src/js/version.mjs index ca6ff73d3..91a3126ba 100644 --- a/src/js/version.mjs +++ b/src/js/version.mjs @@ -2,6 +2,7 @@ import { readFile, writeFile } from 'fs/promises' import path from 'path' +import { exec } from 'child_process' process.argv.shift() process.argv.shift() @@ -10,22 +11,22 @@ const task = process.argv.shift() let inWorkspace = false let packageJson = await readFile(path.join('.', 'package.json')) - .then(bytes => bytes.toString()) - .then(JSON.parse) + .then(bytes => bytes.toString()) + .then(JSON.parse) if (!packageJson.workspaces) { inWorkspace = true packageJson = await readFile(path.join('..', '..', '..', 'package.json')) - .then(bytes => bytes.toString()) - .then(JSON.parse) + .then(bytes => bytes.toString()) + .then(JSON.parse) } const packageMain = packageJson if (task === 'sync') { - + console.log() - + await Promise.all(packageMain.workspaces.map(async workspace => { console.log(`Updating ${workspace}/package.json to version ${packageMain.version}.`) const packageWorkspace = await readFile(path.join('.', workspace, 'package.json')) @@ -40,8 +41,8 @@ if (task === 'sync') { else if (task === 'channel') { const branch = process.argv.shift() const releaserc = await readFile(path.join('.', '.releaserc')) - .then(bytes => bytes.toString()) - .then(JSON.parse) + .then(bytes => bytes.toString()) + .then(JSON.parse) // default to 'test' channel if branch is unknown const config = releaserc.branches.find(b => b.name === branch) || { channel: branch.replace('/', '-') } @@ -76,4 +77,27 @@ else if (task === 'validate') { })) +} else if (task === 'latest-prerelease') { + const prerelease = process.argv.shift() + console.log('prerelease : ' + prerelease) + exec("npm show @firebolt-js/sdk versions --json", (error, result, errlog) => { + if (error) { + console.error(`error: ${error.message}`); + return; + } + let versions = JSON.parse(result).sort().reverse() + let matches = versions.filter(v => { + if (v.indexOf('-') !== -1) { + let prerel = v.substring(v.indexOf('-') + 1) + return prerel.startsWith(prerelease + '.') + } + return false + }) + if (matches.length > 0) { + console.log('Setting package version to ' + matches[0]) + exec("npm version " + matches[0] + " --workspaces") + return matches[0] + } + return null + }); }