Open jwafu opened 12 months ago
So, as per I got it we must follow Semantic Versioning (SemVer) to create two types of releases:
(e.g., v1.0.2)
: These are regular releases for production, typically without breaking changes.(e.g., v1.0.2-dev.0.1)
: These are for ongoing development or testing purposes and use the -dev.x pre-release tag.GitHub Action Workflow for Automatic Deployment:
We can also implement logic to delay the dev version for major releases, which can be manual or delayed by a time period as required.
Here’s a GitHub Action you can use:
name: NPM Publish
# This workflow is triggered automatically when code is pushed to the 'main' branch
# or when a pull request is made against 'main'. It can also be triggered manually.
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch: # Allows manual trigger of the workflow
jobs:
publish:
runs-on: ubuntu-latest
steps:
# Step 1: Check out the latest code from the repository
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Set up Node.js environment to run NPM commands
# We're using Node.js version 16, which is a stable LTS version
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
registry-url: 'https://registry.npmjs.org/' # Set NPM registry
# Step 3: Install project dependencies from package.json
# This ensures we have everything needed to run the project
- name: Install dependencies
run: npm install
# Step 4: Determine the current version of the package from package.json
# We check if it's a major version (e.g., 1.0.0) to decide whether to delay the dev release
- name: Determine version
id: version_check
run: |
# Get the current version from package.json
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "Current Version: $CURRENT_VERSION"
# If it's a major or minor version (e.g., 1.0.0 or 1.1.0), delay dev release
# You could add more advanced logic here if needed
if [[ "$CURRENT_VERSION" =~ ^[0-9]+\.[0-9]+\.0$ ]]; then
echo "Major or minor version change detected. Delay dev release."
echo "::set-output name=delay_dev_release::true"
else
echo "::set-output name=delay_dev_release::false"
fi
# Step 5: Publish the stable version to NPM
# This step will always run to upload the latest stable version
- name: Publish stable version to NPM
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # NPM token stored as a GitHub secret
# Step 6: Conditionally publish the dev version to NPM
# This only happens if the current version is NOT a major or minor release
- name: Publish dev version to NPM
if: steps.version_check.outputs.delay_dev_release == 'false'
run: |
# Increment the prerelease version with a 'dev' tag, e.g., 1.0.2 -> 1.0.2-dev.0
npm version prerelease --preid=dev
# Publish the prerelease version with the 'dev' tag
npm publish --tag dev
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # NPM token stored as a GitHub secret
We need to create a new format for our deployments to NPM - each deployment that does not contain breaking changes should be automatically updated uploaded to both a v1.0.2 version and also v1.0.2.dev-0.1
here is an example npm package with different versions that they are releasing: https://www.npmjs.com/package/react-dom?activeTab=versions