rman 2.30.0 → 2.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -372,7 +372,7 @@ export var PublishService;
372
372
  }), {
373
373
  cwd: publishDir,
374
374
  app: pkg.repository.app,
375
- stdio: 'inherit',
375
+ stdio: 'stderr',
376
376
  });
377
377
  result.push(entry);
378
378
  }
@@ -1,5 +1,6 @@
1
1
  import { execFile } from 'node:child_process';
2
2
  import fs from 'node:fs';
3
+ import os from 'node:os';
3
4
  import path from 'node:path';
4
5
  import { promisify } from 'node:util';
5
6
  import { Service } from '../../../core/classes/service.js';
@@ -113,7 +114,7 @@ export class DockerPublishService extends Service {
113
114
  await exec('docker buildx create --use', {
114
115
  cwd: repository.dirname,
115
116
  app: repository.app,
116
- stdio: 'inherit',
117
+ stdio: 'stderr',
117
118
  throwOnError: false,
118
119
  });
119
120
  const result = [];
@@ -123,9 +124,9 @@ export class DockerPublishService extends Service {
123
124
  continue;
124
125
  }
125
126
  try {
126
- await buildAndPush(repository, entry);
127
+ const digest = await buildAndPush(repository, entry);
127
128
  await updateDescription(entry);
128
- result.push(entry);
129
+ result.push(digest ? { ...entry, artifact: { ref: entry.image, digest } } : entry);
129
130
  }
130
131
  catch (e) {
131
132
  result.push({ ...entry, status: 'error', reason: e.message });
@@ -205,7 +206,7 @@ async function dockerLogin(app, cwd) {
205
206
  await exec(`echo "${password}" | docker login --username "${username}" --password-stdin`, {
206
207
  cwd,
207
208
  app,
208
- stdio: 'inherit',
209
+ stdio: 'stderr',
209
210
  });
210
211
  }
211
212
  async function buildAndPush(repository, entry) {
@@ -234,6 +235,11 @@ async function buildAndPush(repository, entry) {
234
235
  args.push('-f', `"${dockerfile}"`);
235
236
  for (const tag of imageTags(pkg))
236
237
  args.push('-t', `"${entry.image}:${tag}"`);
238
+ /** buildx writes what it pushed here - `containerimage.digest` is the pushed index's own, the
239
+ * address `docker buildx imagetools inspect` reports for the tag right after. */
240
+ const metadataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'rman-buildx-'));
241
+ const metadataFile = path.join(metadataDir, 'metadata.json');
242
+ args.push('--metadata-file', `"${metadataFile}"`);
237
243
  args.push('--push', '.');
238
244
  /** **Shown as it runs and kept**, so a failure can say why. It ran with `stdio: 'inherit'`, and the
239
245
  * one thing left to report was the exit code - measured on `panates/syncbridge`, the line that
@@ -249,8 +255,26 @@ async function buildAndPush(repository, entry) {
249
255
  process.stderr.write(line + '\n');
250
256
  },
251
257
  });
252
- if (result.code)
253
- throw new Error(`docker build exited with code ${result.code}${failureLines(lines)}`);
258
+ try {
259
+ if (result.code)
260
+ throw new Error(`docker build exited with code ${result.code}${failureLines(lines)}`);
261
+ return readPushedDigest(metadataFile);
262
+ }
263
+ finally {
264
+ fs.rmSync(metadataDir, { recursive: true, force: true });
265
+ }
266
+ }
267
+ /** The digest buildx recorded for what it pushed, or `undefined` when its metadata says none. */
268
+ /* Unreadable metadata is not a failed publish - the image is on the registry either way - so it
269
+ * reads as "no digest", which leaves the entry without an `artifact` rather than failing it. */
270
+ function readPushedDigest(file) {
271
+ try {
272
+ const digest = JSON.parse(fs.readFileSync(file, 'utf-8'))['containerimage.digest'];
273
+ return typeof digest === 'string' && /^sha256:[0-9a-f]{64}$/.test(digest) ? digest : undefined;
274
+ }
275
+ catch {
276
+ return undefined;
277
+ }
254
278
  }
255
279
  /**
256
280
  * The lines of a failed build worth putting in a one-paragraph reason: those saying "error", with
@@ -36,8 +36,10 @@ const config = {
36
36
  json: {
37
37
  target: 'cli',
38
38
  alias: 'j',
39
- describe: 'Print the plan as JSON instead of text - one entry per package and target. Combine with ' +
40
- '--dry-run to ask "is there anything to publish?" without publishing (e.g. a CI release gate).',
39
+ describe: 'Print JSON instead of text - one entry per package and target. With --dry-run it is the plan ' +
40
+ '("is there anything to publish?", e.g. a CI release gate); otherwise it is printed once the ' +
41
+ 'run is over and says what happened: "published" entries, with the digest a registry returned ' +
42
+ 'under "artifact".',
41
43
  type: 'boolean',
42
44
  },
43
45
  ignoreDirty: {
@@ -151,10 +153,18 @@ const publishCommand = registerCommand(app => {
151
153
  }
152
154
  }
153
155
  }
154
- if (args.json) {
155
- console.log(JSON.stringify(jsonPlan(plans), undefined, 2));
156
- }
157
- else {
156
+ /** One document per run, whichever way it ends: the plan under `--dry-run` or on any exit
157
+ * before publishing, what happened once it has. */
158
+ let printed = false;
159
+ const printJson = (doc) => {
160
+ if (!args.json || printed)
161
+ return;
162
+ printed = true;
163
+ console.log(JSON.stringify(doc, undefined, 2));
164
+ };
165
+ if (args.dryRun)
166
+ printJson(jsonPlan(plans));
167
+ if (!args.json) {
158
168
  for (const [target, plan] of plans) {
159
169
  printPlan(plan, target.name);
160
170
  printFiles(plan, target);
@@ -165,12 +175,14 @@ const publishCommand = registerCommand(app => {
165
175
  if (requested?.length) {
166
176
  const empty = [...plans].filter(([, plan]) => !plan.length).map(([target]) => target.name);
167
177
  if (empty.length) {
178
+ printJson(jsonPlan(plans));
168
179
  throw logged(`--target ${empty.join(', ')} was given, but no package ships there ` + '(see .rmanrc "publish.target").');
169
180
  }
170
181
  }
171
182
  const all = [...plans.values()].flat();
172
183
  const errors = all.filter(e => e.status === 'error');
173
184
  if (errors.length) {
185
+ printJson(jsonPlan(plans));
174
186
  const allDirty = errors.every(e => e.reason?.startsWith(UNCOMMITTED));
175
187
  throw logged(allDirty
176
188
  ? `${errors.length} package(s) have uncommitted local changes ` +
@@ -178,6 +190,7 @@ const publishCommand = registerCommand(app => {
178
190
  : `${errors.length} package(s) failed to prepare for publish - see the errors above`);
179
191
  }
180
192
  if (!all.some(e => e.status === 'publish')) {
193
+ printJson(jsonPlan(plans));
181
194
  if (!args.json)
182
195
  console.log(colors.gray('Nothing to publish.'));
183
196
  return;
@@ -187,14 +200,18 @@ const publishCommand = registerCommand(app => {
187
200
  let proceed = !!args.yes;
188
201
  if (!proceed) {
189
202
  if (!process.stdout.isTTY) {
190
- console.log(colors.gray('Not a TTY - refusing to prompt. Pass --yes to publish non-interactively.'));
203
+ printJson(jsonPlan(plans));
204
+ console.error(colors.gray('Not a TTY - refusing to prompt. Pass --yes to publish non-interactively.'));
191
205
  return;
192
206
  }
193
207
  proceed = await confirm('Publish these packages?');
194
208
  }
195
- if (!proceed)
209
+ if (!proceed) {
210
+ printJson(jsonPlan(plans));
196
211
  return;
212
+ }
197
213
  const outcomes = [];
214
+ const results = new Map();
198
215
  const publishedBy = new Map();
199
216
  let waited = false;
200
217
  for (const [target, plan] of plans) {
@@ -213,10 +230,12 @@ const publishCommand = registerCommand(app => {
213
230
  finally {
214
231
  staged?.restore();
215
232
  }
216
- const done = printApplied(applied, plan, target.name);
233
+ results.set(target, applied);
234
+ const done = printApplied(applied, plan, target.name, args.json ? console.error : console.log);
217
235
  outcomes.push(...done);
218
236
  publishedBy.set(target, done.filter(o => o.entry.status === 'publish').map(o => o.entry));
219
237
  }
238
+ printJson(jsonResult(plans, results));
220
239
  if (!args.json)
221
240
  printRecap(outcomes);
222
241
  writeStepSummary(outcomes);
@@ -339,6 +358,30 @@ function jsonPlan(plans) {
339
358
  reason: entry.reason,
340
359
  })));
341
360
  }
361
+ /**
362
+ * `--json` once the run has published: the plan's rows, each as it ended - `published` for one that
363
+ * went up (with the registry's `artifact`, where the target reports one), `error` for one that did
364
+ * not, and a row nothing was asked of as it was planned.
365
+ */
366
+ /* **`published`, not the plan's `publish`**, so a document read after the run cannot be mistaken
367
+ * for a plan that has yet to happen. The release workflow counts these and takes the digests from
368
+ * `artifact` - `syncbridge-io/syncbridge` deploys by them. */
369
+ function jsonResult(plans, results) {
370
+ return [...plans].flatMap(([target, plan]) => plan.map(planned => {
371
+ const entry = planned.status === 'publish'
372
+ ? (results.get(target)?.find(e => e.package === planned.package) ?? planned)
373
+ : planned;
374
+ return {
375
+ name: entry.package.name,
376
+ target: target.name,
377
+ status: planned.status === 'publish' && entry.status === 'publish' ? 'published' : entry.status,
378
+ version: entry.version,
379
+ detail: entry.detail,
380
+ reason: entry.reason,
381
+ ...(entry.artifact && { artifact: entry.artifact }),
382
+ };
383
+ }));
384
+ }
342
385
  function printPlan(entries, label) {
343
386
  const prefix = colors.gray(`[${label}] `);
344
387
  for (const e of entries) {
@@ -392,17 +435,17 @@ async function waitForPublished(ctx, publishedBy) {
392
435
  * was already an error before anything ran has been printed once by `printPlan`, and printing it
393
436
  * again under "failed" would read as a push that was attempted and did not work.
394
437
  */
395
- function printApplied(applied, plan, label) {
438
+ function printApplied(applied, plan, label, log) {
396
439
  const prefix = colors.gray(`[${label}] `);
397
440
  const outcomes = [];
398
441
  for (const entry of applied) {
399
442
  if (plan.find(e => e.package === entry.package)?.status !== 'publish')
400
443
  continue;
401
444
  if (entry.status === 'publish') {
402
- console.log(colors.green('published'), prefix + colors.cyan(entry.package.name), entry.detail ?? entry.version);
445
+ log(colors.green('published'), prefix + colors.cyan(entry.package.name), entry.detail ?? entry.version);
403
446
  }
404
447
  else if (entry.status === 'error') {
405
- console.log(colors.red('failed'), prefix + colors.cyan(entry.package.name), colors.red(entry.reason ?? ''));
448
+ log(colors.red('failed'), prefix + colors.cyan(entry.package.name), colors.red(entry.reason ?? ''));
406
449
  }
407
450
  else
408
451
  continue;
package/constants.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.30.0';
1
+ export const version = '2.31.0';
@@ -129,6 +129,15 @@ export declare namespace PublishTarget {
129
129
  /** Printed after the package name - docker's resolved `<namespace>/<image>`, for instance. */
130
130
  detail?: string;
131
131
  reason?: string;
132
+ /**
133
+ * What `applyPlan` actually pushed, when the registry names it by content: `ref` is where it went
134
+ * and `digest` the content address the registry returned. Set by a target on a published entry,
135
+ * never in a plan - nothing has been pushed yet there.
136
+ */
137
+ artifact?: {
138
+ ref: string;
139
+ digest: string;
140
+ };
132
141
  }
133
142
  }
134
143
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rman",
3
3
  "description": "Repository manager",
4
- "version": "2.30.0",
4
+ "version": "2.31.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/utils/exec.d.ts CHANGED
@@ -10,8 +10,10 @@ export interface ExecOptions {
10
10
  */
11
11
  app?: RmanApplication;
12
12
  /** 'inherit' streams the child directly to our stdio (used for non-TTY/CI passthrough).
13
- * 'pipe' (default) captures output so the caller can drive a live view via onLine. */
14
- stdio?: 'inherit' | 'pipe';
13
+ * 'stderr' does the same with the child's stdout sent to our stderr, for a command whose own
14
+ * stdout is a document. 'pipe' (default) captures output so the caller can drive a live view
15
+ * via onLine. */
16
+ stdio?: 'inherit' | 'stderr' | 'pipe';
15
17
  cwd?: string;
16
18
  env?: Record<string, string | undefined>;
17
19
  onLine?: (line: string, stdio: 'stderr' | 'stdout') => void;
package/utils/exec.js CHANGED
@@ -22,7 +22,7 @@ export async function exec(command, options) {
22
22
  };
23
23
  opts.cwd = opts.cwd || process.cwd();
24
24
  const spawnOptions = {
25
- stdio: opts.stdio === 'inherit' ? 'inherit' : 'pipe',
25
+ stdio: opts.stdio === 'inherit' ? 'inherit' : opts.stdio === 'stderr' ? ['inherit', 2, 2] : 'pipe',
26
26
  env: opts.env,
27
27
  cwd: opts.cwd,
28
28
  shell: true,