Skip to content

Commit d9f6893

Browse files
authored
fix: action (#260)
1 parent d748535 commit d9f6893

File tree

1 file changed

+89
-42
lines changed

1 file changed

+89
-42
lines changed

.github/workflows/build-image-preview.yml

Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,69 +24,72 @@ jobs:
2424
REPO=${{ github.repository }}
2525
TOKEN=${{ secrets.GITHUB_TOKEN }}
2626
27-
# 获取 PR 修改的文件列表
2827
RESPONSE=$(curl -s -H "Authorization: token $TOKEN" \
2928
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/files")
3029
31-
# 提取文件路径
3230
FILES=$(echo "$RESPONSE" | jq -r '.[].filename')
3331
3432
echo "All changed files:"
3533
echo "$FILES"
3634
37-
# 检查是否有相关文件需要构建
3835
HAS_RELEVANT_CHANGES="false"
3936
40-
# 检查是否有根目录文件或配置文件变更
37+
# Check for relevant changes
4138
for file in $FILES; do
42-
case "$file" in
43-
"package.json"|"bun.lockb"|".github/workflows/"*"|"bun.lock"|yarn.lock|"pnpm-lock.yaml")
44-
echo "Found relevant root file: $file"
45-
HAS_RELEVANT_CHANGES="true"
46-
;;
47-
"modules/tool/packages/"*)
48-
echo "Found package file: $file"
49-
HAS_RELEVANT_CHANGES="true"
50-
;;
51-
esac
39+
if [ "$file" = "package.json" ]; then
40+
echo "Found package.json"
41+
HAS_RELEVANT_CHANGES="true"
42+
elif [ "$file" = "bun.lock" ]; then
43+
echo "Found bun.lock"
44+
HAS_RELEVANT_CHANGES="true"
45+
elif [ "$file" = "bun.lockb" ]; then
46+
echo "Found bun.lockb"
47+
HAS_RELEVANT_CHANGES="true"
48+
elif [[ "$file" == .github/workflows/* ]]; then
49+
echo "Found workflow file: $file"
50+
HAS_RELEVANT_CHANGES="true"
51+
elif [[ "$file" == modules/tool/packages/* ]]; then
52+
echo "Found package file: $file"
53+
HAS_RELEVANT_CHANGES="true"
54+
fi
5255
done
5356
54-
# 提取在 /modules/tool/packages 下变更的包目录
57+
# Extract package names from changed files
5558
PACKAGE_CHANGES=""
5659
for file in $FILES; do
5760
if [[ "$file" == modules/tool/packages/* ]]; then
58-
# 提取包名(modules/tool/packages/package-name/...)
59-
PACKAGE_NAME=$(echo "$file" | sed 's|^modules/tool/packages/[^/]*/||' | cut -d'/' -f1)
60-
if [[ -n "$PACKAGE_NAME" ]]; then
61-
# 检查是否已经添加过这个包
62-
if ! echo "$PACKAGE_CHANGES" | grep -q "$PACKAGE_NAME"; then
63-
if [ -z "$PACKAGE_CHANGES" ]; then
64-
PACKAGE_CHANGES="$PACKAGE_NAME"
65-
else
66-
PACKAGE_CHANGES="$PACKAGE_CHANGES $PACKAGE_NAME"
67-
fi
61+
# Extract package directory name (first segment after packages/)
62+
PACKAGE_NAME=$(echo "$file" | cut -d'/' -f4)
63+
echo "Found package directory: $PACKAGE_NAME"
64+
65+
# Check if we already have this package
66+
if echo "$PACKAGE_CHANGES" | grep -vqw "$PACKAGE_NAME"; then
67+
if [ -z "$PACKAGE_CHANGES" ]; then
68+
PACKAGE_CHANGES="$PACKAGE_NAME"
69+
else
70+
PACKAGE_CHANGES="$PACKAGE_CHANGES $PACKAGE_NAME"
6871
fi
6972
fi
7073
fi
7174
done
7275
73-
# 构建稀疏检出配置
76+
# Build sparse checkout configuration
7477
SPARSE_CONFIG="/*"
7578
SPARSE_CONFIG="$SPARSE_CONFIG"$'\n'"!/modules/tool/packages"
7679
77-
# 为每个变更的包添加包含规则
80+
# Add changed packages to sparse checkout
7881
for package in $PACKAGE_CHANGES; do
7982
if [ -n "$package" ]; then
8083
SPARSE_CONFIG="$SPARSE_CONFIG"$'\n'"/modules/tool/packages/$package"
81-
echo "Adding package to sparse checkout: modules/tool/packages/$package"
84+
echo "Adding to sparse checkout: modules/tool/packages/$package"
8285
fi
8386
done
8487
85-
# 总是需要包含根目录的关键文件
88+
# Always include essential files
8689
SPARSE_CONFIG="$SPARSE_CONFIG"$'\n'"package.json"
8790
SPARSE_CONFIG="$SPARSE_CONFIG"$'\n'"bun.lockb"
8891
89-
# 保存到输出
92+
# Output results for GitHub Actions
9093
{
9194
echo "files<<EOF"
9295
echo "$FILES"
@@ -169,17 +172,61 @@ jobs:
169172
cache-from: type=local,src=/tmp/.buildx-cache
170173
cache-to: type=local,dest=/tmp/.buildx-cache
171174

172-
- name: '@finleyge/github-tools'
173-
uses: FinleyGe/[email protected]
174-
id: print-image-label
175-
if: success()
175+
- name: Add PR comment on success
176+
if: steps.pr-files.outputs.has_relevant_changes == 'true' && success()
177+
uses: actions/github-script@v6
176178
with:
177-
token: ${{ secrets.GITHUB_TOKEN }}
178-
tool: issue-comment
179-
title: Preview Image Label
180-
body: |
181-
Preview Images for this PR:
182-
183-
```
179+
script: |
180+
const { data: comments } = await github.rest.issues.listComments({
181+
owner: context.repo.owner,
182+
repo: context.repo.repo,
183+
issue_number: context.issue.number,
184+
});
185+
186+
const existingComment = comments.find(comment =>
187+
comment.body.includes('Preview Images for this PR') &&
188+
comment.user.type === 'Bot'
189+
);
190+
191+
const commentBody = `
192+
✅ **Build Successful** - Preview Images for this PR:
193+
194+
\`\`\`
184195
${{ secrets.ALI_IMAGE_NAME }}/fastgpt-plugin-pr:${{ github.event.pull_request.head.sha }}
185-
```
196+
\`\`\`
197+
198+
**Changed packages:**
199+
${{ steps.pr-files.outputs.package_changes || 'None' }}
200+
`;
201+
202+
if (existingComment) {
203+
await github.rest.issues.updateComment({
204+
owner: context.repo.owner,
205+
repo: context.repo.repo,
206+
comment_id: existingComment.id,
207+
body: commentBody
208+
});
209+
} else {
210+
await github.rest.issues.createComment({
211+
owner: context.repo.owner,
212+
repo: context.repo.repo,
213+
issue_number: context.issue.number,
214+
body: commentBody
215+
});
216+
}
217+
218+
- name: Add PR comment on skip
219+
if: steps.pr-files.outputs.has_relevant_changes == 'false'
220+
uses: actions/github-script@v6
221+
with:
222+
script: |
223+
await github.rest.issues.createComment({
224+
owner: context.repo.owner,
225+
repo: context.repo.repo,
226+
issue_number: context.issue.number,
227+
body: `
228+
ℹ️ **Build Skipped** - No relevant changes found in this PR
229+
230+
This PR doesn't contain changes that require rebuilding (no package.json, bun.lockb, or plugin package changes).
231+
`
232+
});

0 commit comments

Comments
 (0)