Skip to content

Commit 536c351

Browse files
committed
feat: enhance publish workflow with semantic versioning and release branches
1 parent 800baec commit 536c351

File tree

1 file changed

+130
-9
lines changed

1 file changed

+130
-9
lines changed

.github/workflows/publish.yml

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1-
name: Publish Engine plugin
1+
name: Publish Engine Plugin
22

33
on:
4-
workflow_dispatch: {}
4+
workflow_dispatch:
5+
inputs:
6+
version_bump:
7+
description: 'Type of version bump to perform'
8+
required: true
9+
default: 'patch'
10+
type: choice
11+
options:
12+
- patch
13+
- minor
14+
- major
15+
npm_tag:
16+
description: 'Custom npm dist-tag (leave empty for auto-detection based on branch)'
17+
required: false
18+
type: string
19+
dry_run:
20+
description: 'Dry run (no actual publishing)'
21+
required: false
22+
default: true
23+
type: boolean
524

625
push:
726
branches:
827
- main
28+
- 'release/v[0-9]*'
929
paths:
1030
- '.browserslistrc'
1131
- 'babel.config.*'
@@ -27,6 +47,8 @@ jobs:
2747
steps:
2848
- name: Check out code
2949
uses: actions/checkout@v4
50+
with:
51+
fetch-depth: 0
3052

3153
- name: Cache dependencies
3254
uses: actions/cache@v4
@@ -68,8 +90,8 @@ jobs:
6890
- name: Check out code
6991
uses: actions/checkout@v4
7092
with:
71-
fetch-depth: 0
72-
ref: main
93+
fetch-depth: 2
94+
ref: ${{ github.ref }}
7395

7496
- name: Restore dependencies
7597
uses: actions/cache/restore@v4
@@ -96,16 +118,115 @@ jobs:
96118
registry-url: https://registry.npmjs.org
97119
scope: '@defra'
98120

99-
- name: Update package versions
100-
run: npm version patch --git-tag-version false --save
121+
- name: Determine version bump type
122+
id: version-type
123+
run: |
124+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
125+
126+
# Check if this is a manual trigger with inputs
127+
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
128+
VERSION_TYPE="${{ github.event.inputs.version_bump }}"
129+
echo "Manual workflow run with version bump: $VERSION_TYPE"
130+
131+
# If a custom npm tag was provided, use it
132+
if [[ -n "${{ github.event.inputs.npm_tag }}" ]]; then
133+
CUSTOM_NPM_TAG="${{ github.event.inputs.npm_tag }}"
134+
echo "Using custom npm tag: $CUSTOM_NPM_TAG"
135+
echo "CUSTOM_NPM_TAG=$CUSTOM_NPM_TAG" >> $GITHUB_OUTPUT
136+
fi
137+
else
138+
# Automatic trigger via push - use commit message logic
139+
COMMIT_MSG=$(git log -1 --pretty=%B)
140+
VERSION_TYPE="patch"
141+
142+
# Check for MINOR or MAJOR in commit message
143+
if [[ "$COMMIT_MSG" == *"MINOR"* ]]; then
144+
VERSION_TYPE="minor"
145+
elif [[ "$COMMIT_MSG" == *"MAJOR"* ]]; then
146+
VERSION_TYPE="major"
147+
fi
148+
fi
149+
150+
# For release branches, respect the branch naming convention
151+
if [[ "$BRANCH_NAME" =~ release/v([0-9]+) ]]; then
152+
# Extract just the major version number
153+
MAJOR_VERSION="${BASH_REMATCH[1]}"
154+
155+
# Set the package version to match the major version if needed
156+
CURRENT_VERSION=$(npm pkg get version | tr -d \")
157+
CURRENT_MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1)
158+
159+
# If the major version doesn't match, reset to major.0.0
160+
if [[ "$CURRENT_MAJOR" != "$MAJOR_VERSION" ]]; then
161+
npm version $MAJOR_VERSION.0.0 --git-tag-version false --allow-same-version
162+
163+
# Override to patch since we've already set the version
164+
VERSION_TYPE="patch"
165+
fi
166+
fi
167+
168+
echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_OUTPUT
169+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_OUTPUT
170+
171+
- name: Update package version
172+
run: npm version ${{ steps.version-type.outputs.VERSION_TYPE }} --git-tag-version false --save
101173

102174
- name: Commit and push updates
103175
run: |
104176
git config user.name github-actions
105177
git config user.email github-actions@github.com
106-
git commit -am "v$(npm pkg get version | tr -d \") [skip ci]" && git push
178+
NEW_VERSION=$(npm pkg get version | tr -d \")
179+
git commit -am "v$NEW_VERSION [skip ci]" && git push
107180
108-
- name: Publish to npm
109-
run: npm publish --access public
181+
- name: Publish to npm with appropriate dist-tag
182+
run: |
183+
BRANCH_NAME="${{ steps.version-type.outputs.BRANCH_NAME }}"
184+
NEW_VERSION=$(npm pkg get version | tr -d \")
185+
PUBLISH_ARGS="--access public"
186+
IS_STANDARD_BRANCH=false
187+
188+
# Determine if this is a standard branch
189+
if [[ "$BRANCH_NAME" == "main" || "$BRANCH_NAME" =~ release/v([0-9]+) ]]; then
190+
IS_STANDARD_BRANCH=true
191+
fi
192+
193+
# First priority: Check for custom tag from version-type step
194+
if [[ -n "${{ steps.version-type.outputs.CUSTOM_NPM_TAG }}" ]]; then
195+
DIST_TAG="${{ steps.version-type.outputs.CUSTOM_NPM_TAG }}"
196+
PUBLISH_ARGS="$PUBLISH_ARGS --tag $DIST_TAG"
197+
echo "Publishing v$NEW_VERSION with custom tag '$DIST_TAG' (from version-type step)"
198+
# Second priority: Check for branch-specific tags
199+
elif [[ "$BRANCH_NAME" == "main" ]]; then
200+
echo "Publishing v$NEW_VERSION from main -> using default 'latest' tag"
201+
elif [[ "$BRANCH_NAME" =~ release/v([0-9]+) ]]; then
202+
MAJOR_VERSION="${BASH_REMATCH[1]}"
203+
DIST_TAG="v${MAJOR_VERSION}"
204+
PUBLISH_ARGS="$PUBLISH_ARGS --tag $DIST_TAG"
205+
echo "Publishing v$NEW_VERSION from $BRANCH_NAME -> using tag '$DIST_TAG'"
206+
else
207+
# Safety check for non-standard branches
208+
if [[ "${{ github.event_name }}" == "workflow_dispatch" && -z "${{ github.event.inputs.npm_tag }}" ]]; then
209+
echo "⚠️ WARNING: Publishing from non-standard branch '$BRANCH_NAME' without a custom npm tag!"
210+
echo "⚠️ This will publish as 'latest' and could overwrite your production release."
211+
echo "⚠️ Consider canceling and re-running with a custom npm tag like 'beta' or 'dev'."
212+
echo "⚠️ Waiting 10 seconds before proceeding..."
213+
sleep 10
214+
fi
215+
echo "Branch $BRANCH_NAME doesn't match expected patterns, using default publishing"
216+
fi
217+
218+
# Add dry-run flag if specified
219+
if [[ "${{ github.event.inputs.dry_run }}" == "true" ]]; then
220+
PUBLISH_ARGS="$PUBLISH_ARGS --dry-run"
221+
echo "DRY RUN MODE - No actual publishing will occur"
222+
fi
223+
224+
# TODO: REMOVE THIS LINE BEFORE MERGING TO MAIN
225+
# Temporary safety measure for testing
226+
PUBLISH_ARGS="$PUBLISH_ARGS --dry-run"
227+
echo "⚠️ TEST MODE: Force using --dry-run flag. Remove before merging to main! ⚠️"
228+
229+
# Execute npm publish with all arguments
230+
npm publish $PUBLISH_ARGS
110231
env:
111232
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}

0 commit comments

Comments
 (0)