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

View file

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

View file

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

View file

@ -1,6 +1,5 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as setupGradle from './setup-gradle' 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 // 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 // @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> { export async function run(): Promise<void> {
try { try {
await setupGradle.complete() await setupGradle.complete()
writeJobSummary()
} catch (error) { } catch (error) {
handleFailure(error) handleFailure(error)
} }

View file

@ -2,9 +2,12 @@ import * as core from '@actions/core'
import * as path from 'path' import * as path from 'path'
import * as os from 'os' import * as os from 'os'
import * as caches from './caches' 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_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
const GRADLE_USER_HOME = 'GRADLE_USER_HOME' const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
const CACHE_LISTENER = 'CACHE_LISTENER'
export async function setup(buildRootDirectory: string): Promise<void> { export async function setup(buildRootDirectory: string): Promise<void> {
const gradleUserHome = determineGradleUserHome(buildRootDirectory) 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. // Save the Gradle User Home for use in the post-action step.
core.saveState(GRADLE_USER_HOME, gradleUserHome) 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> { export async function complete(): Promise<void> {
core.info('Inside setupGradle.complete()')
if (!core.getState(GRADLE_SETUP_VAR)) { if (!core.getState(GRADLE_SETUP_VAR)) {
core.info('Gradle setup post-action only performed for first gradle-build-action step in workflow.') core.info('Gradle setup post-action only performed for first gradle-build-action step in workflow.')
return 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) const gradleUserHome = core.getState(GRADLE_USER_HOME)
await caches.save(gradleUserHome) await caches.save(gradleUserHome, cacheListener)
writeJobSummary(cacheListener)
} }
function determineGradleUserHome(rootDir: string): string { function determineGradleUserHome(rootDir: string): string {