Skip to content

Commit

Permalink
Time for build and pull phases (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firehed authored Aug 17, 2021
1 parent 8967204 commit 2b1b59f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 43 deletions.
66 changes: 47 additions & 19 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,34 @@ export async function runDockerCommand(command: DockerCommand, ...args: string[]
stdout,
}
}


export async function time<T>(name: string, timedFunction: () => Promise<T>): Promise<T> {
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`
}
53 changes: 29 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
getAllStages,
getTaggedImageForStage,
runDockerCommand,
time,
} from './helpers'

async function run(): Promise<void> {
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)
}
Expand Down Expand Up @@ -84,34 +87,36 @@ async function build(): Promise<void> {
* a tag specific to the ref/branch that the action is run on.
*/
async function buildStage(stage: string, extraTags: string[]): Promise<string> {
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<void> {
Expand Down

0 comments on commit 2b1b59f

Please sign in to comment.