-
Notifications
You must be signed in to change notification settings - Fork 0
130 lines (113 loc) · 4.18 KB
/
release.yml
File metadata and controls
130 lines (113 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
name: Release
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: '发布版本 (例如: 1.0.0)'
required: true
type: string
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
id-token: write
steps:
- name: 检出代码
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: 设置 Node.js
uses: actions/setup-node@v4
with:
node-version: 22
registry-url: 'https://registry.npmjs.org'
- name: 安装 pnpm
uses: pnpm/action-setup@v4
with:
version: 10
- name: 获取 pnpm store 目录
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: 设置 pnpm 缓存
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: 安装依赖
run: |
# 尝试使用 frozen lockfile,如果失败则更新 lockfile
pnpm install --frozen-lockfile || {
echo "⚠️ Lockfile 不匹配,正在更新..."
pnpm install --no-frozen-lockfile
}
- name: 格式检查
run: pnpm run lint
- name: 构建项目
run: pnpm run build
- name: 获取版本号
id: get_version
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
echo "tag_name=v${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
else
echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
echo "tag_name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
fi
- name: 更新版本号 (手动触发时)
if: github.event_name == 'workflow_dispatch'
run: |
# 检查 tag 是否已经存在
if git show-ref --tags --verify --quiet "refs/tags/${{ steps.get_version.outputs.tag_name }}"; then
echo "⚠️ Tag ${{ steps.get_version.outputs.tag_name }} 已存在,跳过创建"
else
npm version ${{ steps.get_version.outputs.version }} --no-git-tag-version
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add package.json
git commit -m "chore: bump version to ${{ steps.get_version.outputs.version }}"
git tag ${{ steps.get_version.outputs.tag_name }}
git push origin HEAD:${{ github.ref_name }}
git push origin ${{ steps.get_version.outputs.tag_name }}
fi
- name: 生成变更日志
id: changelog
run: npx changelogithub
continue-on-error: true
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: 发布到 npm
run: |
# 检查版本是否已经发布到 npm
PACKAGE_NAME=$(node -p "require('./package.json').name")
CURRENT_VERSION=$(node -p "require('./package.json').version")
# 尝试获取 npm 上的版本信息
if npm view "${PACKAGE_NAME}@${CURRENT_VERSION}" version 2>/dev/null; then
echo "⚠️ 版本 ${CURRENT_VERSION} 已存在于 npm,跳过发布"
else
echo "📦 发布版本 ${CURRENT_VERSION} 到 npm..."
pnpm publish --no-git-checks
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create GitHub Release
uses: ncipollo/release-action@v1
with:
generateReleaseNotes: "true"
allowUpdates: true
skipIfReleaseExists: false
omitBodyDuringUpdate: false
- name: 通知发布成功
run: |
echo "🎉 发布成功!"
echo "📦 版本: ${{ steps.get_version.outputs.version }}"
echo "🏷️ 标签: ${{ steps.get_version.outputs.tag_name }}"
echo "📝 npm: https://www.npmjs.com/package/@winner-fed/plugin-check-syntax"