2022-06-02 18:17:56 +00:00
|
|
|
import * as core from '@actions/core'
|
2022-06-05 15:07:34 +00:00
|
|
|
import * as exec from '@actions/exec'
|
2022-06-09 15:06:27 +00:00
|
|
|
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
|
2022-06-02 18:17:56 +00:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as os from 'os'
|
|
|
|
import * as caches from './caches'
|
2023-06-03 20:09:52 +00:00
|
|
|
import * as layout from './repository-layout'
|
2023-06-03 21:58:54 +00:00
|
|
|
import * as params from './input-params'
|
2023-07-02 01:00:28 +00:00
|
|
|
import * as dependencyGraph from './dependency-graph'
|
2022-06-05 15:07:34 +00:00
|
|
|
|
2022-06-22 22:35:55 +00:00
|
|
|
import {logJobSummary, writeJobSummary} from './job-summary'
|
|
|
|
import {loadBuildResults} from './build-results'
|
2022-06-03 04:39:09 +00:00
|
|
|
import {CacheListener} from './cache-reporting'
|
2022-06-22 22:35:55 +00:00
|
|
|
import {DaemonController} from './daemon-controller'
|
2022-06-02 18:17:56 +00:00
|
|
|
|
|
|
|
const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
|
|
|
|
const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
|
2022-06-03 04:39:09 +00:00
|
|
|
const CACHE_LISTENER = 'CACHE_LISTENER'
|
2022-06-06 13:13:23 +00:00
|
|
|
|
2023-06-03 20:09:52 +00:00
|
|
|
export async function setup(): Promise<void> {
|
|
|
|
const gradleUserHome = await determineGradleUserHome()
|
2022-06-02 18:17:56 +00:00
|
|
|
|
|
|
|
// Bypass setup on all but first action step in workflow.
|
|
|
|
if (process.env[GRADLE_SETUP_VAR]) {
|
|
|
|
core.info('Gradle setup only performed on first gradle-build-action step in workflow.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Record setup complete: visible to all subsequent actions and prevents duplicate setup
|
|
|
|
core.exportVariable(GRADLE_SETUP_VAR, true)
|
|
|
|
// Record setup complete: visible in post-action, to control action completion
|
|
|
|
core.saveState(GRADLE_SETUP_VAR, true)
|
|
|
|
|
|
|
|
// Save the Gradle User Home for use in the post-action step.
|
|
|
|
core.saveState(GRADLE_USER_HOME, gradleUserHome)
|
|
|
|
|
2022-06-03 04:39:09 +00:00
|
|
|
const cacheListener = new CacheListener()
|
|
|
|
await caches.restore(gradleUserHome, cacheListener)
|
|
|
|
|
|
|
|
core.saveState(CACHE_LISTENER, cacheListener.stringify())
|
2023-07-02 01:00:28 +00:00
|
|
|
|
|
|
|
if (params.isDependencyGraphEnabled()) {
|
|
|
|
dependencyGraph.prepare()
|
|
|
|
}
|
2022-06-02 18:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function complete(): Promise<void> {
|
|
|
|
if (!core.getState(GRADLE_SETUP_VAR)) {
|
|
|
|
core.info('Gradle setup post-action only performed for first gradle-build-action step in workflow.')
|
|
|
|
return
|
|
|
|
}
|
2022-06-22 22:35:55 +00:00
|
|
|
core.info('In final post-action step, saving state and writing summary')
|
2022-06-02 18:17:56 +00:00
|
|
|
|
2022-06-05 15:07:34 +00:00
|
|
|
const buildResults = loadBuildResults()
|
|
|
|
|
2022-06-22 22:35:55 +00:00
|
|
|
const gradleUserHome = core.getState(GRADLE_USER_HOME)
|
2022-06-03 04:39:09 +00:00
|
|
|
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
|
2022-06-22 22:35:55 +00:00
|
|
|
const daemonController = new DaemonController(buildResults)
|
2022-06-03 04:39:09 +00:00
|
|
|
|
2022-06-22 22:39:34 +00:00
|
|
|
await caches.save(gradleUserHome, cacheListener, daemonController)
|
2022-06-03 04:39:09 +00:00
|
|
|
|
2022-06-06 15:26:49 +00:00
|
|
|
if (shouldGenerateJobSummary()) {
|
2022-06-20 23:53:30 +00:00
|
|
|
await writeJobSummary(buildResults, cacheListener)
|
|
|
|
} else {
|
|
|
|
logJobSummary(buildResults, cacheListener)
|
2022-06-06 13:13:23 +00:00
|
|
|
}
|
2023-07-02 01:00:28 +00:00
|
|
|
|
|
|
|
if (params.isDependencyGraphEnabled()) {
|
|
|
|
dependencyGraph.uploadDependencyGraphs()
|
|
|
|
}
|
2022-06-02 18:17:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 20:09:52 +00:00
|
|
|
async function determineGradleUserHome(): Promise<string> {
|
2022-06-02 18:17:56 +00:00
|
|
|
const customGradleUserHome = process.env['GRADLE_USER_HOME']
|
|
|
|
if (customGradleUserHome) {
|
2023-06-03 20:09:52 +00:00
|
|
|
const rootDir = layout.workspaceDirectory()
|
2022-06-02 18:17:56 +00:00
|
|
|
return path.resolve(rootDir, customGradleUserHome)
|
|
|
|
}
|
|
|
|
|
2022-06-06 01:03:43 +00:00
|
|
|
return path.resolve(await determineUserHome(), '.gradle')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Different values can be returned by os.homedir() in Javascript and System.getProperty('user.home') in Java.
|
|
|
|
* In order to determine the correct Gradle User Home, we ask Java for the user home instead of using os.homedir().
|
|
|
|
*/
|
|
|
|
async function determineUserHome(): Promise<string> {
|
|
|
|
const output = await exec.getExecOutput('java', ['-XshowSettings:properties', '-version'], {silent: true})
|
|
|
|
const regex = /user\.home = (\S*)/i
|
|
|
|
const found = output.stderr.match(regex)
|
|
|
|
if (found == null || found.length <= 1) {
|
|
|
|
core.info('Could not determine user.home from java -version output. Using os.homedir().')
|
|
|
|
return os.homedir()
|
|
|
|
}
|
|
|
|
const userHome = found[1]
|
|
|
|
core.debug(`Determined user.home from java -version output: '${userHome}'`)
|
|
|
|
return userHome
|
2022-06-02 18:17:56 +00:00
|
|
|
}
|
2023-06-03 20:09:52 +00:00
|
|
|
|
|
|
|
function shouldGenerateJobSummary(): boolean {
|
|
|
|
// Check if Job Summary is supported on this platform
|
|
|
|
if (!process.env[SUMMARY_ENV_VAR]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-03 21:58:54 +00:00
|
|
|
return params.isJobSummaryEnabled()
|
2023-06-03 20:09:52 +00:00
|
|
|
}
|