Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions .github/TESTING_PUBLISHING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Testing the Publishing Flow

This guide explains how to test the publishing workflow without actually publishing to npm.

## Dry Run Mode

The publishing script supports a dry-run mode that shows what would happen without making any changes:

```bash
# Test from master branch
git checkout master
DRY_RUN=true pnpm run publish

# Or use the flag
pnpm run publish --dry-run
```

Dry-run mode will:
- ✅ Show what version would be created
- ✅ Show what packages would be published
- ✅ Show what git operations would happen
- ❌ **NOT** commit any changes
- ❌ **NOT** create git tags
- ❌ **NOT** push to remote
- ❌ **NOT** publish to npm

## Testing Locally

### 1. Test Version Calculation

```bash
# Check current version
node -p "require('./packages/less/package.json').version"

# Run dry-run to see what version would be created
DRY_RUN=true pnpm run publish
```

### 2. Test Branch Validation

```bash
# Try from a feature branch (should fail)
git checkout -b test-branch
pnpm run publish
# Should error: "Publishing is only allowed from 'master' or 'alpha' branches"
```

### 3. Test Alpha Branch Validations

```bash
# Switch to alpha branch
git checkout alpha

# Test with dry-run
DRY_RUN=true GITHUB_REF_NAME=alpha pnpm run publish

# This will show:
# - Version validation (must contain -alpha.)
# - Master sync check
# - Version comparison with master
```

### 4. Test Version Override

```bash
# Test explicit version override
EXPLICIT_VERSION=4.5.0 DRY_RUN=true pnpm run publish
# Should show: "✨ Using explicit version: 4.5.0"
```

## Testing the GitHub Actions Workflow

### 1. Test Workflow Syntax

```bash
# Validate workflow YAML
gh workflow view publish.yml
# Or use act (local GitHub Actions runner)
act push -W .github/workflows/publish.yml
```

### 2. Test on a Test Branch

Create a test branch that mimics master/alpha:

```bash
# Create test branch from master
git checkout -b test-publish-master master

# Make a small change
echo "# test" >> TEST.md
git add TEST.md
git commit -m "test: publishing workflow"

# Push to trigger workflow (if you want to test the full flow)
# Note: This will actually try to publish if version changed!
```

### 3. Test Workflow Manually

You can manually trigger the workflow from GitHub Actions UI:
1. Go to Actions tab
2. Select "Publish to NPM" workflow
3. Click "Run workflow"
4. Select branch and run

**Warning**: This will actually publish if conditions are met!

## Testing Specific Scenarios

### Test Master Branch Publishing

```bash
git checkout master
DRY_RUN=true pnpm run publish

# Should show:
# - Patch version increment (e.g., 4.4.2 → 4.4.3)
# - Publishing with 'latest' tag
# - Regular release creation
```

### Test Alpha Branch Publishing

```bash
git checkout alpha
DRY_RUN=true GITHUB_REF_NAME=alpha pnpm run publish

# Should show:
# - Alpha version increment (e.g., 5.0.0-alpha.1 → 5.0.0-alpha.2)
# - Publishing with 'alpha' tag
# - Pre-release creation
# - All alpha validations passing
```

### Test Version Validation

```bash
# Test that alpha versions can't go to latest
# (This is enforced in the script, so it will fail before publishing)

# Test that non-alpha versions can't go to alpha tag
# (Also enforced in the script)
```

## Safe Testing Checklist

Before actually publishing:

- [ ] Run dry-run mode to verify version calculation
- [ ] Verify branch restrictions work (try from wrong branch)
- [ ] Test alpha validations (if testing alpha branch)
- [ ] Check that version override works (if needed)
- [ ] Verify package.json files would be updated correctly
- [ ] Review what git operations would happen
- [ ] Confirm npm tag assignment is correct

## Troubleshooting

### Script fails with "branch not allowed"

Make sure you're on `master` or `alpha` branch, or set `GITHUB_REF_NAME` environment variable:

```bash
GITHUB_REF_NAME=master DRY_RUN=true pnpm run publish
```

### Version calculation seems wrong

Check the current version in `packages/less/package.json`:

```bash
node -p "require('./packages/less/package.json').version"
```

### Alpha validations failing

Make sure:
- Alpha branch is up-to-date with master
- Current version contains `-alpha.`
- Alpha base version is >= master version

## Real Publishing Test (Use with Caution)

If you want to test the actual publishing flow:

1. **Use a test npm package** (create a scoped package like `@your-username/less-test`)
2. **Temporarily modify the script** to use your test package name
3. **Test on a separate branch** that won't trigger the workflow
4. **Clean up** after testing

**Never test on the actual `less` package unless you're ready to publish!**
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ jobs:
- uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 8
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
Expand Down
194 changes: 194 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
name: Publish to NPM

on:
push:
branches:
- master
- alpha
paths-ignore:
- '**.md'
- '.github/**'
- 'docs/**'

permissions:
id-token: write # Required for OIDC trusted publishing
contents: read

jobs:
publish:
name: Publish to NPM
runs-on: ubuntu-latest
# Only run on push events, not pull requests
if: github.event_name == 'push'

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install pnpm
uses: pnpm/action-setup@v4

- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Run tests
run: pnpm run test

- name: Build
run: |
cd packages/less
pnpm run build

- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"

- name: Determine branch and tag type
id: branch-info
run: |
BRANCH="${{ github.ref_name }}"
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
if [ "$BRANCH" = "alpha" ]; then
echo "is_alpha=true" >> $GITHUB_OUTPUT
echo "npm_tag=alpha" >> $GITHUB_OUTPUT
echo "release_type=prerelease" >> $GITHUB_OUTPUT
else
echo "is_alpha=false" >> $GITHUB_OUTPUT
echo "npm_tag=latest" >> $GITHUB_OUTPUT
echo "release_type=release" >> $GITHUB_OUTPUT
fi

- name: Validate alpha branch requirements
if: steps.branch-info.outputs.is_alpha == 'true'
run: |
# Fetch master branch
git fetch origin master:master || true

# Check 1: Alpha branch must not be behind master
echo "🔍 Checking if alpha branch is up to date with master..."
MASTER_COMMITS=$(git rev-list --count alpha..master 2>/dev/null || echo "0")

if [ "$MASTER_COMMITS" -gt 0 ]; then
echo "❌ ERROR: Alpha branch is behind master by $MASTER_COMMITS commit(s)"
echo " Alpha branch must include all commits from master before publishing"
exit 1
fi
echo "✅ Alpha branch is up to date with master"

# Check 2: Get current version and validate it contains 'alpha'
CURRENT_VERSION=$(node -p "require('./packages/less/package.json').version")
echo "📦 Current version: $CURRENT_VERSION"

if [[ ! "$CURRENT_VERSION" =~ -alpha\. ]]; then
echo "❌ ERROR: Alpha branch version must contain '-alpha.'"
echo " Current version: $CURRENT_VERSION"
echo " Expected format: X.Y.Z-alpha.N"
exit 1
fi
echo "✅ Version contains 'alpha' suffix"

# Check 3: Alpha base version must be >= master version
echo "🔍 Comparing alpha base version with master version..."
MASTER_VERSION=$(git show master:packages/less/package.json 2>/dev/null | node -p "try { JSON.parse(require('fs').readFileSync(0, 'utf-8')).version } catch(e) { '0.0.0' }" || echo "0.0.0")

if [ "$MASTER_VERSION" = "0.0.0" ]; then
echo "⚠️ Could not determine master version, skipping comparison"
else
echo "📦 Master version: $MASTER_VERSION"

# Extract base version (remove -alpha.X suffix)
ALPHA_BASE=$(echo "$CURRENT_VERSION" | sed 's/-alpha\.[0-9]*$//')
echo "📦 Alpha base version: $ALPHA_BASE"

# Compare versions using semver from root workspace
COMPARE_RESULT=$(node -e "
const semver = require('semver');
const alphaBase = process.argv[1];
const master = process.argv[2];
if (semver.lt(alphaBase, master)) {
console.log('ERROR');
} else {
console.log('OK');
}
" "$ALPHA_BASE" "$MASTER_VERSION" 2>/dev/null || echo "ERROR")

if [ "$COMPARE_RESULT" = "ERROR" ]; then
echo "❌ ERROR: Alpha base version ($ALPHA_BASE) is lower than master version ($MASTER_VERSION)"
echo " According to semver, alpha base version must be >= master version"
exit 1
fi
echo "✅ Alpha base version is >= master version"
fi

- name: Ensure npm 11.5.1 or later for trusted publishing
run: npm install -g npm@latest

- name: Bump version and publish
id: publish
env:
GITHUB_REF_NAME: ${{ github.ref_name }}
run: |
pnpm run publish

# Extract version from package.json
VERSION=$(node -p "require('./packages/less/package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "tag=v$VERSION" >> $GITHUB_OUTPUT

- name: Create GitHub Release (Master)
if: steps.branch-info.outputs.is_alpha != 'true'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.publish.outputs.tag }}
release_name: Release ${{ steps.publish.outputs.tag }}
body: |
## Changes

See [CHANGELOG.md](https://github.com/less/less.js/blob/master/CHANGELOG.md) for details.

## Installation

```bash
npm install less@${{ steps.publish.outputs.version }}
```
draft: false
prerelease: false

- name: Create GitHub Pre-Release (Alpha)
if: steps.branch-info.outputs.is_alpha == 'true'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.publish.outputs.tag }}
release_name: Alpha Release ${{ steps.publish.outputs.tag }}
body: |
## Alpha Release

This is an alpha release from the alpha branch.

## Installation

```bash
npm install less@${{ steps.publish.outputs.version }} --tag alpha
```

Or:

```bash
npm install less@alpha
```
draft: false
prerelease: true
Loading