-
Notifications
You must be signed in to change notification settings - Fork 20
184 lines (148 loc) · 5.57 KB
/
Copy pathgenerate-preview-sample.yml
File metadata and controls
184 lines (148 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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: List artifacts
uses: actions/github-script@v8
id: artifact
with:
script: |
const artifacts =
await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo
});
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.