Skip to content

Commit

Permalink
chore: refactor auto beta release to be publish branch
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbarion committed May 15, 2024
1 parent dff62c1 commit 47bf56d
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 145 deletions.
63 changes: 0 additions & 63 deletions .github/workflows/beta-release.yaml

This file was deleted.

77 changes: 77 additions & 0 deletions .github/workflows/publish-branch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Publish Branch

on:
push:
branches:
- main
- dev

jobs:
release_candidate:
runs-on: ubuntu-latest
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- uses: actions/setup-node@v4
with:
node-version: "16.x"
registry-url: "https://registry.npmjs.org"
cache: "yarn"

- name: Install dev dependencies
run: yarn install

- name: Setup npm credentials file
run: echo "//registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN" >> .npmrc

# - name: Setup git credentials
# run: |
# git config --global user.name 'Auto Release Bot'
# git config --global user.email 'auto-release-bot@users.noreply.github.com'

- name: Get current package.json version
run: echo "PACKAGE_VERSION=$(npm pkg get version | tr -d '"')" >> $GITHUB_ENV

# get the sort SHA and add it into the environment variables
- name: Get short SHA
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV

# get the package name so the workflow can be reusable between projects
# - name: Get current package.json name
# run: echo "PACKAGE_NAME=$(npm pkg get name | tr -d '"')" >> $GITHUB_ENV

- name: Setup Branch Release Candidate Version
run: echo "NEW_VERSION=$PACKAGE_VERSION-$BRANCH_NAME.$SHORT_SHA" >> $GITHUB_ENV

- name: Update Package.json Version with Release Candidate Version
run: node ./update-branch-version.js --version $NEW_VERSION

- name: Publish a new branch release candidate version
run: npm publish --tag $BRANCH_NAME
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

# This comment only works for Pull Requests
# - uses: actions/github-script@v6
# with:
# script: |
# github.rest.issues.createComment({
# issue_number: context.issue.number,
# owner: context.repo.owner,
# repo: context.repo.repo,
# body: `Release candidate released with the last commit 🚀

# \`\`\`
# yarn add ${{ env.PACKAGE_NAME}}@${{ env.NEW_VERSION }}
# \`\`\`
# or
# \`\`\`
# npm install ${{ env.PACKAGE_NAME}}@${{ env.NEW_VERSION }}
# \`\`\`
# `
# })
82 changes: 0 additions & 82 deletions beta-release.js

This file was deleted.

37 changes: 37 additions & 0 deletions update-branch-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable prefer-destructuring */
/* eslint-disable @typescript-eslint/no-var-requires */
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const args = require("minimist")(process.argv.slice(2));

const version = args["version"];

console.log("version: ", version);

const runCommand = async (command) => {
return new Promise((resolve) => {
exec(command, (error, stdout, stderr) => {
resolve({ error, stdout, stderr });
});
});
};

/**
* @NOTE: This can't be done by only using the workflow, because when we try to update
* the version to the desired version, we get an error about wrong version format,
* but directly on CI it works as expected.
*/
const UpdateBranchVersion = async () => {
// update the release candidate version on package.json
const { error } = await runCommand(`npm version ${version}`);

if (error) {
console.error(error);
return;
}

// the release candidate version is already updated on package.json on the next line
console.log("Next Release Candidate version: ", `${version}`);
};

UpdateBranchVersion();

0 comments on commit 47bf56d

Please sign in to comment.