Skip to content

Commit 432c4f7

Browse files
committed
publish packages to read-only repos
1 parent 1e5b446 commit 432c4f7

2 files changed

Lines changed: 120 additions & 13 deletions

File tree

‎.github/scripts/monorepo-split-plan.php‎

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
'origin::',
1111
'tag::',
1212
'calculate-splits',
13+
'publish',
1314
'help',
1415
]);
1516

1617
if (isset($options['help'])) {
1718
fwrite(STDOUT, <<<'HELP'
1819
Usage:
19-
php .github/scripts/monorepo-split-plan.php [--config=.github/monorepo-split.json] [--base=<git-ref>] [--head=<git-ref>] [--origin=<git-ref>] [--tag=<tag>] [--calculate-splits]
20+
php .github/scripts/monorepo-split-plan.php [--config=.github/monorepo-split.json] [--base=<git-ref>] [--head=<git-ref>] [--origin=<git-ref>] [--tag=<tag>] [--calculate-splits] [--publish]
2021
2122
Validates the monorepo split map and prints the read-only repository split plan.
2223
This is a dry-run planner only. By default it prints splitsh-lite commands but
2324
does not run them. With --calculate-splits it runs splitsh-lite and prints split
24-
SHAs, but still never pushes.
25+
SHAs, but still never pushes. With --publish it calculates split SHAs and pushes
26+
branch or tag refs to configured active read-only repositories.
2527

2628
HELP);
2729
exit(0);
@@ -35,6 +37,10 @@
3537
$origin = isset($options['origin']) ? (string) $options['origin'] : 'HEAD';
3638
$tag = isset($options['tag']) ? (string) $options['tag'] : null;
3739
$calculateSplits = isset($options['calculate-splits']);
40+
$publish = isset($options['publish']);
41+
if ($publish) {
42+
$calculateSplits = true;
43+
}
3844

3945
$errors = [];
4046
$warnings = [];
@@ -156,6 +162,10 @@
156162
$errors[] = 'splitsh-lite is required when --calculate-splits is provided.';
157163
}
158164

165+
if ($publish && !commandExists('git')) {
166+
$errors[] = 'git is required when --publish is provided.';
167+
}
168+
159169
if (null !== $base || null !== $head) {
160170
if (null === $base || null === $head) {
161171
$errors[] = 'Both --base and --head are required when checking changed paths.';
@@ -165,13 +175,13 @@
165175
}
166176

167177
if ([] !== $errors) {
168-
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, [], [], $origin, $tag, $root, $calculateSplits, $errors);
178+
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, [], [], $origin, $tag, $root, $calculateSplits, $publish, $errors);
169179
fail($errors);
170180
}
171181

172182
[$plannedPackages, $skippedPackages] = planPackages($packages, $changedFiles, null !== $tag);
173183
$splitErrors = [];
174-
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, $plannedPackages, $skippedPackages, $origin, $tag, $root, $calculateSplits, $splitErrors);
184+
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, $plannedPackages, $skippedPackages, $origin, $tag, $root, $calculateSplits, $publish, $splitErrors);
175185

176186
if ([] !== $splitErrors) {
177187
fail($splitErrors);
@@ -319,9 +329,9 @@ function packageChanged(string $path, array $changedFiles): bool
319329
* @param list<array<string, mixed>> $plannedPackages
320330
* @param list<array{name: string, reason: string}> $skippedPackages
321331
*/
322-
function printSummary(string $configFile, array $packages, array $statusCounts, array $warnings, array $changedFiles, array $plannedPackages, array $skippedPackages, string $origin, ?string $tag, string $root, bool $calculateSplits, array &$splitErrors): void
332+
function printSummary(string $configFile, array $packages, array $statusCounts, array $warnings, array $changedFiles, array $plannedPackages, array $skippedPackages, string $origin, ?string $tag, string $root, bool $calculateSplits, bool $publish, array &$splitErrors): void
323333
{
324-
fwrite(STDOUT, 'Monorepo split dry-run plan' . PHP_EOL);
334+
fwrite(STDOUT, $publish ? 'Monorepo split publish plan' . PHP_EOL : 'Monorepo split dry-run plan' . PHP_EOL);
325335
fwrite(STDOUT, 'Config: ' . $configFile . PHP_EOL);
326336
fwrite(STDOUT, 'Split origin: ' . $origin . PHP_EOL);
327337
if (null !== $tag) {
@@ -350,7 +360,7 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
350360
fwrite(STDOUT, PHP_EOL . 'Changed files considered: none, planning every active package' . PHP_EOL);
351361
}
352362

353-
fwrite(STDOUT, PHP_EOL . sprintf('Would split/publish: %d package(s)', count($plannedPackages)) . PHP_EOL);
363+
fwrite(STDOUT, PHP_EOL . sprintf('%s split/publish: %d package(s)', $publish ? 'Will' : 'Would', count($plannedPackages)) . PHP_EOL);
354364
foreach ($plannedPackages as $package) {
355365
$line = sprintf(
356366
' - %s | path=%s | repository=%s | branch=%s',
@@ -371,6 +381,9 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
371381
$sha = splitshSha((string) $package['path'], $origin, $root, $splitErrors);
372382
if (null !== $sha) {
373383
fwrite(STDOUT, ' sha: ' . $sha . PHP_EOL);
384+
if ($publish) {
385+
publishSplit($package, $sha, $tag, $splitErrors);
386+
}
374387
}
375388
}
376389
}
@@ -389,6 +402,12 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
389402
}
390403
}
391404

405+
if ($publish) {
406+
fwrite(STDOUT, PHP_EOL . 'Publish mode enabled. Planned split refs were pushed when no errors occurred.' . PHP_EOL);
407+
408+
return;
409+
}
410+
392411
if ($calculateSplits) {
393412
fwrite(STDOUT, PHP_EOL . 'Dry run only. splitsh-lite calculated split SHAs, but nothing was pushed.' . PHP_EOL);
394413

@@ -428,6 +447,73 @@ function splitshSha(string $path, string $origin, string $root, array &$errors):
428447
return $sha;
429448
}
430449

450+
/**
451+
* @param array<string, mixed> $package
452+
*/
453+
function publishSplit(array $package, string $sha, ?string $tag, array &$errors): void
454+
{
455+
$name = (string) $package['name'];
456+
$repository = (string) $package['repository'];
457+
458+
if (null !== $tag) {
459+
publishTag($name, $repository, $sha, $tag, $errors);
460+
461+
return;
462+
}
463+
464+
$branch = (string) $package['branch'];
465+
$refspec = sprintf('%s:refs/heads/%s', $sha, $branch);
466+
$command = sprintf('git push %s %s 2>&1', escapeshellarg($repository), escapeshellarg($refspec));
467+
exec($command, $output, $exitCode);
468+
if (0 !== $exitCode) {
469+
$errors[] = sprintf('%s branch publish failed: %s', $name, implode(PHP_EOL, $output));
470+
471+
return;
472+
}
473+
474+
fwrite(STDOUT, sprintf(' pushed: %s -> %s:%s', $sha, $repository, $branch) . PHP_EOL);
475+
}
476+
477+
function publishTag(string $name, string $repository, string $sha, string $tag, array &$errors): void
478+
{
479+
$remoteTagRef = 'refs/tags/' . $tag;
480+
$lookupCommand = sprintf(
481+
'git ls-remote --tags --refs %s %s 2>&1',
482+
escapeshellarg($repository),
483+
escapeshellarg($remoteTagRef),
484+
);
485+
exec($lookupCommand, $lookupOutput, $lookupExitCode);
486+
if (0 !== $lookupExitCode) {
487+
$errors[] = sprintf('%s tag lookup failed for %s: %s', $name, $tag, implode(PHP_EOL, $lookupOutput));
488+
489+
return;
490+
}
491+
492+
if ([] !== $lookupOutput) {
493+
$remoteSha = strtok(trim($lookupOutput[0]), " \t");
494+
if ($remoteSha === $sha) {
495+
fwrite(STDOUT, sprintf(' tag exists: %s -> %s:%s', $sha, $repository, $tag) . PHP_EOL);
496+
497+
return;
498+
}
499+
500+
$errors[] = sprintf('%s tag %s already exists on %s at %s, expected %s', $name, $tag, $repository, $remoteSha, $sha);
501+
502+
return;
503+
}
504+
505+
$refspec = sprintf('%s:%s', $sha, $remoteTagRef);
506+
$pushCommand = sprintf('git push %s %s 2>&1', escapeshellarg($repository), escapeshellarg($refspec));
507+
exec($pushCommand, $pushOutput, $pushExitCode);
508+
if (0 !== $pushExitCode) {
509+
$errors[] = sprintf('%s tag publish failed for %s: %s', $name, $tag, implode(PHP_EOL, $pushOutput));
510+
511+
return;
512+
}
513+
514+
fwrite(STDOUT, sprintf(' pushed tag: %s -> %s:%s', $sha, $repository, $tag) . PHP_EOL);
515+
}
516+
431517
function commandExists(string $command): bool
432518
{
433519
exec(sprintf('command -v %s >/dev/null 2>&1', escapeshellarg($command)), $output, $exitCode);

‎.github/workflows/monorepo-split-plan.yml‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Monorepo Split Plan
1+
name: Monorepo Split
22
on:
33
push:
44
branches: [main]
@@ -28,12 +28,13 @@ concurrency:
2828

2929
jobs:
3030
plan:
31-
name: Dry-run split plan
31+
name: Split packages
3232
runs-on: ubuntu-latest
3333
steps:
3434
- uses: actions/checkout@v7
3535
with:
3636
fetch-depth: 0
37+
persist-credentials: false
3738
- uses: shivammathur/setup-php@v2
3839
with:
3940
php-version: '8.3'
@@ -60,6 +61,26 @@ jobs:
6061
GOBIN="$RUNNER_TEMP/bin" go install github.com/splitsh/lite@master
6162
sudo mv "$RUNNER_TEMP/bin/lite" /usr/local/bin/splitsh-lite
6263
splitsh-lite --help
64+
- name: Configure read-only repo SSH
65+
if: github.event_name == 'push'
66+
env:
67+
MONOREPO_SPLIT_SSH_KEY: ${{ secrets.MONOREPO_SPLIT_SSH_KEY }}
68+
run: |
69+
if [ -z "$MONOREPO_SPLIT_SSH_KEY" ]; then
70+
echo "::error::Missing MONOREPO_SPLIT_SSH_KEY secret for read-only repo publishing."
71+
exit 1
72+
fi
73+
74+
mkdir -p ~/.ssh
75+
printf '%s\n' "$MONOREPO_SPLIT_SSH_KEY" > ~/.ssh/monorepo_split
76+
chmod 600 ~/.ssh/monorepo_split
77+
ssh-keyscan github.com >> ~/.ssh/known_hosts
78+
{
79+
echo 'Host github.com'
80+
echo ' IdentityFile ~/.ssh/monorepo_split'
81+
echo ' IdentitiesOnly yes'
82+
echo ' StrictHostKeyChecking yes'
83+
} >> ~/.ssh/config
6384
- name: Validate split config
6485
run: php .github/scripts/monorepo-split-plan.php
6586
- name: Plan pull request package splits
@@ -70,18 +91,18 @@ jobs:
7091
--head='${{ github.event.pull_request.head.sha }}' \
7192
--origin='${{ github.event.pull_request.head.sha }}' \
7293
--calculate-splits
73-
- name: Plan mainline package splits
94+
- name: Publish mainline package splits
7495
if: github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/')
7596
run: |
7697
php .github/scripts/monorepo-split-plan.php \
7798
--base='${{ github.event.before }}' \
7899
--head='${{ github.sha }}' \
79100
--origin='${{ github.sha }}' \
80-
--calculate-splits
81-
- name: Plan release tag package splits
101+
--publish
102+
- name: Publish release tag package splits
82103
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
83104
run: |
84105
php .github/scripts/monorepo-split-plan.php \
85106
--tag='${{ github.ref_name }}' \
86107
--origin='${{ github.ref }}' \
87-
--calculate-splits
108+
--publish

0 commit comments

Comments
 (0)