Skip to content

Commit f382f81

Browse files
committed
action
1 parent 5e8fc78 commit f382f81

File tree

4 files changed

+110
-99
lines changed

4 files changed

+110
-99
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build Docs Preview (Unprivileged)
1+
name: Preview docs build
22

33
on:
44
pull_request:
@@ -34,6 +34,12 @@ jobs:
3434
id: datetime
3535
run: echo "datetime=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT
3636

37+
- name: Save PR metadata
38+
run: |
39+
mkdir -p /tmp/pr-metadata
40+
echo "${{ github.event.pull_request.number }}" > /tmp/pr-metadata/pr-number.txt
41+
echo "${{ github.event.pull_request.head.sha }}" > /tmp/pr-metadata/pr-sha.txt
42+
3743
- name: Set up Docker Buildx
3844
uses: docker/setup-buildx-action@v3
3945

@@ -58,5 +64,21 @@ jobs:
5864
path: /tmp/fastgpt-docs-${{ steps.datetime.outputs.datetime }}.tar
5965
retention-days: 1
6066

67+
- name: Upload PR metadata
68+
uses: actions/upload-artifact@v4
69+
with:
70+
name: pr-metadata-docs-${{ steps.datetime.outputs.datetime }}
71+
path: /tmp/pr-metadata/
72+
retention-days: 1
73+
74+
call-push-workflow:
75+
needs: build-docs-image
76+
uses: ./.github/workflows/preview-docs-push.yml
77+
secrets: inherit
78+
with:
79+
pr_number: ${{ github.event.pull_request.number }}
80+
datetime: ${{ needs.build-docs-image.outputs.datetime }}
81+
run_id: ${{ github.run_id }}
82+
6183
outputs:
6284
datetime: ${{ steps.datetime.outputs.datetime }}

.github/workflows/preview-docs-push.yml

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,58 @@
1-
name: Deploy Docs Preview (Privileged)
1+
name: Preview docs push
22

33
on:
4-
workflow_run:
5-
workflows: ["Build Docs Preview (Unprivileged)"]
6-
types: [completed]
7-
branches:
8-
- '**' # 监听所有分支
4+
workflow_call:
5+
inputs:
6+
pr_number:
7+
required: true
8+
type: string
9+
datetime:
10+
required: true
11+
type: string
12+
run_id:
13+
required: true
14+
type: string
915

1016
permissions:
1117
contents: read
1218
packages: write
1319
attestations: write
1420
id-token: write
1521
pull-requests: write
16-
issues: write # Required for issue-comment (PR comments use Issues API)
22+
issues: write # Required for issue-comment (PR comments use Issues API)
1723

1824
jobs:
1925
push-and-deploy:
20-
if: ${{ github.event.workflow_run.conclusion == 'success' }}
2126
runs-on: ubuntu-24.04
2227

2328
steps:
24-
- name: Get PR information
25-
id: pr
26-
uses: actions/github-script@v7
29+
- name: Download PR metadata
30+
uses: actions/download-artifact@v4
2731
with:
28-
script: |
29-
// 获取触发工作流的 PR 信息
30-
const { data: pullRequests } = await github.rest.pulls.list({
31-
owner: context.repo.owner,
32-
repo: context.repo.repo,
33-
state: 'open',
34-
});
35-
36-
// 查找匹配的 PR(支持 fork 仓库)
37-
const pr = pullRequests.find(pr =>
38-
pr.head.ref === context.payload.workflow_run.head_branch &&
39-
pr.head.sha === context.payload.workflow_run.head_sha
40-
);
41-
42-
if (!pr) {
43-
core.setFailed('No open PR found for this branch and commit');
44-
return;
45-
}
46-
47-
core.setOutput('number', pr.number);
48-
49-
- name: Get workflow artifacts
50-
uses: actions/github-script@v7
32+
name: pr-metadata-docs-${{ inputs.datetime }}
33+
path: /tmp/pr-metadata/
34+
run-id: ${{ inputs.run_id }}
35+
github-token: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Read PR information
38+
id: pr
39+
run: |
40+
PR_NUMBER="${{ inputs.pr_number }}"
41+
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
42+
echo "Found PR #$PR_NUMBER"
43+
44+
- name: Get artifact name
5145
id: artifacts
52-
with:
53-
script: |
54-
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
55-
owner: context.repo.owner,
56-
repo: context.repo.repo,
57-
run_id: context.payload.workflow_run.id,
58-
});
59-
60-
const artifact = artifacts.data.artifacts[0];
61-
if (!artifact) {
62-
core.setFailed('No artifact found');
63-
return;
64-
}
65-
66-
// Extract datetime from artifact name
67-
const datetime = artifact.name.replace('fastgpt-docs-', '');
68-
core.setOutput('datetime', datetime);
69-
core.setOutput('artifact_name', artifact.name);
46+
run: |
47+
echo "datetime=${{ inputs.datetime }}" >> $GITHUB_OUTPUT
48+
echo "artifact_name=fastgpt-docs-${{ inputs.datetime }}" >> $GITHUB_OUTPUT
7049
7150
- name: Download image artifact
7251
uses: actions/download-artifact@v4
7352
with:
7453
name: ${{ steps.artifacts.outputs.artifact_name }}
7554
path: /tmp/
76-
run-id: ${{ github.event.workflow_run.id }}
55+
run-id: ${{ inputs.run_id }}
7756
github-token: ${{ secrets.GITHUB_TOKEN }}
7857

7958
- name: Load Docker image

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: FastGPT Build (Unprivileged)
1+
name: Preview fastgpt build
22

33
on:
44
pull_request:
@@ -33,6 +33,12 @@ jobs:
3333
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}
3434
fetch-depth: 0
3535

36+
- name: Save PR metadata
37+
run: |
38+
mkdir -p /tmp/pr-metadata
39+
echo "${{ github.event.pull_request.number }}" > /tmp/pr-metadata/pr-number.txt
40+
echo "${{ github.event.pull_request.head.sha }}" > /tmp/pr-metadata/pr-sha.txt
41+
3642
- name: Set up Docker Buildx
3743
uses: docker/setup-buildx-action@v3
3844
with:
@@ -88,3 +94,24 @@ jobs:
8894
name: ${{ steps.config.outputs.IMAGE_NAME }}-${{ github.sha }}
8995
path: /tmp/${{ steps.config.outputs.IMAGE_NAME }}-${{ github.sha }}.tar
9096
retention-days: 1
97+
98+
- name: Upload PR metadata
99+
uses: actions/upload-artifact@v4
100+
with:
101+
name: pr-metadata-${{ matrix.image }}-${{ github.sha }}
102+
path: /tmp/pr-metadata/
103+
retention-days: 1
104+
105+
call-push-workflow:
106+
needs: build-preview-images
107+
strategy:
108+
matrix:
109+
image: [fastgpt, sandbox, mcp_server]
110+
fail-fast: false
111+
uses: ./.github/workflows/preview-fastgpt-push.yml
112+
secrets: inherit
113+
with:
114+
pr_number: ${{ github.event.pull_request.number }}
115+
pr_sha: ${{ github.sha }}
116+
run_id: ${{ github.run_id }}
117+
image: ${{ matrix.image }}
Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,55 @@
1-
name: FastGPT Push (Privileged)
1+
name: Preview fastgpt push
22

33
on:
4-
workflow_run:
5-
workflows: ["FastGPT Build (Unprivileged)"]
6-
types: [completed]
7-
branches:
8-
- '**' # 监听所有分支
4+
workflow_call:
5+
inputs:
6+
pr_number:
7+
required: true
8+
type: string
9+
pr_sha:
10+
required: true
11+
type: string
12+
run_id:
13+
required: true
14+
type: string
15+
image:
16+
required: true
17+
type: string
918

1019
jobs:
1120
push-preview-images:
12-
# 只在构建成功时运行
13-
if: ${{ github.event.workflow_run.conclusion == 'success' }}
14-
1521
permissions:
1622
contents: read
1723
packages: write
1824
attestations: write
1925
id-token: write
2026
pull-requests: write
21-
issues: write # Required for issue-comment (PR comments use Issues API)
27+
issues: write # Required for issue-comment (PR comments use Issues API)
2228

2329
runs-on: ubuntu-24.04
24-
strategy:
25-
matrix:
26-
image: [fastgpt, sandbox, mcp_server]
27-
fail-fast: false
2830

2931
steps:
30-
- name: Get PR information
32+
- name: Read PR information
3133
id: pr
32-
uses: actions/github-script@v7
33-
with:
34-
script: |
35-
// 获取触发工作流的 PR 信息
36-
const { data: pullRequests } = await github.rest.pulls.list({
37-
owner: context.repo.owner,
38-
repo: context.repo.repo,
39-
state: 'open',
40-
});
41-
42-
// 查找匹配的 PR(支持 fork 仓库)
43-
const pr = pullRequests.find(pr =>
44-
pr.head.ref === context.payload.workflow_run.head_branch &&
45-
pr.head.sha === context.payload.workflow_run.head_sha
46-
);
47-
48-
if (!pr) {
49-
core.setFailed('No open PR found for this branch and commit');
50-
return;
51-
}
52-
53-
core.setOutput('number', pr.number);
54-
core.setOutput('sha', context.payload.workflow_run.head_sha);
34+
run: |
35+
echo "number=${{ inputs.pr_number }}" >> $GITHUB_OUTPUT
36+
echo "sha=${{ inputs.pr_sha }}" >> $GITHUB_OUTPUT
37+
echo "Found PR #${{ inputs.pr_number }} (SHA: ${{ inputs.pr_sha }})"
5538
5639
- name: Set image config
5740
id: config
5841
run: |
5942
SHA="${{ steps.pr.outputs.sha }}"
6043
61-
if [[ "${{ matrix.image }}" == "fastgpt" ]]; then
44+
if [[ "${{ inputs.image }}" == "fastgpt" ]]; then
6245
echo "IMAGE_NAME=fastgpt" >> $GITHUB_OUTPUT
6346
echo "DESCRIPTION=fastgpt-pr image" >> $GITHUB_OUTPUT
6447
echo "DOCKER_REPO_TAGGED=${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-pr:fastgpt_${SHA}" >> $GITHUB_OUTPUT
65-
elif [[ "${{ matrix.image }}" == "sandbox" ]]; then
48+
elif [[ "${{ inputs.image }}" == "sandbox" ]]; then
6649
echo "IMAGE_NAME=fastgpt-sandbox" >> $GITHUB_OUTPUT
6750
echo "DESCRIPTION=fastgpt-sandbox-pr image" >> $GITHUB_OUTPUT
6851
echo "DOCKER_REPO_TAGGED=${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-pr:fastgpt_sandbox_${SHA}" >> $GITHUB_OUTPUT
69-
elif [[ "${{ matrix.image }}" == "mcp_server" ]]; then
52+
elif [[ "${{ inputs.image }}" == "mcp_server" ]]; then
7053
echo "IMAGE_NAME=fastgpt-mcp-server" >> $GITHUB_OUTPUT
7154
echo "DESCRIPTION=fastgpt-mcp_server-pr image" >> $GITHUB_OUTPUT
7255
echo "DOCKER_REPO_TAGGED=${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-pr:fastgpt_mcp_server_${SHA}" >> $GITHUB_OUTPUT
@@ -77,7 +60,7 @@ jobs:
7760
with:
7861
name: ${{ steps.config.outputs.IMAGE_NAME }}-${{ steps.pr.outputs.sha }}
7962
path: /tmp/
80-
run-id: ${{ github.event.workflow_run.id }}
63+
run-id: ${{ inputs.run_id }}
8164
github-token: ${{ secrets.GITHUB_TOKEN }}
8265

8366
- name: Load Docker image
@@ -115,7 +98,7 @@ jobs:
11598
token: ${{ secrets.GITHUB_TOKEN }}
11699
tool: issue-comment
117100
issue-number: ${{ steps.pr.outputs.number }}
118-
title: 'Preview ${{ matrix.image }} Image:'
101+
title: 'Preview ${{ inputs.image }} Image:'
119102
body: |
120103
```
121104
${{ steps.config.outputs.DOCKER_REPO_TAGGED }}
@@ -128,6 +111,6 @@ jobs:
128111
token: ${{ secrets.GITHUB_TOKEN }}
129112
tool: issue-comment
130113
issue-number: ${{ steps.pr.outputs.number }}
131-
title: 'Preview ${{ matrix.image }} Image Push Failed'
114+
title: 'Preview ${{ inputs.image }} Image Push Failed'
132115
body: |
133116
Failed to push preview image. Please check workflow logs.

0 commit comments

Comments
 (0)