Move writing of cache report into job-summary.ts

This commit is contained in:
Daz DeBoer 2022-06-02 22:39:09 -06:00
parent f9c8fcf79f
commit a9a5bcf180
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
5 changed files with 32 additions and 23 deletions

View file

@ -92,7 +92,7 @@ export function logCachingReport(listener: CacheListener): void {
return
}
core.summary.addHeading('Caching Summary')
core.summary.addHeading('Caching Summary', 3)
const entries = listener.cacheEntries
.map(
@ -129,10 +129,9 @@ export function logCachingReport(listener: CacheListener): void {
<pre>
${entries}
</pre>
`
)
core.summary.write()
}
function getCount(

View file

@ -1,12 +1,11 @@
import * as core from '@actions/core'
import {isCacheDisabled, isCacheReadOnly, isCacheWriteOnly} from './cache-utils'
import {logCachingReport, CacheListener} from './cache-reporting'
import {CacheListener} from './cache-reporting'
import {GradleStateCache} from './cache-base'
const CACHE_RESTORED_VAR = 'GRADLE_BUILD_ACTION_CACHE_RESTORED'
const CACHE_LISTENER = 'CACHE_LISTENER'
export async function restore(gradleUserHome: string): Promise<void> {
export async function restore(gradleUserHome: string, cacheListener: CacheListener): Promise<void> {
// Bypass restore cache on all but first action step in workflow.
if (process.env[CACHE_RESTORED_VAR]) {
core.info('Cache only restored on first action step.')
@ -40,31 +39,23 @@ export async function restore(gradleUserHome: string): Promise<void> {
}
await core.group('Restore Gradle state from cache', async () => {
const cacheListener = new CacheListener()
await gradleStateCache.restore(cacheListener)
core.saveState(CACHE_LISTENER, cacheListener.stringify())
})
}
export async function save(gradleUserHome: string): Promise<void> {
export async function save(gradleUserHome: string, cacheListener: CacheListener): Promise<void> {
if (!shouldSaveCaches()) {
return
}
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
if (isCacheReadOnly()) {
core.info('Cache is read-only: will not save state for use in subsequent builds.')
logCachingReport(cacheListener)
return
}
await core.group('Caching Gradle state', async () => {
return new GradleStateCache(gradleUserHome).save(cacheListener)
})
logCachingReport(cacheListener)
}
function shouldSaveCaches(): boolean {

View file

@ -1,6 +1,7 @@
import * as core from '@actions/core'
import fs from 'fs'
import path from 'path'
import {logCachingReport, CacheListener} from './cache-reporting'
interface BuildResult {
get rootProject(): string
@ -10,13 +11,21 @@ interface BuildResult {
get buildScanUri(): string
}
export function writeJobSummary(): void {
export function writeJobSummary(cacheListener: CacheListener): void {
core.info('Writing job summary...')
const buildResults = loadBuildResults()
if (buildResults.length === 0) {
core.debug('No Gradle build results found. Summary table will not be generated.')
} else {
core.info('Writing summary table')
writeSummaryTable(buildResults)
}
core.info('Writing cache report...')
logCachingReport(cacheListener)
core.summary.write()
}
function loadBuildResults(): BuildResult[] {
@ -34,8 +43,9 @@ function loadBuildResults(): BuildResult[] {
}
function writeSummaryTable(results: BuildResult[]): void {
core.summary.addRaw('\n')
core.summary.addHeading('Gradle Builds', 3)
core.summary.addRaw(`| Root Project | Tasks | Gradle Version | Outcome |\n| - | - | - | - |\n`)
core.summary.addRaw('\n| Root Project | Tasks | Gradle Version | Outcome |\n| - | - | - | - |\n')
for (const result of results) {
const tableRow = `| ${result.rootProject} \
| ${result.requestedTasks} \
@ -44,7 +54,7 @@ function writeSummaryTable(results: BuildResult[]): void {
|\n`
core.summary.addRaw(tableRow)
}
core.summary.write()
core.summary.addRaw('\n')
}
function renderOutcome(result: BuildResult): string {

View file

@ -1,6 +1,5 @@
import * as core from '@actions/core'
import * as setupGradle from './setup-gradle'
import {writeJobSummary} from './job-summary'
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
@ -13,8 +12,6 @@ process.on('uncaughtException', e => handleFailure(e))
export async function run(): Promise<void> {
try {
await setupGradle.complete()
writeJobSummary()
} catch (error) {
handleFailure(error)
}

View file

@ -2,9 +2,12 @@ import * as core from '@actions/core'
import * as path from 'path'
import * as os from 'os'
import * as caches from './caches'
import {CacheListener} from './cache-reporting'
import {writeJobSummary} from './job-summary'
const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
const CACHE_LISTENER = 'CACHE_LISTENER'
export async function setup(buildRootDirectory: string): Promise<void> {
const gradleUserHome = determineGradleUserHome(buildRootDirectory)
@ -22,17 +25,26 @@ export async function setup(buildRootDirectory: string): Promise<void> {
// Save the Gradle User Home for use in the post-action step.
core.saveState(GRADLE_USER_HOME, gradleUserHome)
await caches.restore(gradleUserHome)
const cacheListener = new CacheListener()
await caches.restore(gradleUserHome, cacheListener)
core.saveState(CACHE_LISTENER, cacheListener.stringify())
}
export async function complete(): Promise<void> {
core.info('Inside setupGradle.complete()')
if (!core.getState(GRADLE_SETUP_VAR)) {
core.info('Gradle setup post-action only performed for first gradle-build-action step in workflow.')
return
}
core.info('In final post-action step, saving state and writing summary')
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
const gradleUserHome = core.getState(GRADLE_USER_HOME)
await caches.save(gradleUserHome)
await caches.save(gradleUserHome, cacheListener)
writeJobSummary(cacheListener)
}
function determineGradleUserHome(rootDir: string): string {