-
-
Notifications
You must be signed in to change notification settings - Fork 20
173 lines (149 loc) · 5.73 KB
/
changeset-snapshot.yml
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
name: Changeset Snapshot
on:
workflow_dispatch:
issue_comment:
types: [created]
defaults:
run:
shell: bash
env:
FORCE_COLOR: 1
concurrency: ${{ github.workflow }}-${{ github.ref }}
permissions:
contents: write
pull-requests: write
issues: write
id-token: write
actions: read
checks: read
deployments: read
discussions: read
packages: read
pages: read
repository-projects: read
security-events: read
statuses: read
jobs:
snapshot-release:
name: Create a snapshot release of a pull request
if: ${{ github.repository_owner == 'astrolicious' && github.event.issue.pull_request && (contains(github.event.comment.body, '!preview') || contains(github.event.comment.body, '/preview') || contains(github.event.comment.body, '!snapshot') || contains(github.event.comment.body, '/snapshot')) }}
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
issues: write
pull-requests: write
steps:
- name: "Check if user has write access"
uses: "lannonbr/repo-permission-check-action@2bb8c89ba8bf115c4bfab344d6a6f442b24c9a1f" # 2.0.2
with:
permission: "write"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Extract the snapshot name from comment body
id: getSnapshotName
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7
with:
script: |
const { body } = context.payload.comment;
const PREVIEW_RE = /^[!\/](?:preview|snapshot)\s+(\S*)\s*$/gim;
const [_, name] = PREVIEW_RE.exec(body) ?? [];
if (name) return name;
const error = 'Invalid command. Expected: "/preview <snapshot-name>"'
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: error,
})
core.setFailed(error)
result-encoding: string
- name: resolve pr refs
id: refs
uses: eficode/resolve-pr-refs@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
ref: ${{ steps.refs.outputs.head_ref }}
fetch-depth: 0
- run: git fetch origin main:main
- name: Setup pnpm (corepack enabled)
uses: pnpm/action-setup@v2
- name: Setup Node.js 20.x
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4
with:
node-version-file: '.node-version'
cache: 'pnpm'
- name: Install dependencies
run: pnpm ci:install
- name: Bump Package Versions
id: changesets
run: |
pnpm exec changeset status --output status.output.json 2>&1
pnpm exec changeset version --snapshot ${{ steps.getSnapshotName.outputs.result }}
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "status<<$EOF" >> $GITHUB_OUTPUT
echo "$(cat status.output.json)" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
env:
# Needs access to run the script
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Disable color
FORCE_COLOR: 0
NO_COLOR: 1
- name: Publish Release
id: publish
run: |
GITHUB_ACTIONS=0 pnpm run build > build.output.txt 2>&1
pnpm exec changeset publish --tag experimental--${{ steps.getSnapshotName.outputs.result }} > publish.output.txt 2>&1
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "build<<$EOF" >> $GITHUB_OUTPUT
echo "$(cat build.output.txt)" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
cat build.output.txt
echo "publish<<$EOF" >> $GITHUB_OUTPUT
echo "$(cat publish.output.txt)" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
cat publish.output.txt
env:
# Needs access to publish to npm
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# Disable color
FORCE_COLOR: 0
NO_COLOR: 1
- name: Pull Request Notification
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7
env:
TAG: ${{ steps.getSnapshotName.outputs.result }}
STATUS_DATA: ${{ steps.changesets.outputs.status }}
BUILD_LOG: ${{ steps.publish.outputs.build }}
PUBLISH_LOG: ${{ steps.publish.outputs.publish }}
with:
github-token: ${{ secrets.STUDIOCMS_SERVICE_TOKEN }}
script: |
let changeset = { releases: [] };
try {
changeset = JSON.parse(process.env.STATUS_DATA);
} catch (e) {}
let message = 'Snapshots have been released for the following packages:'
for (const release of changeset.releases) {
if (release.type === 'none') continue;
message += `\n- \`${release.name}@experimental--${process.env.TAG}\``;
}
function details(title, body) {
message += '\n';
message += `<details><summary><strong>${title}</strong></summary>`
message += '\n\n```\n';
message += body;
message += '\n```\n\n</details>';
}
details('Publish Log', process.env.PUBLISH_LOG);
details('Build Log', process.env.BUILD_LOG);
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message,
})