|
19 | 19 | * installed, matching `sync-chart-appversion.ts`; for the same reason it reports |
20 | 20 | * with `console` rather than `@sim/logger`. |
21 | 21 | * |
22 | | - * The release tag must be present locally (a shallow checkout fetches it first). A release |
23 | | - * that predates the list (v0.9.72 and earlier) gets a notice and no file, so its chart |
24 | | - * behaves like a checkout: pre-pull needs an explicit function image. |
| 22 | + * The release tag is fetched when the checkout lacks it (publish jobs check out shallow). On |
| 23 | + * the release commit itself the tag may not exist yet, since publishing does not wait for the |
| 24 | + * job that cuts it, so HEAD is read when its subject names the release (`v1.2.3: ...`, the rule |
| 25 | + * helm.yml resolves the version by). A release that predates the list (v0.9.72 and earlier) |
| 26 | + * gets a notice and no file, so its chart behaves like a checkout: pre-pull needs an explicit |
| 27 | + * function image. |
25 | 28 | * |
26 | 29 | * @example |
27 | 30 | * ``` |
@@ -63,17 +66,55 @@ function run(command: string, args: string[]): string { |
63 | 66 | return result.stdout |
64 | 67 | } |
65 | 68 |
|
66 | | -/** True when `path` exists at `revision`; throws when the revision itself is unknown. */ |
67 | | -function existsAt(revision: string, path: string): boolean { |
68 | | - const commit = spawnSync('git', ['rev-parse', '--verify', '--quiet', `${revision}^{commit}`], { |
69 | | - cwd: ROOT, |
| 69 | +function git(args: readonly string[]): { ok: boolean; stdout: string; stderr: string } { |
| 70 | + const result = spawnSync('git', args, { cwd: ROOT, encoding: 'utf8' }) |
| 71 | + return { ok: result.status === 0, stdout: result.stdout ?? '', stderr: result.stderr ?? '' } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * The git revision to read `version`'s image list from: its tag when present, else HEAD when |
| 76 | + * HEAD is the release commit that will be tagged `version`. Throws otherwise, naming why. |
| 77 | + */ |
| 78 | +export function releaseRevision(input: { |
| 79 | + version: string |
| 80 | + tagPresent: boolean |
| 81 | + headSubject: string |
| 82 | + fetchError?: string |
| 83 | +}): string { |
| 84 | + if (input.tagPresent) return `refs/tags/${input.version}` |
| 85 | + if (input.headSubject.startsWith(`${input.version}:`)) return 'HEAD' |
| 86 | + throw new Error( |
| 87 | + `Release tag ${input.version} is not present and HEAD ("${input.headSubject}") is not its release commit${input.fetchError ? `; fetching the tag failed: ${input.fetchError.trim()}` : ''}` |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +/** Resolves {@link releaseRevision} against the repository, fetching the tag when absent. */ |
| 92 | +function resolveReleaseRevision(version: string): string { |
| 93 | + const tagRef = `refs/tags/${version}` |
| 94 | + const hasTag = () => git(['rev-parse', '--verify', '--quiet', `${tagRef}^{commit}`]).ok |
| 95 | + let fetchError: string | undefined |
| 96 | + if (!hasTag()) { |
| 97 | + const fetched = git([ |
| 98 | + 'fetch', |
| 99 | + '--quiet', |
| 100 | + '--no-tags', |
| 101 | + '--depth', |
| 102 | + '1', |
| 103 | + 'origin', |
| 104 | + `+${tagRef}:${tagRef}`, |
| 105 | + ]) |
| 106 | + if (!fetched.ok) fetchError = fetched.stderr |
| 107 | + } |
| 108 | + return releaseRevision({ |
| 109 | + version, |
| 110 | + tagPresent: hasTag(), |
| 111 | + headSubject: git(['log', '-1', '--format=%s', 'HEAD']).stdout.trim(), |
| 112 | + fetchError, |
70 | 113 | }) |
71 | | - if (commit.status !== 0) throw new Error(`Release tag ${revision} is not present locally`) |
72 | | - return spawnSync('git', ['cat-file', '-e', `${revision}:${path}`], { cwd: ROOT }).status === 0 |
73 | 114 | } |
74 | 115 |
|
75 | | -function releaseFunctionImage(version: string): string { |
76 | | - const list: unknown = JSON.parse(run('git', ['show', `${version}:${IMAGE_LIST_PATH}`])) |
| 116 | +function releaseFunctionImage(version: string, revision: string): string { |
| 117 | + const list: unknown = JSON.parse(run('git', ['show', `${revision}:${IMAGE_LIST_PATH}`])) |
77 | 118 | const image = |
78 | 119 | typeof list === 'object' && list !== null && 'function' in list ? list.function : undefined |
79 | 120 | if (typeof image !== 'string' || !FUNCTION_IMAGE.test(image)) { |
@@ -111,13 +152,14 @@ function renderedPrePullImage(): string { |
111 | 152 |
|
112 | 153 | async function main() { |
113 | 154 | const { version, verify } = parseArgs(process.argv.slice(2)) |
114 | | - if (!existsAt(version, IMAGE_LIST_PATH)) { |
| 155 | + const revision = resolveReleaseRevision(version) |
| 156 | + if (!git(['cat-file', '-e', `${revision}:${IMAGE_LIST_PATH}`]).ok) { |
115 | 157 | console.log( |
116 | 158 | `Notice: ${version} has no ${IMAGE_LIST_PATH}, so the chart carries no default sandbox Function image; sandbox.kubernetes.prePull needs images.function.tag or digest with it.` |
117 | 159 | ) |
118 | 160 | return |
119 | 161 | } |
120 | | - const image = releaseFunctionImage(version) |
| 162 | + const image = releaseFunctionImage(version, revision) |
121 | 163 | await mkdir(dirname(OUTPUT_PATH), { recursive: true }) |
122 | 164 | await writeFile(OUTPUT_PATH, `${JSON.stringify({ function: image }, null, 2)}\n`) |
123 | 165 | console.log(`Wrote ${image} to helm/sim/files/sandbox-images.json.`) |
|
0 commit comments