diff --git a/.github/workflows/preview-docs-build.yml b/.github/workflows/preview-docs-build.yml index 9280b0199f..16cbff7ae9 100644 --- a/.github/workflows/preview-docs-build.yml +++ b/.github/workflows/preview-docs-build.yml @@ -1,4 +1,4 @@ -name: Build Docs Preview (Unprivileged) +name: Preview docs build on: pull_request: @@ -34,6 +34,12 @@ jobs: id: datetime run: echo "datetime=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT + - name: Save PR metadata + run: | + mkdir -p /tmp/pr-metadata + echo "${{ github.event.pull_request.number }}" > /tmp/pr-metadata/pr-number.txt + echo "${{ github.event.pull_request.head.sha }}" > /tmp/pr-metadata/pr-sha.txt + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -58,5 +64,21 @@ jobs: path: /tmp/fastgpt-docs-${{ steps.datetime.outputs.datetime }}.tar retention-days: 1 + - name: Upload PR metadata + uses: actions/upload-artifact@v4 + with: + name: pr-metadata-docs-${{ steps.datetime.outputs.datetime }} + path: /tmp/pr-metadata/ + retention-days: 1 + + call-push-workflow: + needs: build-docs-image + uses: ./.github/workflows/preview-docs-push.yml + secrets: inherit + with: + pr_number: ${{ github.event.pull_request.number }} + datetime: ${{ needs.build-docs-image.outputs.datetime }} + run_id: ${{ github.run_id }} + outputs: datetime: ${{ steps.datetime.outputs.datetime }} diff --git a/.github/workflows/preview-docs-push.yml b/.github/workflows/preview-docs-push.yml index 64c43a8d41..9495044f48 100644 --- a/.github/workflows/preview-docs-push.yml +++ b/.github/workflows/preview-docs-push.yml @@ -1,11 +1,17 @@ -name: Deploy Docs Preview (Privileged) +name: Preview docs push on: - workflow_run: - workflows: ["Build Docs Preview (Unprivileged)"] - types: [completed] - branches: - - '**' # 监听所有分支 + workflow_call: + inputs: + pr_number: + required: true + type: string + datetime: + required: true + type: string + run_id: + required: true + type: string permissions: contents: read @@ -13,67 +19,40 @@ permissions: attestations: write id-token: write pull-requests: write - issues: write # Required for issue-comment (PR comments use Issues API) + issues: write # Required for issue-comment (PR comments use Issues API) jobs: push-and-deploy: - if: ${{ github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-24.04 steps: - - name: Get PR information + - name: Download PR metadata + uses: actions/download-artifact@v4 + with: + name: pr-metadata-docs-${{ inputs.datetime }} + path: /tmp/pr-metadata/ + run-id: ${{ inputs.run_id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Read PR information id: pr - uses: actions/github-script@v7 - with: - script: | - // 获取触发工作流的 PR 信息 - const { data: pullRequests } = await github.rest.pulls.list({ - owner: context.repo.owner, - repo: context.repo.repo, - state: 'open', - }); + run: | + PR_NUMBER="${{ inputs.pr_number }}" + echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT + echo "Found PR #$PR_NUMBER" - // 查找匹配的 PR(支持 fork 仓库) - const pr = pullRequests.find(pr => - pr.head.ref === context.payload.workflow_run.head_branch && - pr.head.sha === context.payload.workflow_run.head_sha - ); - - if (!pr) { - core.setFailed('No open PR found for this branch and commit'); - return; - } - - core.setOutput('number', pr.number); - - - name: Get workflow artifacts - uses: actions/github-script@v7 + - name: Get artifact name id: artifacts - with: - script: | - const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ - owner: context.repo.owner, - repo: context.repo.repo, - run_id: context.payload.workflow_run.id, - }); - - const artifact = artifacts.data.artifacts[0]; - if (!artifact) { - core.setFailed('No artifact found'); - return; - } - - // Extract datetime from artifact name - const datetime = artifact.name.replace('fastgpt-docs-', ''); - core.setOutput('datetime', datetime); - core.setOutput('artifact_name', artifact.name); + run: | + echo "datetime=${{ inputs.datetime }}" >> $GITHUB_OUTPUT + echo "artifact_name=fastgpt-docs-${{ inputs.datetime }}" >> $GITHUB_OUTPUT - name: Download image artifact uses: actions/download-artifact@v4 with: name: ${{ steps.artifacts.outputs.artifact_name }} path: /tmp/ - run-id: ${{ github.event.workflow_run.id }} + run-id: ${{ inputs.run_id }} github-token: ${{ secrets.GITHUB_TOKEN }} - name: Load Docker image diff --git a/.github/workflows/preview-fastgpt-build.yml b/.github/workflows/preview-fastgpt-build.yml index 5aa23a7218..703d966601 100644 --- a/.github/workflows/preview-fastgpt-build.yml +++ b/.github/workflows/preview-fastgpt-build.yml @@ -1,4 +1,4 @@ -name: FastGPT Build (Unprivileged) +name: Preview fastgpt build on: pull_request: @@ -33,6 +33,12 @@ jobs: ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }} fetch-depth: 0 + - name: Save PR metadata + run: | + mkdir -p /tmp/pr-metadata + echo "${{ github.event.pull_request.number }}" > /tmp/pr-metadata/pr-number.txt + echo "${{ github.event.pull_request.head.sha }}" > /tmp/pr-metadata/pr-sha.txt + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 with: @@ -88,3 +94,24 @@ jobs: name: ${{ steps.config.outputs.IMAGE_NAME }}-${{ github.sha }} path: /tmp/${{ steps.config.outputs.IMAGE_NAME }}-${{ github.sha }}.tar retention-days: 1 + + - name: Upload PR metadata + uses: actions/upload-artifact@v4 + with: + name: pr-metadata-${{ matrix.image }}-${{ github.sha }} + path: /tmp/pr-metadata/ + retention-days: 1 + + call-push-workflow: + needs: build-preview-images + strategy: + matrix: + image: [fastgpt, sandbox, mcp_server] + fail-fast: false + uses: ./.github/workflows/preview-fastgpt-push.yml + secrets: inherit + with: + pr_number: ${{ github.event.pull_request.number }} + pr_sha: ${{ github.sha }} + run_id: ${{ github.run_id }} + image: ${{ matrix.image }} diff --git a/.github/workflows/preview-fastgpt-push.yml b/.github/workflows/preview-fastgpt-push.yml index 17097031d6..603291c7b6 100644 --- a/.github/workflows/preview-fastgpt-push.yml +++ b/.github/workflows/preview-fastgpt-push.yml @@ -1,72 +1,55 @@ -name: FastGPT Push (Privileged) +name: Preview fastgpt push on: - workflow_run: - workflows: ["FastGPT Build (Unprivileged)"] - types: [completed] - branches: - - '**' # 监听所有分支 + workflow_call: + inputs: + pr_number: + required: true + type: string + pr_sha: + required: true + type: string + run_id: + required: true + type: string + image: + required: true + type: string jobs: push-preview-images: - # 只在构建成功时运行 - if: ${{ github.event.workflow_run.conclusion == 'success' }} - permissions: contents: read packages: write attestations: write id-token: write pull-requests: write - issues: write # Required for issue-comment (PR comments use Issues API) + issues: write # Required for issue-comment (PR comments use Issues API) runs-on: ubuntu-24.04 - strategy: - matrix: - image: [fastgpt, sandbox, mcp_server] - fail-fast: false steps: - - name: Get PR information + - name: Read PR information id: pr - uses: actions/github-script@v7 - with: - script: | - // 获取触发工作流的 PR 信息 - const { data: pullRequests } = await github.rest.pulls.list({ - owner: context.repo.owner, - repo: context.repo.repo, - state: 'open', - }); - - // 查找匹配的 PR(支持 fork 仓库) - const pr = pullRequests.find(pr => - pr.head.ref === context.payload.workflow_run.head_branch && - pr.head.sha === context.payload.workflow_run.head_sha - ); - - if (!pr) { - core.setFailed('No open PR found for this branch and commit'); - return; - } - - core.setOutput('number', pr.number); - core.setOutput('sha', context.payload.workflow_run.head_sha); + run: | + echo "number=${{ inputs.pr_number }}" >> $GITHUB_OUTPUT + echo "sha=${{ inputs.pr_sha }}" >> $GITHUB_OUTPUT + echo "Found PR #${{ inputs.pr_number }} (SHA: ${{ inputs.pr_sha }})" - name: Set image config id: config run: | SHA="${{ steps.pr.outputs.sha }}" - if [[ "${{ matrix.image }}" == "fastgpt" ]]; then + if [[ "${{ inputs.image }}" == "fastgpt" ]]; then echo "IMAGE_NAME=fastgpt" >> $GITHUB_OUTPUT echo "DESCRIPTION=fastgpt-pr image" >> $GITHUB_OUTPUT echo "DOCKER_REPO_TAGGED=${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-pr:fastgpt_${SHA}" >> $GITHUB_OUTPUT - elif [[ "${{ matrix.image }}" == "sandbox" ]]; then + elif [[ "${{ inputs.image }}" == "sandbox" ]]; then echo "IMAGE_NAME=fastgpt-sandbox" >> $GITHUB_OUTPUT echo "DESCRIPTION=fastgpt-sandbox-pr image" >> $GITHUB_OUTPUT echo "DOCKER_REPO_TAGGED=${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-pr:fastgpt_sandbox_${SHA}" >> $GITHUB_OUTPUT - elif [[ "${{ matrix.image }}" == "mcp_server" ]]; then + elif [[ "${{ inputs.image }}" == "mcp_server" ]]; then echo "IMAGE_NAME=fastgpt-mcp-server" >> $GITHUB_OUTPUT echo "DESCRIPTION=fastgpt-mcp_server-pr image" >> $GITHUB_OUTPUT echo "DOCKER_REPO_TAGGED=${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-pr:fastgpt_mcp_server_${SHA}" >> $GITHUB_OUTPUT @@ -77,7 +60,7 @@ jobs: with: name: ${{ steps.config.outputs.IMAGE_NAME }}-${{ steps.pr.outputs.sha }} path: /tmp/ - run-id: ${{ github.event.workflow_run.id }} + run-id: ${{ inputs.run_id }} github-token: ${{ secrets.GITHUB_TOKEN }} - name: Load Docker image @@ -115,7 +98,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} tool: issue-comment issue-number: ${{ steps.pr.outputs.number }} - title: 'Preview ${{ matrix.image }} Image:' + title: 'Preview ${{ inputs.image }} Image:' body: | ``` ${{ steps.config.outputs.DOCKER_REPO_TAGGED }} @@ -128,6 +111,6 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} tool: issue-comment issue-number: ${{ steps.pr.outputs.number }} - title: 'Preview ${{ matrix.image }} Image Push Failed' + title: 'Preview ${{ inputs.image }} Image Push Failed' body: | Failed to push preview image. Please check workflow logs.