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