Skip to content

Commit 5cd329c

Browse files
authored
Set up auto-publishing and alpha branch (#4390)
* Set up alpha branch and auto-publishing * Restore 4.5.0 version number * Set up post-merge hook to stablize version and fix tests * Restore .gitattributes * Fix to publish.yml * Revert PNPM version to version 8 for Node 16 compatibility * Another attempt to resolve PNPM version * Another attempt to resolve PNPM version
1 parent 4322869 commit 5cd329c

File tree

15 files changed

+4560
-8533
lines changed

15 files changed

+4560
-8533
lines changed

.github/TESTING_PUBLISHING.md

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# Testing the Publishing Flow
2+
3+
This guide explains how to test the publishing workflow without actually publishing to npm.
4+
5+
## Dry Run Mode
6+
7+
The publishing script supports a dry-run mode that shows what would happen without making any changes:
8+
9+
```bash
10+
# Test from master branch
11+
git checkout master
12+
DRY_RUN=true pnpm run publish
13+
14+
# Or use the flag
15+
pnpm run publish --dry-run
16+
```
17+
18+
Dry-run mode will:
19+
- ✅ Show what version would be created
20+
- ✅ Show what packages would be published
21+
- ✅ Show what git operations would happen
22+
-**NOT** commit any changes
23+
-**NOT** create git tags
24+
-**NOT** push to remote
25+
-**NOT** publish to npm
26+
27+
## Testing Locally
28+
29+
### 1. Test Version Calculation
30+
31+
```bash
32+
# Check current version
33+
node -p "require('./packages/less/package.json').version"
34+
35+
# Run dry-run to see what version would be created
36+
DRY_RUN=true pnpm run publish
37+
```
38+
39+
### 2. Test Branch Validation
40+
41+
```bash
42+
# Try from a feature branch (should fail)
43+
git checkout -b test-branch
44+
pnpm run publish
45+
# Should error: "Publishing is only allowed from 'master' or 'alpha' branches"
46+
```
47+
48+
### 3. Test Alpha Branch Validations
49+
50+
```bash
51+
# Switch to alpha branch
52+
git checkout alpha
53+
54+
# Test with dry-run
55+
DRY_RUN=true GITHUB_REF_NAME=alpha pnpm run publish
56+
57+
# This will show:
58+
# - Version validation (must contain -alpha.)
59+
# - Master sync check
60+
# - Version comparison with master
61+
```
62+
63+
### 4. Test Version Override
64+
65+
```bash
66+
# Test explicit version override
67+
EXPLICIT_VERSION=4.5.0 DRY_RUN=true pnpm run publish
68+
# Should show: "✨ Using explicit version: 4.5.0"
69+
```
70+
71+
## Testing the GitHub Actions Workflow
72+
73+
### 1. Test Workflow Syntax
74+
75+
```bash
76+
# Validate workflow YAML
77+
gh workflow view publish.yml
78+
# Or use act (local GitHub Actions runner)
79+
act push -W .github/workflows/publish.yml
80+
```
81+
82+
### 2. Test on a Test Branch
83+
84+
Create a test branch that mimics master/alpha:
85+
86+
```bash
87+
# Create test branch from master
88+
git checkout -b test-publish-master master
89+
90+
# Make a small change
91+
echo "# test" >> TEST.md
92+
git add TEST.md
93+
git commit -m "test: publishing workflow"
94+
95+
# Push to trigger workflow (if you want to test the full flow)
96+
# Note: This will actually try to publish if version changed!
97+
```
98+
99+
### 3. Test Workflow Manually
100+
101+
You can manually trigger the workflow from GitHub Actions UI:
102+
1. Go to Actions tab
103+
2. Select "Publish to NPM" workflow
104+
3. Click "Run workflow"
105+
4. Select branch and run
106+
107+
**Warning**: This will actually publish if conditions are met!
108+
109+
## Testing Specific Scenarios
110+
111+
### Test Master Branch Publishing
112+
113+
```bash
114+
git checkout master
115+
DRY_RUN=true pnpm run publish
116+
117+
# Should show:
118+
# - Patch version increment (e.g., 4.4.2 → 4.4.3)
119+
# - Publishing with 'latest' tag
120+
# - Regular release creation
121+
```
122+
123+
### Test Alpha Branch Publishing
124+
125+
```bash
126+
git checkout alpha
127+
DRY_RUN=true GITHUB_REF_NAME=alpha pnpm run publish
128+
129+
# Should show:
130+
# - Alpha version increment (e.g., 5.0.0-alpha.1 → 5.0.0-alpha.2)
131+
# - Publishing with 'alpha' tag
132+
# - Pre-release creation
133+
# - All alpha validations passing
134+
```
135+
136+
### Test Version Validation
137+
138+
```bash
139+
# Test that alpha versions can't go to latest
140+
# (This is enforced in the script, so it will fail before publishing)
141+
142+
# Test that non-alpha versions can't go to alpha tag
143+
# (Also enforced in the script)
144+
```
145+
146+
## Safe Testing Checklist
147+
148+
Before actually publishing:
149+
150+
- [ ] Run dry-run mode to verify version calculation
151+
- [ ] Verify branch restrictions work (try from wrong branch)
152+
- [ ] Test alpha validations (if testing alpha branch)
153+
- [ ] Check that version override works (if needed)
154+
- [ ] Verify package.json files would be updated correctly
155+
- [ ] Review what git operations would happen
156+
- [ ] Confirm npm tag assignment is correct
157+
158+
## Troubleshooting
159+
160+
### Script fails with "branch not allowed"
161+
162+
Make sure you're on `master` or `alpha` branch, or set `GITHUB_REF_NAME` environment variable:
163+
164+
```bash
165+
GITHUB_REF_NAME=master DRY_RUN=true pnpm run publish
166+
```
167+
168+
### Version calculation seems wrong
169+
170+
Check the current version in `packages/less/package.json`:
171+
172+
```bash
173+
node -p "require('./packages/less/package.json').version"
174+
```
175+
176+
### Alpha validations failing
177+
178+
Make sure:
179+
- Alpha branch is up-to-date with master
180+
- Current version contains `-alpha.`
181+
- Alpha base version is >= master version
182+
183+
## Real Publishing Test (Use with Caution)
184+
185+
If you want to test the actual publishing flow:
186+
187+
1. **Use a test npm package** (create a scoped package like `@your-username/less-test`)
188+
2. **Temporarily modify the script** to use your test package name
189+
3. **Test on a separate branch** that won't trigger the workflow
190+
4. **Clean up** after testing
191+
192+
**Never test on the actual `less` package unless you're ready to publish!**

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ jobs:
3232
- uses: actions/checkout@v4
3333
- name: Install pnpm
3434
uses: pnpm/action-setup@v4
35-
with:
36-
version: 8
3735
- uses: actions/setup-node@v4
3836
with:
3937
node-version: ${{ matrix.node }}

.github/workflows/publish.yml

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
name: Publish to NPM
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- alpha
8+
paths-ignore:
9+
- '**.md'
10+
- '.github/**'
11+
- 'docs/**'
12+
13+
permissions:
14+
id-token: write # Required for OIDC trusted publishing
15+
contents: read
16+
17+
jobs:
18+
publish:
19+
name: Publish to NPM
20+
runs-on: ubuntu-latest
21+
# Only run on push events, not pull requests
22+
if: github.event_name == 'push'
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v4
27+
with:
28+
fetch-depth: 0
29+
token: ${{ secrets.GITHUB_TOKEN }}
30+
31+
- name: Install pnpm
32+
uses: pnpm/action-setup@v4
33+
34+
- uses: actions/setup-node@v4
35+
with:
36+
node-version: 'lts/*'
37+
registry-url: 'https://registry.npmjs.org'
38+
cache: 'pnpm'
39+
40+
- name: Install dependencies
41+
run: pnpm install --frozen-lockfile
42+
43+
- name: Run tests
44+
run: pnpm run test
45+
46+
- name: Build
47+
run: |
48+
cd packages/less
49+
pnpm run build
50+
51+
- name: Configure Git
52+
run: |
53+
git config --global user.name "github-actions[bot]"
54+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
55+
56+
- name: Determine branch and tag type
57+
id: branch-info
58+
run: |
59+
BRANCH="${{ github.ref_name }}"
60+
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
61+
if [ "$BRANCH" = "alpha" ]; then
62+
echo "is_alpha=true" >> $GITHUB_OUTPUT
63+
echo "npm_tag=alpha" >> $GITHUB_OUTPUT
64+
echo "release_type=prerelease" >> $GITHUB_OUTPUT
65+
else
66+
echo "is_alpha=false" >> $GITHUB_OUTPUT
67+
echo "npm_tag=latest" >> $GITHUB_OUTPUT
68+
echo "release_type=release" >> $GITHUB_OUTPUT
69+
fi
70+
71+
- name: Validate alpha branch requirements
72+
if: steps.branch-info.outputs.is_alpha == 'true'
73+
run: |
74+
# Fetch master branch
75+
git fetch origin master:master || true
76+
77+
# Check 1: Alpha branch must not be behind master
78+
echo "🔍 Checking if alpha branch is up to date with master..."
79+
MASTER_COMMITS=$(git rev-list --count alpha..master 2>/dev/null || echo "0")
80+
81+
if [ "$MASTER_COMMITS" -gt 0 ]; then
82+
echo "❌ ERROR: Alpha branch is behind master by $MASTER_COMMITS commit(s)"
83+
echo " Alpha branch must include all commits from master before publishing"
84+
exit 1
85+
fi
86+
echo "✅ Alpha branch is up to date with master"
87+
88+
# Check 2: Get current version and validate it contains 'alpha'
89+
CURRENT_VERSION=$(node -p "require('./packages/less/package.json').version")
90+
echo "📦 Current version: $CURRENT_VERSION"
91+
92+
if [[ ! "$CURRENT_VERSION" =~ -alpha\. ]]; then
93+
echo "❌ ERROR: Alpha branch version must contain '-alpha.'"
94+
echo " Current version: $CURRENT_VERSION"
95+
echo " Expected format: X.Y.Z-alpha.N"
96+
exit 1
97+
fi
98+
echo "✅ Version contains 'alpha' suffix"
99+
100+
# Check 3: Alpha base version must be >= master version
101+
echo "🔍 Comparing alpha base version with master version..."
102+
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")
103+
104+
if [ "$MASTER_VERSION" = "0.0.0" ]; then
105+
echo "⚠️ Could not determine master version, skipping comparison"
106+
else
107+
echo "📦 Master version: $MASTER_VERSION"
108+
109+
# Extract base version (remove -alpha.X suffix)
110+
ALPHA_BASE=$(echo "$CURRENT_VERSION" | sed 's/-alpha\.[0-9]*$//')
111+
echo "📦 Alpha base version: $ALPHA_BASE"
112+
113+
# Compare versions using semver from root workspace
114+
COMPARE_RESULT=$(node -e "
115+
const semver = require('semver');
116+
const alphaBase = process.argv[1];
117+
const master = process.argv[2];
118+
if (semver.lt(alphaBase, master)) {
119+
console.log('ERROR');
120+
} else {
121+
console.log('OK');
122+
}
123+
" "$ALPHA_BASE" "$MASTER_VERSION" 2>/dev/null || echo "ERROR")
124+
125+
if [ "$COMPARE_RESULT" = "ERROR" ]; then
126+
echo "❌ ERROR: Alpha base version ($ALPHA_BASE) is lower than master version ($MASTER_VERSION)"
127+
echo " According to semver, alpha base version must be >= master version"
128+
exit 1
129+
fi
130+
echo "✅ Alpha base version is >= master version"
131+
fi
132+
133+
- name: Ensure npm 11.5.1 or later for trusted publishing
134+
run: npm install -g npm@latest
135+
136+
- name: Bump version and publish
137+
id: publish
138+
env:
139+
GITHUB_REF_NAME: ${{ github.ref_name }}
140+
run: |
141+
pnpm run publish
142+
143+
# Extract version from package.json
144+
VERSION=$(node -p "require('./packages/less/package.json').version")
145+
echo "version=$VERSION" >> $GITHUB_OUTPUT
146+
echo "tag=v$VERSION" >> $GITHUB_OUTPUT
147+
148+
- name: Create GitHub Release (Master)
149+
if: steps.branch-info.outputs.is_alpha != 'true'
150+
uses: actions/create-release@v1
151+
env:
152+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
153+
with:
154+
tag_name: ${{ steps.publish.outputs.tag }}
155+
release_name: Release ${{ steps.publish.outputs.tag }}
156+
body: |
157+
## Changes
158+
159+
See [CHANGELOG.md](https://github.com/less/less.js/blob/master/CHANGELOG.md) for details.
160+
161+
## Installation
162+
163+
```bash
164+
npm install less@${{ steps.publish.outputs.version }}
165+
```
166+
draft: false
167+
prerelease: false
168+
169+
- name: Create GitHub Pre-Release (Alpha)
170+
if: steps.branch-info.outputs.is_alpha == 'true'
171+
uses: actions/create-release@v1
172+
env:
173+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
174+
with:
175+
tag_name: ${{ steps.publish.outputs.tag }}
176+
release_name: Alpha Release ${{ steps.publish.outputs.tag }}
177+
body: |
178+
## Alpha Release
179+
180+
This is an alpha release from the alpha branch.
181+
182+
## Installation
183+
184+
```bash
185+
npm install less@${{ steps.publish.outputs.version }} --tag alpha
186+
```
187+
188+
Or:
189+
190+
```bash
191+
npm install less@alpha
192+
```
193+
draft: false
194+
prerelease: true

0 commit comments

Comments
 (0)