From a9a5bcf180d5613c28888d17b1d267ec6c1af23c Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Thu, 2 Jun 2022 22:39:09 -0600 Subject: [PATCH] Move writing of cache report into job-summary.ts --- src/cache-reporting.ts | 5 ++--- src/caches.ts | 15 +++------------ src/job-summary.ts | 16 +++++++++++++--- src/post.ts | 3 --- src/setup-gradle.ts | 16 ++++++++++++++-- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/cache-reporting.ts b/src/cache-reporting.ts index 767c13e..ed607cf 100644 --- a/src/cache-reporting.ts +++ b/src/cache-reporting.ts @@ -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 {
 ${entries}
 
+ ` ) - - core.summary.write() } function getCount( diff --git a/src/caches.ts b/src/caches.ts index 47afc64..a5f774f 100644 --- a/src/caches.ts +++ b/src/caches.ts @@ -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 { +export async function restore(gradleUserHome: string, cacheListener: CacheListener): Promise { // 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 { } 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 { +export async function save(gradleUserHome: string, cacheListener: CacheListener): Promise { 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 { diff --git a/src/job-summary.ts b/src/job-summary.ts index 709c462..eba8bed 100644 --- a/src/job-summary.ts +++ b/src/job-summary.ts @@ -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 { diff --git a/src/post.ts b/src/post.ts index cebdeab..d81a8ca 100644 --- a/src/post.ts +++ b/src/post.ts @@ -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 { try { await setupGradle.complete() - - writeJobSummary() } catch (error) { handleFailure(error) } diff --git a/src/setup-gradle.ts b/src/setup-gradle.ts index dc093e3..993dd65 100644 --- a/src/setup-gradle.ts +++ b/src/setup-gradle.ts @@ -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 { const gradleUserHome = determineGradleUserHome(buildRootDirectory) @@ -22,17 +25,26 @@ export async function setup(buildRootDirectory: string): Promise { // 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 { + 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 {