-
Notifications
You must be signed in to change notification settings - Fork 1
190 lines (169 loc) · 7.64 KB
/
sync-starter.yml
File metadata and controls
190 lines (169 loc) · 7.64 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
185
186
187
188
189
190
name: Sync starter to flat repo
# Mirrors `packages/starter/` into `emdashCommerce/starter` as a flat, cloneable
# template. Content-mirror approach (not `git subtree split`) — gives us a
# clean history in the target and room for the `workspace:*` → `^x.y.z`
# rewrite without fighting rebases.
#
# Fires on pushes to main that touch the starter or core's version. Also
# triggerable manually via the Actions UI.
on:
push:
branches: [main]
paths:
- "packages/starter/**"
- "packages/core/package.json"
- ".github/workflows/sync-starter.yml"
workflow_dispatch:
concurrency: sync-starter
permissions:
contents: read
env:
TARGET_REPO: emdashCommerce/starter
TARGET_BRANCH: main
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout monorepo
uses: actions/checkout@v4
with:
fetch-depth: 1
# Handle both "target repo has commits" and "target repo is brand-new and
# empty" cases without failing. actions/checkout refuses empty repos
# because there's no branch to resolve yet, so manage target's .git
# directly instead.
- name: Initialize or fetch target repo
run: |
mkdir -p target
cd target
git init --initial-branch="${TARGET_BRANCH}" --quiet
git remote add origin "https://x-access-token:${TOKEN}@github.com/${TARGET_REPO}.git"
if git fetch --depth=1 origin "${TARGET_BRANCH}" 2>/dev/null; then
echo "::notice::Fetched existing ${TARGET_REPO}@${TARGET_BRANCH}"
git reset --hard "origin/${TARGET_BRANCH}"
else
echo "::notice::${TARGET_REPO} is empty — this run will seed it"
fi
env:
TOKEN: ${{ secrets.STARTER_REPO_TOKEN }}
TARGET_REPO: ${{ env.TARGET_REPO }}
TARGET_BRANCH: ${{ env.TARGET_BRANCH }}
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "22"
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Mirror starter contents into target
run: |
# Keep target's .git, wipe everything else, copy starter/ in.
find target -mindepth 1 -maxdepth 1 -not -name '.git' -exec rm -rf {} +
cp -R packages/starter/. target/
# Strip local-dev artifacts that shouldn't ship to the flat repo
rm -rf target/node_modules target/dist target/.astro target/.wrangler \
target/data.db target/data.db-shm target/data.db-wal target/uploads
mkdir -p target/uploads && touch target/uploads/.gitkeep
# emdash bun-patches live at the monorepo root; propagate to flat repo
# root so patchedDependencies in target/package.json can find them.
if [ -d patches ]; then
cp -R patches target/patches
fi
- name: Rewrite workspace deps + CF-first scripts + wrangler.jsonc
run: |
CORE_VERSION=$(node -p "require('./packages/core/package.json').version")
echo "Pinning @dashcommerce/core to ^${CORE_VERSION}"
node - "$CORE_VERSION" <<'NODE'
const fs = require('fs');
const [, , version] = process.argv;
// ── package.json ──
const pkgPath = 'target/package.json';
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
// Pin workspace deps to the real published version of core.
for (const field of ['dependencies', 'devDependencies', 'peerDependencies']) {
if (!pkg[field]) continue;
for (const dep of Object.keys(pkg[field])) {
if (pkg[field][dep] === 'workspace:*') {
pkg[field][dep] = `^${version}`;
}
}
}
// Flat repo's primary deploy target is Cloudflare (via the Deploy
// button), so swap scripts so `build` IS the CF build — CF dashboard
// auto-detect will land on `bun run build` and do the right thing.
// `build:node` stays available for contributors who want Node.
if (pkg.scripts?.build && pkg.scripts['build:cf']) {
pkg.scripts['build:node'] = pkg.scripts.build;
pkg.scripts.build = pkg.scripts['build:cf'];
delete pkg.scripts['build:cf'];
}
// Propagate the emdash bun-patch reference from the monorepo root
// so the flat repo applies it on `bun install` too.
const rootPkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
if (rootPkg.patchedDependencies) {
pkg.patchedDependencies = rootPkg.patchedDependencies;
}
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, '\t') + '\n');
// ── wrangler.jsonc ──
// Strip any previously-injected main + assets block. These fields
// must NOT exist at config-load time because @cloudflare/vite-plugin
// (invoked during astro sync, before the build runs) validates that
// main points at an existing file — and the file is the build
// output. @astrojs/cloudflare emits dist/server/wrangler.json at
// build time with these fields filled in correctly; deploy with:
// npx wrangler deploy --config dist/server/wrangler.json
const wranglerPath = 'target/wrangler.jsonc';
if (fs.existsSync(wranglerPath)) {
let content = fs.readFileSync(wranglerPath, 'utf8');
const mainBlockRe = /\n\n\t"main":\s*"\.\/dist\/server\/entry\.mjs",\n\t"assets":\s*\{[^}]*\},?/;
if (mainBlockRe.test(content)) {
content = content.replace(mainBlockRe, '');
fs.writeFileSync(wranglerPath, content);
console.log('Stripped stale main + assets block from wrangler.jsonc');
}
}
// ── railway.json ──
// Railway auto-detect runs `bun run build`, which on the flat repo
// is the Cloudflare build. Override with build:node so Railway gets
// a Node-standalone server bundle compatible with its runtime.
const railway = {
"$schema": "https://railway.com/railway.schema.json",
build: {
builder: "RAILPACK",
buildCommand: "bun install --frozen-lockfile && bun run build:node",
},
deploy: {
startCommand: "bun run start",
healthcheckPath: "/",
healthcheckTimeout: 60,
restartPolicyType: "ON_FAILURE",
restartPolicyMaxRetries: 3,
},
};
fs.writeFileSync(
'target/railway.json',
JSON.stringify(railway, null, '\t') + '\n',
);
console.log('Wrote flat-repo railway.json (uses build:node for Railway)');
NODE
- name: Regenerate standalone lockfile
working-directory: target
run: |
rm -f bun.lock bun.lockb
bun install --ignore-scripts
- name: Commit + push to target
working-directory: target
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
if git diff --cached --quiet; then
echo "::notice::No starter changes to sync"
exit 0
fi
SHORT_SHA="${GITHUB_SHA:0:7}"
git commit -m "sync: ${GITHUB_REPOSITORY}@${SHORT_SHA}" \
-m "Source: https://github.com/${GITHUB_REPOSITORY}/commit/${GITHUB_SHA}"
git push origin "${{ env.TARGET_BRANCH }}"
echo "::notice::Synced ${GITHUB_REPOSITORY}@${SHORT_SHA} → ${TARGET_REPO}"