From 2b1b59f35a3c4ef4467d921e924e67b22969498a Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Tue, 17 Aug 2021 10:42:27 -0700 Subject: [PATCH] Time for build and pull phases (#18) --- dist/index.js | 66 +++++++++++++++++++++++++++++++++++--------------- src/helpers.ts | 31 ++++++++++++++++++++++++ src/index.ts | 53 ++++++++++++++++++++++------------------ 3 files changed, 107 insertions(+), 43 deletions(-) diff --git a/dist/index.js b/dist/index.js index 510384c..a8c186e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7682,14 +7682,40 @@ async function runDockerCommand(command, ...args) { stdout, }; } +async function time(name, timedFunction) { + const start = Date.now(); + try { + return await timedFunction(); + } + finally { + const durationMs = Date.now() - start; + const formattedDuration = formatMs(durationMs); + core.info(`${name} completed in ${formattedDuration}`); + } +} +function formatMs(ms) { + if (ms < 1000) { + return `${ms}ms`; + } + const seconds = Math.floor(ms / 1000); + if (seconds < 60) { + return `${ms / 1000}s`; + } + const minutes = Math.floor(seconds / 60); + if (minutes < 60) { + return `${minutes}m ${seconds % 60}s`; + } + const hours = Math.floor(minutes / 60); + return `${hours}h ${minutes % 60}m`; +} ;// CONCATENATED MODULE: ./src/index.ts async function run() { try { - await core.group('Pull images for layer cache', pull); - await core.group('Build', build); + await time('Pull images', () => core.group('Pull images for layer cache', pull)); + await time('Full Build', build); } catch (error) { core.setFailed(error.message); @@ -7754,23 +7780,25 @@ async function build() { * a tag specific to the ref/branch that the action is run on. */ async function buildStage(stage, extraTags) { - core.startGroup(`Building stage: ${stage}`); - const dockerfile = core.getInput('dockerfile'); - const targetTag = getTaggedImageForStage(stage, getTagForRun()); - const cacheFromArg = getAllPossibleCacheTargets() - .flatMap(target => ['--cache-from', target]); - const result = await runDockerCommand('build', - // '--build-arg', 'BUILDKIT_INLINE_CACHE="1"', - ...cacheFromArg, '--file', dockerfile, '--tag', targetTag, '--target', stage, '.'); - if (result.exitCode > 0) { - throw 'Docker build failed'; - } - await dockerPush(targetTag); - for (const extraTag of extraTags) { - await addTagAndPush(targetTag, stage, extraTag); - } - core.endGroup(); - return targetTag; + return time(`Build ${stage}`, async () => { + core.startGroup(`Building stage: ${stage}`); + const dockerfile = core.getInput('dockerfile'); + const targetTag = getTaggedImageForStage(stage, getTagForRun()); + const cacheFromArg = getAllPossibleCacheTargets() + .flatMap(target => ['--cache-from', target]); + const result = await runDockerCommand('build', + // '--build-arg', 'BUILDKIT_INLINE_CACHE="1"', + ...cacheFromArg, '--file', dockerfile, '--tag', targetTag, '--target', stage, '.'); + if (result.exitCode > 0) { + throw 'Docker build failed'; + } + await dockerPush(targetTag); + for (const extraTag of extraTags) { + await addTagAndPush(targetTag, stage, extraTag); + } + core.endGroup(); + return targetTag; + }); } async function dockerPush(taggedImage) { core.debug(`Pushing ${taggedImage}`); diff --git a/src/helpers.ts b/src/helpers.ts index 4f511ac..2ce2ee8 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -110,3 +110,34 @@ export async function runDockerCommand(command: DockerCommand, ...args: string[] stdout, } } + + +export async function time(name: string, timedFunction: () => Promise): Promise { + const start = Date.now() + try { + return await timedFunction() + } finally { + const durationMs = Date.now() - start + + const formattedDuration = formatMs(durationMs) + + core.info(`${name} completed in ${formattedDuration}`) + } +} + +function formatMs(ms: number): string { + if (ms < 1000) { + return `${ms}ms` + } + const seconds = Math.floor(ms / 1000) + if (seconds < 60) { + return `${ms / 1000}s` + } + const minutes = Math.floor(seconds / 60) + if (minutes < 60) { + return `${minutes}m ${seconds % 60}s` + } + + const hours = Math.floor(minutes / 60) + return `${hours}h ${minutes % 60}m` +} diff --git a/src/index.ts b/src/index.ts index 87dc23c..29ecf90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,12 +8,15 @@ import { getAllStages, getTaggedImageForStage, runDockerCommand, + time, } from './helpers' async function run(): Promise { try { - await core.group('Pull images for layer cache', pull) - await core.group('Build', build) + await time('Pull images', () => + core.group('Pull images for layer cache', pull) + ) + await time('Full Build', build) } catch (error) { core.setFailed(error.message) } @@ -84,34 +87,36 @@ async function build(): Promise { * a tag specific to the ref/branch that the action is run on. */ async function buildStage(stage: string, extraTags: string[]): Promise { - core.startGroup(`Building stage: ${stage}`) + return time(`Build ${stage}`, async () => { + core.startGroup(`Building stage: ${stage}`) - const dockerfile = core.getInput('dockerfile') + const dockerfile = core.getInput('dockerfile') - const targetTag = getTaggedImageForStage(stage, getTagForRun()) + const targetTag = getTaggedImageForStage(stage, getTagForRun()) - const cacheFromArg = getAllPossibleCacheTargets() + const cacheFromArg = getAllPossibleCacheTargets() .flatMap(target => ['--cache-from', target]) - const result = await runDockerCommand( - 'build', - // '--build-arg', 'BUILDKIT_INLINE_CACHE="1"', - ...cacheFromArg, - '--file', dockerfile, - '--tag', targetTag, - '--target', stage, - '.' - ) - if (result.exitCode > 0) { - throw 'Docker build failed' - } - await dockerPush(targetTag) + const result = await runDockerCommand( + 'build', + // '--build-arg', 'BUILDKIT_INLINE_CACHE="1"', + ...cacheFromArg, + '--file', dockerfile, + '--tag', targetTag, + '--target', stage, + '.' + ) + if (result.exitCode > 0) { + throw 'Docker build failed' + } + await dockerPush(targetTag) - for (const extraTag of extraTags) { - await addTagAndPush(targetTag, stage, extraTag) - } - core.endGroup() - return targetTag + for (const extraTag of extraTags) { + await addTagAndPush(targetTag, stage, extraTag) + } + core.endGroup() + return targetTag + }) } async function dockerPush(taggedImage: string): Promise {