Skip to content

Commit 068bece

Browse files
authored
ci: add auto publish (#157)
1 parent 11aa489 commit 068bece

2 files changed

Lines changed: 202 additions & 0 deletions

File tree

.github/workflows/auto-publish.yml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Auto Publish
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
workflow_dispatch:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: CheckOut Code
15+
uses: actions/checkout@master
16+
with:
17+
ref: ${{ github.ref_name }}
18+
19+
- name: Setup pnpm
20+
uses: pnpm/action-setup@v2
21+
22+
- name: Setup Node
23+
uses: actions/setup-node@v3
24+
with:
25+
node-version: 20.10.0
26+
registry-url: "https://registry.npmjs.org"
27+
28+
- name: Get pnpm store directory
29+
id: pnpm-cache
30+
run: |
31+
echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT
32+
33+
- uses: actions/cache@v3
34+
name: Setup pnpm cache
35+
with:
36+
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
37+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
38+
restore-keys: |
39+
${{ runner.os }}-pnpm-store-
40+
- name: Install dependencies
41+
run: pnpm i --no-frozen-lockfile
42+
- name: Build
43+
run: |
44+
pnpm build
45+
- name: Upload build artifact
46+
uses: actions/upload-artifact@v4
47+
with:
48+
name: dist-artifact
49+
path: dist/
50+
# Publish job
51+
publish:
52+
needs: build
53+
runs-on: ubuntu-latest
54+
steps:
55+
- name: Download build artifact
56+
uses: actions/download-artifact@v4
57+
with:
58+
name: dist-artifact
59+
path: dist/
60+
- name: Parse Publish tag
61+
id: parse_tag
62+
run: |
63+
tag_name="${GITHUB_REF#refs/tags/}"
64+
if [[ "$tag_name" == *alpha* ]]; then
65+
echo "dist_tag=alpha" >> "$GITHUB_OUTPUT"
66+
elif [[ "$tag_name" == *beta* ]]; then
67+
echo "dist_tag=beta" >> "$GITHUB_OUTPUT"
68+
elif [[ "$tag_name" == *rc* ]]; then
69+
echo "dist_tag=rc" >> "$GITHUB_OUTPUT"
70+
else
71+
echo "dist_tag=latest" >> "$GITHUB_OUTPUT"
72+
fi
73+
- name: Show version and tag
74+
run: |
75+
VERSION="$(node -p "require('./package.json').version")"
76+
echo "publish version: $VERSION"
77+
echo "publish tag: ${{ steps.parse_tag.outputs.dist_tag }}"
78+
- name: Publish @opentiny/fluent-editor
79+
run: |
80+
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" > ~/.npmrc
81+
npm publish --tag ${{ steps.parse_tag.outputs.dist_tag }}
82+
env:
83+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
84+
85+
- name: Release
86+
if: ${{ steps.parse_tag.outputs.dist_tag == 'latest' }}
87+
uses: softprops/action-gh-release@v1
88+
with:
89+
tag_name: ${{ github.ref_name }}
90+
generate_release_notes: true
91+
env:
92+
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Dispatch Publish
2+
3+
# 触发条件配置
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
version:
8+
description: 输入您将要发布的版本号(默认使用 package.json 中的版本号),例如:`0.1.0`。
9+
required: false
10+
type: string
11+
tag:
12+
description: "选择发布/部署版本tag (alpha/beta/rc/latest)"
13+
required: true
14+
default: "alpha"
15+
type: choice
16+
options:
17+
- alpha
18+
- beta
19+
- rc
20+
- latest
21+
22+
concurrency:
23+
group: ${{ github.workflow }}-${{ github.sha }}
24+
cancel-in-progress: true
25+
26+
# 定义工作流中的作业
27+
jobs:
28+
build:
29+
# 指定运行环境为最新版本的ubuntu
30+
runs-on: ubuntu-latest
31+
outputs:
32+
version: ${{ steps.ver.outputs.value }}
33+
steps:
34+
# 步骤1: 检出代码
35+
- name: CheckOut Code
36+
uses: actions/checkout@v4
37+
with:
38+
ref: ${{ github.ref_name }}
39+
40+
# 步骤2: 设置pnpm包管理器
41+
- name: Setup pnpm
42+
uses: pnpm/action-setup@v4
43+
44+
# 步骤3: 设置Node.js环境
45+
- name: Setup Node
46+
uses: actions/setup-node@v4
47+
with:
48+
node-version: 20 # 使用Node.js 20版本
49+
registry-url: "https://registry.npmjs.org" # 设置npm registry地址
50+
51+
# 步骤4: 获取pnpm缓存目录路径
52+
- name: Get pnpm store directory
53+
id: pnpm-cache
54+
run: |
55+
echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT
56+
57+
# 步骤5: 配置pnpm缓存
58+
- uses: actions/cache@v3
59+
name: Setup pnpm cache
60+
with:
61+
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
62+
# 使用操作系统类型和pnpm-lock.yaml的哈希值作为缓存键
63+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
64+
restore-keys: |
65+
${{ runner.os }}-pnpm-store-
66+
67+
# 步骤6: 安装项目依赖
68+
- name: Install dependencies
69+
run: pnpm i --no-frozen-lockfile
70+
- name: Get version
71+
id: ver
72+
run: |
73+
# 优先用手动输入的版本号
74+
if [ -n "${{ inputs.version }}" ]; then
75+
VERSION="${{ inputs.version }}"
76+
else
77+
VERSION="$(node -p "require('./package.json').version")"
78+
fi
79+
echo "version: $VERSION"
80+
echo "value=$VERSION" >> $GITHUB_OUTPUT
81+
82+
- name: Build lib
83+
run: pnpm build
84+
- name: Upload build artifact
85+
uses: actions/upload-artifact@v4
86+
with:
87+
name: dist-artifact
88+
path: dist/
89+
# Publish job
90+
publish:
91+
needs: build
92+
runs-on: ubuntu-latest
93+
steps:
94+
- name: Download build artifact
95+
uses: actions/download-artifact@v4
96+
with:
97+
name: dist-artifact
98+
path: dist/
99+
- name: Show version and tag
100+
run: |
101+
echo "publish version: ${{ needs.build.outputs.version }}"
102+
echo "publish tag: ${{ inputs.tag }}"
103+
104+
# 步骤8: 发布组件到NPM
105+
- name: Publish @opentiny/fluent-editor
106+
run: |
107+
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" > ~/.npmrc
108+
npm publish --tag ${{ inputs.tag }}
109+
env:
110+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 commit comments

Comments
 (0)