fix: action #11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Preview build image | ||
|
Check failure on line 1 in .github/workflows/build-image-preview.yml
|
||
| 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" | ||
| HAS_RELEVANT_CHANGES="false" | ||
| # Check for relevant changes | ||
| for file in $FILES; do | ||
| if [ "$file" = "package.json" ]; then | ||
| echo "Found package.json" | ||
| HAS_RELEVANT_CHANGES="true" | ||
| elif [ "$file" = "bun.lock" ]; then | ||
| echo "Found bun.lock" | ||
| HAS_RELEVANT_CHANGES="true" | ||
| elif [ "$file" = "bun.lockb" ]; then | ||
| echo "Found bun.lockb" | ||
| HAS_RELEVANT_CHANGES="true" | ||
| elif [[ "$file" == .github/workflows/* ]]; then | ||
| echo "Found workflow file: $file" | ||
| HAS_RELEVANT_CHANGES="true" | ||
| elif [[ "$file" == modules/tool/packages/* ]]; then | ||
| echo "Found package file: $file" | ||
| HAS_RELEVANT_CHANGES="true" | ||
| fi | ||
| done | ||
| # Extract package names from changed files | ||
| PACKAGE_CHANGES="" | ||
| for file in $FILES; do | ||
| if [[ "$file" == modules/tool/packages/* ]]; then | ||
| # Extract package directory name (first segment after packages/) | ||
| PACKAGE_NAME=$(echo "$file" | cut -d'/' -f4) | ||
| echo "Found package directory: $PACKAGE_NAME" | ||
| # Check if we already have this package | ||
| if echo "$PACKAGE_CHANGES" | grep -vqw "$PACKAGE_NAME"; then | ||
| if [ -z "$PACKAGE_CHANGES" ]; then | ||
| PACKAGE_CHANGES="$PACKAGE_NAME" | ||
| else | ||
| PACKAGE_CHANGES="$PACKAGE_CHANGES $PACKAGE_NAME" | ||
| fi | ||
| fi | ||
| fi | ||
| done | ||
| # Output results for GitHub Actions | ||
| { | ||
| echo "files<<EOF" | ||
| echo "$FILES" | ||
| echo "EOF" | ||
| echo "package_changes<<EOF" | ||
| echo "$PACKAGE_CHANGES" | ||
| echo "EOF" | ||
| echo "has_relevant_changes=$HAS_RELEVANT_CHANGES" | ||
| } >> $GITHUB_OUTPUT | ||
| echo "Package changes detected: $PACKAGE_CHANGES" | ||
| echo "" | ||
| echo "Has relevant changes: $HAS_RELEVANT_CHANGES" | ||
| - name: Checkout code with sparse checkout | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ github.event.pull_request.head.sha }} | ||
| sparse-checkout-cone-mode: false | ||
| sparse-checkout: | | ||
| /* | ||
| !/modules/tool/packages/* | ||
| package.json | ||
| bun.lockb | ||
| ${{ steps.pr-files.outputs.package_changes == '' && format('') || join(format('/modules/tool/packages/{0}/*', split(' ', steps.pr-files.outputs.package_changes)), '\n') }} | ||
| - name: Verify sparse checkout | ||
| run: | | ||
| echo "Workspace contents:" | ||
| find . -maxdepth 2 -type f -name "*.json" -o -name "*.lockb" | head -10 | ||
| echo "" | ||
| echo "Package directories:" | ||
| ls -la modules/tool/packages/ 2>/dev/null || echo "No packages directory found" | ||
| echo "" | ||
| echo "Total workspace size:" | ||
| du -sh . | ||
| - name: Setup bun | ||
| if: steps.pr-files.outputs.has_relevant_changes == 'true' | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: 1.2.23 | ||
| - name: Install dependencies | ||
| if: steps.pr-files.outputs.has_relevant_changes == 'true' | ||
| run: bun install --frozen-lockfile && bun run install:plugins | ||
| - name: Build pkg | ||
| if: steps.pr-files.outputs.has_relevant_changes == 'true' | ||
| 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 for amd64 | ||
| id: build | ||
| 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: steps.pr-files.outputs.has_relevant_changes == 'true' && 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 | ||
| }); | ||
| } | ||
| - name: Add PR comment on skip | ||
| if: steps.pr-files.outputs.has_relevant_changes == 'false' | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body: ` | ||
| ℹ️ **Build Skipped** - No relevant changes found in this PR | ||
| This PR doesn't contain changes that require rebuilding (no package.json, bun.lockb, or plugin package changes). | ||
| ` | ||
| }); | ||