Skip to content

Commit cf1036f

Browse files
committed
ci: add test package publishing workflow
Add automated test package publishing job that runs on all branch pushes after tests pass. This creates a test version of the package with an incremented patch version and publishes it to npm with the 'test' tag. The workflow includes steps to determine the next version, update package.json with a test package name, build the package, and publish it as a test version for verification purposes. ci: 添加测试包发布工作流 添加自动化测试包发布作业,在所有分支推送且测试通过后运行。 这会创建一个具有递增补丁版本的测试包,并将其以 'test' 标签发布到 npm。 该工作流包括确定下一个版本的步骤,使用测试包名称更新 package.json, 构建包,并将其作为测试版本发布以供验证。 Change-Id: I38d8d008f29fa6885bf9d343e2af46073ef6b23b Signed-off-by: OhYee <[email protected]>
1 parent 3ad7613 commit cf1036f

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

.github/workflows/ci.yml

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name: CI
22

33
on:
44
push:
5-
branches: [main, master]
65
pull_request:
76
branches: [main, master]
87

@@ -65,3 +64,97 @@ jobs:
6564
token: ${{ secrets.CODECOV_TOKEN }}
6665
files: ./coverage/coverage-final.json
6766
fail_ci_if_error: false
67+
68+
# 自动发布测试包(所有分支 push 且测试通过时)
69+
publish-test:
70+
name: Publish Test Package
71+
runs-on: ubuntu-latest
72+
needs: [build, coverage]
73+
if: github.event_name == 'push'
74+
permissions:
75+
contents: write
76+
id-token: write
77+
steps:
78+
- name: Checkout
79+
uses: actions/checkout@v4
80+
with:
81+
fetch-depth: 0
82+
83+
- name: Setup Node.js
84+
uses: actions/setup-node@v4
85+
with:
86+
node-version: '18'
87+
registry-url: 'https://registry.npmjs.org'
88+
always-auth: true
89+
90+
- name: Determine version
91+
id: version
92+
run: |
93+
# 测试包名
94+
PACKAGE_NAME="@agentrun/sdk-test"
95+
echo "PACKAGE_NAME=${PACKAGE_NAME}" >> $GITHUB_OUTPUT
96+
97+
# 从 npm 获取当前最新版本
98+
NPM_RESPONSE=$(npm view "$PACKAGE_NAME" version 2>/dev/null || echo "")
99+
100+
if [ -z "$NPM_RESPONSE" ]; then
101+
CURRENT_VERSION="0.0.0"
102+
echo "Package not found on npm, starting from 0.0.0"
103+
else
104+
CURRENT_VERSION="$NPM_RESPONSE"
105+
echo "Latest version on npm: $CURRENT_VERSION"
106+
fi
107+
108+
# 解析版本号并递增 patch
109+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
110+
PATCH=$((PATCH + 1))
111+
VERSION="${MAJOR}.${MINOR}.${PATCH}"
112+
113+
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
114+
echo "New version: ${VERSION}"
115+
116+
- name: Update package.json for test publish
117+
run: |
118+
VERSION="${{ steps.version.outputs.VERSION }}"
119+
PACKAGE_NAME="${{ steps.version.outputs.PACKAGE_NAME }}"
120+
121+
node -e "
122+
const fs = require('fs');
123+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
124+
pkg.name = process.env.PACKAGE_NAME;
125+
pkg.version = process.env.VERSION;
126+
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
127+
"
128+
echo "Updated package.json:"
129+
cat package.json | head -10
130+
env:
131+
VERSION: ${{ steps.version.outputs.VERSION }}
132+
PACKAGE_NAME: ${{ steps.version.outputs.PACKAGE_NAME }}
133+
134+
- name: Clean and install dependencies
135+
run: |
136+
rm -rf node_modules package-lock.json
137+
npm install
138+
139+
- name: Build
140+
run: npm run build
141+
142+
- name: Publish test package
143+
env:
144+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
145+
run: |
146+
PACKAGE_NAME="${{ steps.version.outputs.PACKAGE_NAME }}"
147+
VERSION="${{ steps.version.outputs.VERSION }}"
148+
echo "Publishing ${PACKAGE_NAME}@${VERSION} with tag=test"
149+
npm publish --tag test --access public
150+
151+
- name: Summary
152+
run: |
153+
PACKAGE_NAME="${{ steps.version.outputs.PACKAGE_NAME }}"
154+
VERSION="${{ steps.version.outputs.VERSION }}"
155+
echo "## 🧪 Test Package Published!" >> $GITHUB_STEP_SUMMARY
156+
echo "" >> $GITHUB_STEP_SUMMARY
157+
echo "- **Package:** ${PACKAGE_NAME}@${VERSION}" >> $GITHUB_STEP_SUMMARY
158+
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
159+
echo "" >> $GITHUB_STEP_SUMMARY
160+
echo "Install: \`npm install ${PACKAGE_NAME}@${VERSION}\`" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)