Skip to content

.github/workflows/generate-preview-sample.yml #11

.github/workflows/generate-preview-sample.yml

.github/workflows/generate-preview-sample.yml #11

name: PressMint Preview Sample Generator
on:
workflow_dispatch:
inputs:
pr_number:
description: "Pull Request number"
required: true
type: string
repo:
description: "Repository (owner/repo)"
required: true
type: string
permissions:
contents: write
pull-requests: write
jobs:
preview:
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Load PR metadata
id: pr
uses: actions/github-script@v8
with:
script: |
const prNumber = Number('${{ inputs.pr_number }}');
const [owner, repo] = '${{ inputs.repo }}'.split('/');
const { data: pr } = await github.rest.pulls.get({
owner,
repo,
pull_number: prNumber
});
core.setOutput('number', pr.number);
core.setOutput('head_sha', pr.head.sha);
core.setOutput('head_repo', pr.head.repo.full_name);
core.setOutput('head_ref', pr.head.ref);
- name: Find Validate run
id: run
uses: actions/github-script@v8
with:
script: |
const prNumber = Number('${{ inputs.pr_number }}');
const runs = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "Validate.yml",
event: "pull_request",
status: "success"
});
const run = runs.data.workflow_runs.find(r =>
r.pull_requests?.some(pr => pr.number === prNumber)
);
if (!run) {
core.setFailed("No Validate run found for PR");
}
core.setOutput("run_id", run.id);
- name: List artifacts
uses: actions/github-script@v8
id: artifact
with:
script: |
const runId = Number('${{ steps.run.outputs.run_id }}');
const artifacts =
await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
});
const artifact = artifacts.data.artifacts.find(
a => a.name === 'pressmint-meta-pr-${{steps.pr.outputs.number}}'
);
if (!artifact) {
core.setFailed('Artifact not found');
}
core.setOutput('artifact_id', artifact.id);
- name: Download artifact
run: |
gh api \
repos/${{ github.repository }}/actions/artifacts/${{ steps.artifact.outputs.artifact_id }}/zip \
> artifact.zip
unzip artifact.zip
env:
GH_TOKEN: ${{ github.token }}
- name: Load metadata
id: meta
run: |
source pressmint.env
echo "PRESS_COUNT=$press_count" >> $GITHUB_OUTPUT
echo "PRESS_CODE=$(echo "$press_process" | jq -r '.[0]')" >> $GITHUB_OUTPUT
- name: Gate preview
if: steps.meta.outputs.PRESS_COUNT != '1'
run: |
echo "::notice:: Skipping preview generation as ${steps.meta.outputs.PRESS_COUNT} press folders were changed"
exit 0
# -----------------------------
# 1. Checkout PR source safely
# -----------------------------
- name: Checkout PR
uses: actions/checkout@v5
with:
repository: ${{ steps.pr.outputs.head_repo }}
ref: ${{ steps.pr.outputs.head_sha }}
path: src
# -----------------------------
# 2. Run PressMintBuild
# -----------------------------
- name: Run PressMintBuild
uses: ./PressMint/.github/actions/PressMintBuild
with:
press: '${{ steps.meta.outputs.PRESS_CODE }}'
# -----------------------------
# 3. Prepare preview branch
# -----------------------------
- name: Publish preview branch
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
PRESS_CODE: ${{ steps.meta.outputs.PRESS_CODE }}
BRANCH: preview-pr-${{ steps.pr.outputs.number }}--${PRESS_CODE,,}
run: |
set -e
echo "Publishing preview branch: $BRANCH"
git config --global user.name 'Matyáš Kopp (through GitHub Action)'
git config --global user.email 'matyaskopp@users.noreply.github.com'
# clone repo with token
git clone \
https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git \
repo
cd repo
# create or switch to orphan branch
git checkout --orphan "$BRANCH" || git checkout "$BRANCH"
# clean branch
git rm -rf . || true
# copy only sample output
mkdir -p sample
cp -R ../build/sample/* sample/
# commit only if changes exist
git add sample
if git diff --cached --quiet; then
echo "No changes in sample"
exit 0
fi
git commit -m "PressMint preview for PR #$PR_NUMBER"
git push origin "$BRANCH" --force
# -----------------------------
# 4. Comment on PR (idempotent)
# -----------------------------
- name: Find existing comment
id: find_comment
uses: peter-evans/find-comment@v4
with:
issue-number: ${{ steps.pr.outputs.number }}
comment-author: "github-actions[bot]"
body-includes: "<!-- pressmint-preview -->"
- name: Create or update PR comment
uses: peter-evans/create-or-update-comment@v5
with:
comment-id: ${{ steps.find_comment.outputs.comment-id }}
issue-number: ${{ steps.pr.outputs.number }}
edit-mode: replace
body: |
<!-- pressmint-preview -->
## PressMint Preview
A sample has been generated for this PR.
**Preview branch:**
```
preview-pr-${{ steps.pr.outputs.number }}--${{ steps.meta.outputs.PRESS_CODE }}
```
**Sample URL:**
https://github.com/${{ github.repository }}/tree/preview-pr-${{ steps.pr.outputs.number }}--${{ steps.meta.outputs.PRESS_CODE }}/Sample/PressMint-${{ steps.meta_load.outputs.PRESS_CODE }}
This preview updates automatically on every push.