Skip to content

Preview Plugin Image Push #98

Preview Plugin Image Push

Preview Plugin Image Push #98

name: Preview Plugin Image Push
on:
workflow_run:
workflows: ['Preview Plugin Image Build']
types: [completed]
concurrency:
group: preview-plugin-image-push
cancel-in-progress: false
permissions:
contents: read
jobs:
check_pr_author:
name: Check trusted PR author
runs-on: ubuntu-24.04
if: ${{ github.event.workflow_run.conclusion == 'success' }}
permissions:
contents: read
pull-requests: read
actions: read
outputs:
trusted: ${{ steps.trust.outputs.trusted }}
should_publish: ${{ steps.trust.outputs.should_publish }}
number: ${{ steps.trust.outputs.number }}
sha: ${{ steps.trust.outputs.sha }}
steps:
- name: Check trusted PR author
id: trust
uses: actions/github-script@v7
with:
script: |
const allowedAssociations = new Set(['OWNER', 'MEMBER', 'COLLABORATOR']);
const workflowRun = context.payload.workflow_run;
let prNumber = workflowRun.pull_requests?.[0]?.number;
core.setOutput('trusted', 'false');
core.setOutput('should_publish', 'false');
if (!prNumber) {
const headOwner = workflowRun.head_repository?.owner?.login;
const headBranch = workflowRun.head_branch;
if (headOwner && headBranch) {
core.info(`workflow_run payload did not include a PR number; looking up PR by ${headOwner}:${headBranch}.`);
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${headOwner}:${headBranch}`
});
const matchedPullRequest = pullRequests.find((pullRequest) =>
pullRequest.head.sha === workflowRun.head_sha
) ?? pullRequests[0];
prNumber = matchedPullRequest?.number;
}
}
if (!prNumber) {
core.warning('No pull request was found on the workflow_run payload. Skipping privileged preview publish.');
return;
}
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
const trusted = allowedAssociations.has(pullRequest.author_association);
core.setOutput('trusted', trusted ? 'true' : 'false');
core.setOutput('number', String(pullRequest.number));
core.setOutput('sha', pullRequest.head.sha);
if (!trusted) {
core.warning(`PR #${pullRequest.number} author association is ${pullRequest.author_association}; skipping privileged preview publish.`);
return;
}
const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, {
owner: context.repo.owner,
repo: context.repo.repo,
run_id: workflowRun.id,
per_page: 100
});
const artifactNames = new Set(artifacts.map((artifact) => artifact.name));
const shouldPublish = artifactNames.has('preview-plugin-server-image');
core.setOutput('should_publish', shouldPublish ? 'true' : 'false');
core.info(`PR #${pullRequest.number} author association is ${pullRequest.author_association}; preview server image publish=${shouldPublish}.`);
push:
name: Push preview Server image
needs: check_pr_author
runs-on: ubuntu-24.04
if: ${{ needs.check_pr_author.outputs.trusted == 'true' && needs.check_pr_author.outputs.should_publish == 'true' }}
permissions:
contents: read
packages: write
pull-requests: write
issues: write
actions: read
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: preview-plugin-server-image
path: /tmp
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Load Docker image
run: docker load --input /tmp/fastgpt-plugin-image.tar
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Tag and push Docker image
run: |
SHA="${{ needs.check_pr_author.outputs.sha }}"
PREVIEW_IMAGE="ghcr.io/${{ github.repository_owner }}/fastgpt-plugin-pr:server_${SHA}"
docker tag "fastgpt-plugin-pr:${SHA}" "${PREVIEW_IMAGE}"
docker push "${PREVIEW_IMAGE}"
- name: Format preview timestamp
id: preview_time
run: |
echo "value=$(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S (UTC+8)')" >> "$GITHUB_OUTPUT"
- name: Add PR comment on success
if: success() && needs.check_pr_author.outputs.number != ''
uses: actions/github-script@v7
with:
script: |
const prNumber = parseInt('${{ needs.check_pr_author.outputs.number }}', 10);
const marker = '<!-- fastgpt-plugin-preview-server -->';
const image = 'ghcr.io/${{ github.repository_owner }}/fastgpt-plugin-pr:server_${{ needs.check_pr_author.outputs.sha }}';
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 Server image for this PR:',
'',
'```',
image,
'```',
'',
'Time: ${{ steps.preview_time.outputs.value }}'
].join('\n');
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
});
}