Skip to content

Commit 8486f93

Browse files
committed
ci: consolidate publish workflow with enhanced version management
Merges release and test publishing workflows into a single comprehensive workflow with improved version bump options, automated version detection, and better package management controls The previous separate workflows for release and test publishing have been combined into one unified publish.yml workflow that handles both official and test releases with enhanced configuration options. --- ci: 合并发布工作流程并增强版本管理 将发布和测试发布工作流合并为一个综合工作流程,具有改进的版本递增选项、自动版本检测和更好的包管理控制 之前用于发布和测试发布工作流已合并为一个统一的 publish.yml 工作流,该工作流处理正式发布和测试发布,并提供增强的配置选项。 Change-Id: Iecdd190ec2f09d3a351b6608d708e89087f66d53 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent 5e20938 commit 8486f93

3 files changed

Lines changed: 187 additions & 296 deletions

File tree

.github/workflows/publish.yml

Lines changed: 187 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,180 @@
11
name: Publish to npm
22

33
on:
4-
push:
5-
tags:
6-
- 'v*'
7-
branches:
8-
- main
4+
# GitHub Release 触发正式发布
5+
release:
6+
types: [published]
7+
8+
# 手动触发
99
workflow_dispatch:
1010
inputs:
1111
publish_type:
12-
description: 'Type of publish: official or test'
12+
description: '发布类型 / Publish type'
1313
required: true
1414
default: 'test'
1515
type: choice
1616
options:
17-
- official
18-
- test
17+
- official # 正式发布
18+
- test # 测试发布
19+
version:
20+
description: '版本号(留空则自动递增)/ Version (leave empty for auto-increment)'
21+
required: false
22+
type: string
23+
version_bump:
24+
description: '版本递增类型(仅自动递增时有效)/ Version bump type (only for auto-increment)'
25+
required: false
26+
default: 'patch'
27+
type: choice
28+
options:
29+
- patch # 0.0.1 -> 0.0.2
30+
- minor # 0.0.1 -> 0.1.0
31+
- major # 0.0.1 -> 1.0.0
1932
test_package_name:
20-
description: 'Optional custom package name for test publish (e.g. @alicloud/agentrun-sdk-test)'
33+
description: '测试包名(可选)/ Test package name (optional)'
2134
required: false
35+
type: string
2236

2337
jobs:
24-
prepare:
25-
name: Build
38+
publish:
39+
name: Publish Package
2640
runs-on: ubuntu-latest
27-
outputs:
28-
built: ${{ steps.build.outputs.built }}
41+
permissions:
42+
contents: write
43+
id-token: write
2944
steps:
3045
- name: Checkout
3146
uses: actions/checkout@v4
47+
with:
48+
ssh-key: ${{ secrets.DEPLOY_KEY }}
49+
ref: main
50+
fetch-depth: 0
3251

3352
- name: Setup Node.js
3453
uses: actions/setup-node@v4
3554
with:
3655
node-version: '18'
3756
registry-url: 'https://registry.npmjs.org'
57+
always-auth: true
3858

39-
- name: Clean and install dependencies
59+
- name: Determine publish type and version
60+
id: config
4061
run: |
41-
rm -rf node_modules package-lock.json
42-
npm install
62+
# 确定发布类型
63+
if [ "${{ github.event_name }}" = "release" ]; then
64+
PUBLISH_TYPE="official"
65+
VERSION="${{ github.event.release.tag_name }}"
66+
VERSION="${VERSION#v}" # 移除 v 前缀
67+
echo "Triggered by release: $VERSION"
68+
else
69+
PUBLISH_TYPE="${{ inputs.publish_type }}"
70+
VERSION="${{ inputs.version }}"
71+
fi
72+
73+
echo "PUBLISH_TYPE=${PUBLISH_TYPE}" >> $GITHUB_OUTPUT
74+
echo "Publish type: ${PUBLISH_TYPE}"
75+
76+
# 确定包名
77+
if [ "$PUBLISH_TYPE" = "test" ]; then
78+
TEST_PKG_NAME="${{ inputs.test_package_name }}"
79+
if [ -z "$TEST_PKG_NAME" ]; then
80+
TEST_PKG_NAME="@agentrun/sdk-test"
81+
fi
82+
PACKAGE_NAME="$TEST_PKG_NAME"
83+
NPM_TAG="test"
84+
else
85+
PACKAGE_NAME=$(node -p "require('./package.json').name")
86+
NPM_TAG="latest"
87+
fi
88+
89+
echo "PACKAGE_NAME=${PACKAGE_NAME}" >> $GITHUB_OUTPUT
90+
echo "NPM_TAG=${NPM_TAG}" >> $GITHUB_OUTPUT
91+
echo "Package name: ${PACKAGE_NAME}"
92+
echo "NPM tag: ${NPM_TAG}"
93+
94+
# 确定版本号
95+
if [ -n "$VERSION" ]; then
96+
# 使用指定的版本号
97+
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
98+
echo "Using specified version: ${VERSION}"
99+
else
100+
# 自动递增版本号
101+
echo "Auto-incrementing version..."
102+
103+
# 从 npm 获取当前最新版本
104+
NPM_RESPONSE=$(npm view "$PACKAGE_NAME" version 2>/dev/null || echo "")
105+
106+
if [ -z "$NPM_RESPONSE" ]; then
107+
CURRENT_VERSION="0.0.0"
108+
echo "Package not found on npm, starting from 0.0.0"
109+
else
110+
CURRENT_VERSION="$NPM_RESPONSE"
111+
echo "Latest version on npm: $CURRENT_VERSION"
112+
fi
113+
114+
# 解析版本号
115+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
116+
117+
# 根据用户选择递增版本
118+
BUMP_TYPE="${{ inputs.version_bump }}"
119+
if [ -z "$BUMP_TYPE" ]; then
120+
BUMP_TYPE="patch"
121+
fi
122+
123+
case "$BUMP_TYPE" in
124+
major)
125+
MAJOR=$((MAJOR + 1))
126+
MINOR=0
127+
PATCH=0
128+
;;
129+
minor)
130+
MINOR=$((MINOR + 1))
131+
PATCH=0
132+
;;
133+
patch)
134+
PATCH=$((PATCH + 1))
135+
;;
136+
esac
137+
138+
VERSION="${MAJOR}.${MINOR}.${PATCH}"
139+
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
140+
echo "New version: ${VERSION}"
141+
fi
43142
44-
- name: Build
45-
id: build
143+
- name: Update package.json
46144
run: |
47-
npm run build
48-
echo "built=true" >> $GITHUB_OUTPUT
49-
50-
publish-official:
51-
name: Publish Official Package
52-
needs: prepare
53-
runs-on: ubuntu-latest
54-
permissions:
55-
contents: read
56-
id-token: write
57-
if: startsWith(github.ref, 'refs/tags/v') || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_type == 'official')
58-
steps:
59-
- name: Checkout
60-
uses: actions/checkout@v4
145+
VERSION="${{ steps.config.outputs.VERSION }}"
146+
PACKAGE_NAME="${{ steps.config.outputs.PACKAGE_NAME }}"
147+
PUBLISH_TYPE="${{ steps.config.outputs.PUBLISH_TYPE }}"
148+
149+
# 更新 package.json
150+
node -e "
151+
const fs = require('fs');
152+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
153+
pkg.version = process.env.VERSION;
154+
if (process.env.PUBLISH_TYPE === 'test') {
155+
pkg.name = process.env.PACKAGE_NAME;
156+
}
157+
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
158+
"
159+
echo "Updated package.json:"
160+
cat package.json | head -10
161+
env:
162+
VERSION: ${{ steps.config.outputs.VERSION }}
163+
PACKAGE_NAME: ${{ steps.config.outputs.PACKAGE_NAME }}
164+
PUBLISH_TYPE: ${{ steps.config.outputs.PUBLISH_TYPE }}
61165

62-
- name: Setup Node.js
63-
uses: actions/setup-node@v4
64-
with:
65-
node-version: '18'
66-
registry-url: 'https://registry.npmjs.org'
67-
always-auth: true
166+
- name: Commit version changes (official release only)
167+
if: steps.config.outputs.PUBLISH_TYPE == 'official'
168+
run: |
169+
git config --local user.email "action@github.com"
170+
git config --local user.name "GitHub Action"
171+
git add package.json
172+
if git diff --staged --quiet; then
173+
echo "No version changes to commit"
174+
else
175+
git commit -m "chore: bump version to ${{ steps.config.outputs.VERSION }}"
176+
git push
177+
fi
68178
69179
- name: Clean and install dependencies
70180
run: |
@@ -74,52 +184,51 @@ jobs:
74184
- name: Build
75185
run: npm run build
76186

77-
- name: Publish to npm (official)
187+
- name: Publish to npm
78188
env:
79189
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
80190
run: |
81-
echo "Publishing official package to npm"
82-
npm publish --access public
191+
NPM_TAG="${{ steps.config.outputs.NPM_TAG }}"
192+
PACKAGE_NAME="${{ steps.config.outputs.PACKAGE_NAME }}"
193+
VERSION="${{ steps.config.outputs.VERSION }}"
194+
195+
echo "Publishing ${PACKAGE_NAME}@${VERSION} with tag=${NPM_TAG}"
196+
npm publish --tag "$NPM_TAG" --access public
83197
84-
publish-test:
85-
name: Publish Test Package
86-
needs: prepare
87-
runs-on: ubuntu-latest
88-
permissions:
89-
contents: read
90-
id-token: write
91-
if: (github.ref == 'refs/heads/main') || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_type == 'test')
92-
steps:
93-
- name: Checkout
94-
uses: actions/checkout@v4
95-
96-
- name: Setup Node.js
97-
uses: actions/setup-node@v4
98-
with:
99-
node-version: '18'
100-
registry-url: 'https://registry.npmjs.org'
101-
always-auth: true
102-
103-
- name: Clean and install dependencies
198+
- name: Create and push tag (test release only)
199+
if: steps.config.outputs.PUBLISH_TYPE == 'test'
104200
run: |
105-
rm -rf node_modules package-lock.json
106-
npm install
201+
PACKAGE_NAME="${{ steps.config.outputs.PACKAGE_NAME }}"
202+
VERSION="${{ steps.config.outputs.VERSION }}"
203+
# 简化包名作为 tag 前缀
204+
TAG_PREFIX=$(echo "$PACKAGE_NAME" | sed 's/@//g' | sed 's/\//-/g')
205+
TAG="${TAG_PREFIX}-v${VERSION}"
206+
207+
git config --local user.email "action@github.com"
208+
git config --local user.name "GitHub Action"
209+
git tag -a "$TAG" -m "Release test package version ${VERSION}" || true
210+
git push origin "$TAG" || true
211+
echo "Created and pushed tag: $TAG"
107212
108-
- name: Build
109-
run: npm run build
110-
111-
- name: Publish test package (tag=test)
112-
env:
113-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
114-
TEST_PKG_NAME: ${{ github.event.inputs.test_package_name }}
213+
- name: Summary
115214
run: |
116-
set -e
117-
echo "Publishing test package (tag=test)"
118-
if [ -n "$TEST_PKG_NAME" ]; then
119-
node -e "const fs=require('fs');const p=JSON.parse(fs.readFileSync('package.json'));p.name=process.env.TEST_PKG_NAME;fs.writeFileSync('package.json.tmp',JSON.stringify(p,null,2));fs.copyFileSync('package.json','package.json.bak');fs.copyFileSync('package.json.tmp','package.json');"
120-
npm publish --tag test --access public
121-
mv package.json.bak package.json
122-
rm -f package.json.tmp
215+
PUBLISH_TYPE="${{ steps.config.outputs.PUBLISH_TYPE }}"
216+
PACKAGE_NAME="${{ steps.config.outputs.PACKAGE_NAME }}"
217+
VERSION="${{ steps.config.outputs.VERSION }}"
218+
NPM_TAG="${{ steps.config.outputs.NPM_TAG }}"
219+
220+
if [ "$PUBLISH_TYPE" = "official" ]; then
221+
echo "## 🚀 Official Package Released!" >> $GITHUB_STEP_SUMMARY
123222
else
124-
npm publish --tag test --access public
223+
echo "## 🧪 Test Package Released!" >> $GITHUB_STEP_SUMMARY
125224
fi
225+
echo "" >> $GITHUB_STEP_SUMMARY
226+
echo "- **Package Name:** ${PACKAGE_NAME}" >> $GITHUB_STEP_SUMMARY
227+
echo "- **Version:** ${VERSION}" >> $GITHUB_STEP_SUMMARY
228+
echo "- **NPM Tag:** ${NPM_TAG}" >> $GITHUB_STEP_SUMMARY
229+
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
230+
echo "" >> $GITHUB_STEP_SUMMARY
231+
echo "Install with:" >> $GITHUB_STEP_SUMMARY
232+
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
233+
echo "npm install ${PACKAGE_NAME}@${VERSION}" >> $GITHUB_STEP_SUMMARY
234+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)