-
Notifications
You must be signed in to change notification settings - Fork 91
149 lines (123 loc) · 4.67 KB
/
Copy pathbuild-image-preview.yml
File metadata and controls
149 lines (123 loc) · 4.67 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
name: Preview build image
on:
workflow_dispatch:
pull_request_target:
branches: ['*']
permissions:
contents: read
packages: write
attestations: write
id-token: write
pull-requests: write
jobs:
preview-image:
runs-on: ubuntu-24.04
steps:
- name: Get PR changed files via API
id: pr-files
run: |
PR_NUMBER=${{ github.event.number }}
REPO=${{ github.repository }}
TOKEN=${{ secrets.GITHUB_TOKEN }}
RESPONSE=$(curl -s -H "Authorization: token $TOKEN" \
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/files")
FILES=$(echo "$RESPONSE" | jq -r '.[].filename')
echo "All changed files:"
echo "$FILES"
PACKAGE_CHANGES=""
# Extract package names from changed files
for file in $FILES; do
if [[ "$file" == modules/tool/packages/* ]]; then
echo "Found package file: $file"
# Extract package directory name
PACKAGE_NAME=$(echo "$file" | cut -d'/' -f4)
echo "Found package directory: $PACKAGE_NAME"
# Add to package changes if not already present
if [[ ! " $PACKAGE_CHANGES " =~ " $PACKAGE_NAME " ]]; then
if [ -z "$PACKAGE_CHANGES" ]; then
PACKAGE_CHANGES="$PACKAGE_NAME"
else
PACKAGE_CHANGES="$PACKAGE_CHANGES $PACKAGE_NAME"
fi
fi
fi
done
echo "Package changes detected: $PACKAGE_CHANGES"
echo "package_changes=$PACKAGE_CHANGES" >> $GITHUB_OUTPUT
- name: Set sparse checkout environment variable
run: |
echo "SPARSE_CHECKOUT_CONFIG<<EOF" >> $GITHUB_ENV
echo "/*" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
sparse-checkout-cone-mode: false
sparse-checkout: ${{ env.SPARSE_CHECKOUT_CONFIG }}
- name: Setup bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.2.23
- name: Install dependencies
run: bun install --frozen-lockfile && bun run install:plugins
- name: Build pkg
run: bun run build:pkg
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Aliyun Container Registry
uses: docker/login-action@v3
with:
registry: registry.cn-hangzhou.aliyuncs.com
username: ${{ secrets.ALI_HUB_USERNAME }}
password: ${{ secrets.ALI_HUB_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64
push: true
tags: |
${{ secrets.ALI_IMAGE_NAME }}/fastgpt-plugin-pr:${{ github.event.pull_request.head.sha }}
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.description=https://github.com/${{ github.repository }} image
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
- name: Add PR comment on success
if: success()
uses: actions/github-script@v6
with:
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existingComment = comments.find(comment =>
comment.body.includes('Preview Images for this PR') &&
comment.user.type === 'Bot'
);
const commentBody = `
✅ **Build Successful** - Preview Images for this PR:
\`\`\`
${{ secrets.ALI_IMAGE_NAME }}/fastgpt-plugin-pr:${{ github.event.pull_request.head.sha }}
\`\`\`
**Changed packages:**
${{ steps.pr-files.outputs.package_changes || 'None' }}
`;
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: commentBody
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody
});
}