Skip to content

Preview Docs Image — Push #1117

Preview Docs Image — Push

Preview Docs Image — Push #1117

name: Preview Docs Image — Push
on:
workflow_run:
workflows: ['Preview Docs Image — Build']
types: [completed]
# Only one push at a time
concurrency:
group: 'preview-docs-push'
cancel-in-progress: false
permissions:
contents: read
jobs:
check_pr_author:
runs-on: ubuntu-24.04
# Only inspect the upstream PR when the build succeeded.
if: ${{ github.event.workflow_run.conclusion == 'success' }}
permissions:
contents: read
pull-requests: read
outputs:
trusted: ${{ steps.trust.outputs.trusted }}
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;
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 docs preview publish.');
core.setOutput('trusted', 'false');
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.info(`PR #${pullRequest.number} author association is ${pullRequest.author_association}; privileged docs preview publish is allowed.`);
} else {
core.warning(`PR #${pullRequest.number} author association is ${pullRequest.author_association}; skipping privileged docs preview publish.`);
}
push:
needs: check_pr_author
runs-on: ubuntu-24.04
# Only trusted repository members/collaborators can promote PR-built artifacts.
if: ${{ needs.check_pr_author.outputs.trusted == 'true' }}
permissions:
contents: read
packages: write
attestations: write
id-token: write
pull-requests: write
issues: write
actions: read
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: preview-docs-image
path: /tmp
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Load Docker image
run: docker load --input /tmp/docs-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: |
docker tag fastgpt-docs-pr:${{ needs.check_pr_author.outputs.sha }} \
ghcr.io/${{ github.repository_owner }}/fastgpt-docs-pr:${{ needs.check_pr_author.outputs.sha }}
docker push ghcr.io/${{ github.repository_owner }}/fastgpt-docs-pr:${{ needs.check_pr_author.outputs.sha }}
- name: Update deployment image
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG_CN }}
run: |
KUBECONFIG_FILE=$(mktemp)
trap "rm -f $KUBECONFIG_FILE" EXIT
echo "$KUBE_CONFIG" > "$KUBECONFIG_FILE"
chmod 600 "$KUBECONFIG_FILE"
kubectl --kubeconfig "$KUBECONFIG_FILE" set image deployment/fastgpt-doc-preview \
fastgpt-doc-preview=ghcr.io/${{ github.repository_owner }}/fastgpt-docs-pr:${{ needs.check_pr_author.outputs.sha }}
kubectl --kubeconfig "$KUBECONFIG_FILE" annotate deployment/fastgpt-doc-preview \
originImageName="ghcr.io/${{ github.repository_owner }}/fastgpt-docs-pr:${{ needs.check_pr_author.outputs.sha }}" --overwrite
- 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 }}');
const marker = '<!-- fastgpt-doc-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}
✅ **Docs Preview Deployed!**
🔗 [👀 Click here to visit preview](https://xcldnthwehkh.sealoshzh.site)
\`\`\`
ghcr.io/${{ github.repository_owner }}/fastgpt-docs-pr:${{ needs.check_pr_author.outputs.sha }}
\`\`\`
🕒 Time: ${{ steps.preview_time.outputs.value }}
`;
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
});
}