From ac6557930fe38098f5e570172caeef1c49d37f2c Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Mon, 25 Nov 2024 16:40:36 +0100 Subject: [PATCH] feat(binary-builder): defer version asset upload (#1089) fix: defer version asset upload --- src/commands/binary/index.ts | 43 +++++++++++++++++++++--------- src/utils/github.ts | 2 +- test/commands/binary/index.spec.ts | 2 +- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/commands/binary/index.ts b/src/commands/binary/index.ts index 7e9d02416..7dfa552ad 100644 --- a/src/commands/binary/index.ts +++ b/src/commands/binary/index.ts @@ -2,9 +2,14 @@ import { mkdir } from 'node:fs/promises'; import { setFailed } from '@actions/core'; import chalk from 'chalk'; import { getArg, getWorkspace } from '../../util'; -import { addHostRule, getBuildList } from '../../utils/builds'; +import { + type BuildsResult, + addHostRule, + getBuildList, +} from '../../utils/builds'; import { init } from '../../utils/docker/buildx'; import { + type GitHubOctokit, downloadAsset, getOctokit, hasAsset, @@ -15,6 +20,7 @@ import { } from '../../utils/github'; import log from '../../utils/logger'; import { createChecksum } from '../../utils/sum'; +import type { BinaryBuilderConfig } from '../../utils/types'; import { createBuilderImage, getConfig, runBuilder } from './utils'; let toBuild = 99; @@ -61,18 +67,6 @@ export async function run(): Promise { await updateRelease(api, cfg, version, builds.latestStable); } - if (!(await hasVersionAsset(api, version))) { - if (cfg.dryRun) { - log.warn( - chalk.yellow('[DRY_RUN] Would upload version asset:'), - builds.latestStable ?? version, - ); - } else { - log('Uploading version file:', builds.latestStable ?? version); - await uploadVersionAsset(api, cfg, version, builds.latestStable); - } - } - if (await hasAsset(api, cfg, version)) { if (!(await hasAsset(api, cfg, version, true))) { log('Creating checksum for existing version:', version); @@ -92,6 +86,8 @@ export async function run(): Promise { log('Uploading release:', version); await uploadAsset(api, cfg, version, builds.latestStable, true); } + + await uploadVersionAssetIfNeeded(api, version, cfg, builds); } catch (e) { failed.push(version); // eslint-disable-next-line @@ -134,6 +130,8 @@ export async function run(): Promise { await uploadAsset(api, cfg, version, builds.latestStable); await uploadAsset(api, cfg, version, builds.latestStable, true); } + + await uploadVersionAssetIfNeeded(api, version, cfg, builds); } catch (e) { failed.push(version); // eslint-disable-next-line @@ -149,3 +147,22 @@ export async function run(): Promise { setFailed(error as Error); } } + +async function uploadVersionAssetIfNeeded( + api: GitHubOctokit, + version: string, + cfg: BinaryBuilderConfig, + builds: BuildsResult, +): Promise { + if (!(await hasVersionAsset(api, version))) { + if (cfg.dryRun) { + log.warn( + chalk.yellow('[DRY_RUN] Would upload version asset:'), + builds.latestStable ?? version, + ); + } else { + log('Uploading version file:', builds.latestStable ?? version); + await uploadVersionAsset(api, cfg, version, builds.latestStable); + } + } +} diff --git a/src/utils/github.ts b/src/utils/github.ts index 3620a9315..780a3a012 100644 --- a/src/utils/github.ts +++ b/src/utils/github.ts @@ -11,7 +11,7 @@ import type { BinaryBuilderConfig } from './types'; export { getOctokit }; -type GitHubOctokit = InstanceType; +export type GitHubOctokit = InstanceType; interface GhAsset { name: string; diff --git a/test/commands/binary/index.spec.ts b/test/commands/binary/index.spec.ts index 713d33ca8..b13282043 100644 --- a/test/commands/binary/index.spec.ts +++ b/test/commands/binary/index.spec.ts @@ -48,7 +48,7 @@ describe('commands/binary/index', () => { await run(); expect(docker.dockerRun).not.toHaveBeenCalled(); - expect(github.uploadVersionAsset).toHaveBeenCalled(); + expect(github.uploadVersionAsset).not.toHaveBeenCalled(); }); it('works ruby (dry-run)', async () => {