Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 0 additions & 147 deletions .github/workflows/build-image-preview.yml

This file was deleted.

10 changes: 5 additions & 5 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ jobs:
uses: docker/login-action@v3
with:
registry: registry.cn-hangzhou.aliyuncs.com
username: ${{ secrets.ALI_HUB_USERNAME }}
password: ${{ secrets.ALI_HUB_PASSWORD }}
username: ${{ secrets.FASTGPT_ALI_IMAGE_USER }}
password: ${{ secrets.FASTGPT_ALI_IMAGE_PSW }}

- name: Create manifest list and push
working-directory: /tmp/digests
Expand All @@ -110,9 +110,9 @@ jobs:
GITHUB_LATEST="ghcr.io/${{ github.repository_owner }}/fastgpt-plugin:latest"
GITHUB_SHA="ghcr.io/${{ github.repository_owner }}/fastgpt-plugin:${{ github.sha }}"

ALI_TAG="${{ secrets.ALI_IMAGE_NAME }}/fastgpt-plugin:${{ github.ref_name }}"
ALI_LATEST="${{ secrets.ALI_IMAGE_NAME }}/fastgpt-plugin:latest"
ALI_SHA="${{ secrets.ALI_IMAGE_NAME }}/fastgpt-plugin:${{ github.sha }}"
ALI_TAG="${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-plugin:${{ github.ref_name }}"
ALI_LATEST="${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-plugin:latest"
ALI_SHA="${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-plugin:${{ github.sha }}"

DOCKER_TAG="${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt-plugin:${{ github.ref_name }}"
DOCKER_LATEST="${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt-plugin:latest"
Expand Down
112 changes: 112 additions & 0 deletions .github/workflows/preview-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: Preview Image — Build

on:
pull_request:
types: [opened, synchronize, reopened]
branches: ['*']

# Only one build per PR branch at a time
concurrency:
group: 'preview-build-${{ github.head_ref }}'
cancel-in-progress: true

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-24.04

steps:
- name: Get PR changed files via API
id: pr-files
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUMBER=${{ github.event.number }}
REPO=${{ github.repository }}

RESPONSE=$(gh api 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" > /tmp/package-changes.txt

- name: Set sparse checkout environment variable
run: |
echo "SPARSE_CHECKOUT_CONFIG<<EOF" >> $GITHUB_ENV
echo "/*" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV

- name: Checkout PR code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
sparse-checkout-cone-mode: false
sparse-checkout: ${{ env.SPARSE_CHECKOUT_CONFIG }}

- name: Setup bun
uses: oven-sh/setup-bun@v2

- 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: Build Docker image (no push)
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64
push: false
tags: fastgpt-plugin-pr:${{ github.event.pull_request.head.sha }}
outputs: type=docker,dest=/tmp/image.tar
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Save PR metadata
run: |
echo "${{ github.event.pull_request.number }}" > /tmp/pr-number.txt
echo "${{ github.event.pull_request.head.sha }}" > /tmp/pr-sha.txt

- name: Upload Docker image artifact
uses: actions/upload-artifact@v4
with:
name: preview-image
path: |
/tmp/image.tar
/tmp/pr-number.txt
/tmp/pr-sha.txt
/tmp/package-changes.txt
retention-days: 1
106 changes: 106 additions & 0 deletions .github/workflows/preview-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: Preview Image — Push

on:
workflow_run:
workflows: ['Preview Image — Build']
types: [completed]

# Only one push at a time
concurrency:
group: 'preview-push'
cancel-in-progress: false

permissions:
contents: read
packages: write
attestations: write
id-token: write
pull-requests: write
actions: read

jobs:
push:
runs-on: ubuntu-24.04
# Only push when the build succeeded
if: ${{ github.event.workflow_run.conclusion == 'success' }}

steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: preview-image
path: /tmp
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Read PR metadata
id: pr
run: |
echo "number=$(cat /tmp/pr-number.txt)" >> "$GITHUB_OUTPUT"
echo "sha=$(cat /tmp/pr-sha.txt)" >> "$GITHUB_OUTPUT"
echo "package_changes=$(cat /tmp/package-changes.txt)" >> "$GITHUB_OUTPUT"

- name: Load Docker image
run: docker load --input /tmp/image.tar

- 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.FASTGPT_ALI_IMAGE_USER }}
password: ${{ secrets.FASTGPT_ALI_IMAGE_PSW }}

- name: Tag and push Docker image
run: |
docker tag fastgpt-plugin-pr:${{ steps.pr.outputs.sha }} \
${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-plugin-pr:${{ steps.pr.outputs.sha }}
docker push ${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-plugin-pr:${{ steps.pr.outputs.sha }}

- name: Add PR comment on success
if: success() && steps.pr.outputs.number != ''
uses: actions/github-script@v7
with:
script: |
const prNumber = parseInt('${{ steps.pr.outputs.number }}');
const marker = '<!-- fastgpt-plugin-preview -->';

const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});

const existingComment = comments.find(comment =>
comment.body.includes(marker)
);

const commentBody = `${marker}
✅ **Build Successful** - Preview Images for this PR:

\`\`\`
${{ secrets.FASTGPT_ALI_IMAGE_PREFIX }}/fastgpt-plugin-pr:${{ steps.pr.outputs.sha }}
\`\`\`

**Changed packages:**
${{ steps.pr.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: prNumber,
body: commentBody
});
}

Loading