-
Notifications
You must be signed in to change notification settings - Fork 420
208 lines (176 loc) · 7.9 KB
/
PR&Commit-npm_build_preview.yml
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
name: PR Merge Dev NPM Preview
# task:
# 1. 仅在主仓库中运行 (done)
# 2. 仅在pr合并时运行 (done)
# 3. 当pr合并时,获取pr合并后的commit id (done)
# 4. 根据commit id 修改package.json中的版本号和名称 (done)
# 5. 发布到npm (done)
# 6. 发布成功后,将发布的版本号和链接回复到当前pr下(done)
# 7. 发布成功后,将发布的版本号和链接回复到本repo的相关issue中 (done)
# 8. 修改README.md —— 提示dev版本需谨慎使用 (done)
on:
push:
pull_request_target:
types:
- closed
paths: # 这里是用来指定哪个文件更改,才会触发的
- "src/**"
- "package.json"
- "types/**"
- "examples/**"
permissions:
pull-requests: write
issues: write
jobs:
dev-deploy:
# 不需要在fork仓库的pr中运行, 仅当pr合并时运行
if: github.repository == 'Tencent/cherry-markdown' && github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
# 检出仓库代码
- name: Checkout repository
uses:
actions/checkout@v4
# 获取PR合并后的commit id 或者 push commit id
- name: Get Merge Commit SHA or Push Commit SHA
run: |
if [ "${{ github.event.pull_request.merged }}" == "true" ]; then
SHORT_SHA=$(echo "${{ github.event.pull_request.merge_commit_sha }}" | cut -c1-7)
echo "COMMIT_SHORT_SHA=$SHORT_SHA" >> "$GITHUB_ENV"
else
if [ -n "${{ github.event.head_commit.id }}" ]; then
SHORT_SHA=$(echo "${{ github.event.head_commit.id }}" | cut -c1-7)
echo "COMMIT_SHORT_SHA=$SHORT_SHA" >> "$GITHUB_ENV"
else
echo "Not a merged PR and no push commit found, skipping"
fi
fi
# 根据commit id 修改package.json中的版本号和名称
- name: dev package version and name
run: |
if [ -f package.json ]; then
# 获取当前版本号并添加 Merge Commit SHA or Push Commit SHA
VERSION=$(node -p "require('./package.json').version")-dev.${{ env.COMMIT_SHORT_SHA }}
# 设置环境变量
echo "PACKAGE_VERSION=$VERSION" >> "$GITHUB_ENV"
package_name="@cherry-markdown-publisher/preview-dev"
echo "PACKAGE_NAME=$package_name" >> "$GITHUB_ENV"
# 打印当前版本号
echo "Current version: $PACKAGE_VERSION"
# 修改 package.json 中的 name 和 version,并检查 scripts 中是否存在 publish 属性,如果存在则移除
jq --arg package_name "$package_name" --arg package_version "$VERSION" '
.name=$package_name |
.version=$package_version |
if .scripts.publish then del(.scripts.publish) else . end
' package.json > temp.json && mv temp.json package.json
# 打印修改后的 name 和 version
echo "Updated package.json:"
cat package.json | jq '.name, .version'
echo "$PACKAGE_VERSION"
# 检查 scripts 中是否存在 publish 属性,如果存在则移除
if jq -e '.scripts.publish' package.json > /dev/null; then
jq 'del(.scripts.publish)' package.json > temp.json && mv temp.json package.json
fi
else
echo "package.json 文件不存在"
fi
# 重写或创建 README.md 文件
echo -e '<p align="center"><img src="logo/new_logo.png" alt="cherry logo" width="50%"/></p>\n' > README.md
echo -e '# Cherry Markdown Writer\n' >> README.md
echo -e '> !WARNING\n This is a dev preview version of `Cherry Markdown`, please use it with caution. No responsibility for production versions.\n' >> README.md
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: "18"
registry-url: https://registry.npmjs.org/
- name: yarn install
run: |
npm i yarn -g
yarn install
- name: build
run: yarn build
- name: npm publish
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Comment on related issues
id: get-issues
uses: actions/github-script@v7
with:
result-encoding: string
retries: 3
retry-exempt-status-codes: 400,401
script: |
let issueArray = [];
// 添加issue到数组并去重
const addIssueToArray = (newIssues) => {
issueArray = [...new Set([...issueArray, ...newIssues])];
};
// 获取issue编号并转换成数字数组
const getIssueRegex = (issueText) => {
const issueRegex = /#(\d+)/g;
return Array.from(issueText?.matchAll(issueRegex), match => parseInt(match[1], 10));
};
const comment = `谢谢您的大力支持,请安装和此相关的测试版进行极速体验:
\`\`\`shell
npm install ${process.env.PACKAGE_NAME}@${process.env.PACKAGE_VERSION}
\`\`\`
[查看npm发布版本](https://www.npmjs.com/package/${process.env.PACKAGE_NAME}/v/${process.env.PACKAGE_VERSION}),请注意这是一个开发预览版本,请谨慎使用!`;
// 发布评论到issue
const createComment = async (issueNumbers) => {
console.log('issueNumbers', issueNumbers);
for (const issueNumber of issueNumbers) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: comment
});
}
};
try {
// 获取当前PR的issue信息
const processPullRequest = async (pull_number) => {
issueArray.push(pull_number);
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_number
});
addIssueToArray(getIssueRegex(pullRequest.title));
addIssueToArray(getIssueRegex(pullRequest.body));
// 获取当前PR的issue评论信息
const { data: issueComments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull_number
});
issueComments.forEach(comment => {
addIssueToArray(getIssueRegex(comment.body));
});
// 获取当前PR的review评论信息
const { data: reviewComments } = await github.rest.pulls.listReviewComments({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_number
});
reviewComments.forEach(comment => {
addIssueToArray(getIssueRegex(comment.body));
});
};
// 获取commit信息
const processCommits = () => {
context.payload.commits.forEach(commit => {
addIssueToArray(getIssueRegex(commit.message));
});
};
// 判断当前PR是否有issue 或者 commit是否有issue
if (context.payload.pull_request) {
await processPullRequest(context.issue.number);
} else if (context.payload.commits) {
processCommits();
}
await createComment(issueArray);
} catch (error) {
console.log('error', error);
}