Skip to content

Commit

Permalink
feat(binary-builder): defer version asset upload (#1089)
Browse files Browse the repository at this point in the history
fix: defer version asset upload
  • Loading branch information
viceice authored Nov 25, 2024
1 parent 202a347 commit ac65579
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
43 changes: 30 additions & 13 deletions src/commands/binary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -61,18 +67,6 @@ export async function run(): Promise<void> {
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);
Expand All @@ -92,6 +86,8 @@ export async function run(): Promise<void> {
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
Expand Down Expand Up @@ -134,6 +130,8 @@ export async function run(): Promise<void> {
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
Expand All @@ -149,3 +147,22 @@ export async function run(): Promise<void> {
setFailed(error as Error);
}
}

async function uploadVersionAssetIfNeeded(
api: GitHubOctokit,
version: string,
cfg: BinaryBuilderConfig,
builds: BuildsResult,
): Promise<void> {
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);
}
}
}
2 changes: 1 addition & 1 deletion src/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { BinaryBuilderConfig } from './types';

export { getOctokit };

type GitHubOctokit = InstanceType<typeof GitHub>;
export type GitHubOctokit = InstanceType<typeof GitHub>;

interface GhAsset {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion test/commands/binary/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit ac65579

Please sign in to comment.