2022-06-02 14:17:56 -04:00
|
|
|
import * as core from '@actions/core'
|
2022-06-05 11:07:34 -04:00
|
|
|
import * as exec from '@actions/exec'
|
|
|
|
import * as fs from 'fs'
|
2022-06-02 14:17:56 -04:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as os from 'os'
|
|
|
|
import * as caches from './caches'
|
2022-06-05 11:07:34 -04:00
|
|
|
|
2022-06-03 00:39:09 -04:00
|
|
|
import {CacheListener} from './cache-reporting'
|
2022-06-05 11:07:34 -04:00
|
|
|
import {BuildResult, loadBuildResults, writeJobSummary} from './job-summary'
|
2022-06-02 14:17:56 -04:00
|
|
|
|
|
|
|
const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
|
|
|
|
const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
|
2022-06-03 00:39:09 -04:00
|
|
|
const CACHE_LISTENER = 'CACHE_LISTENER'
|
2022-06-02 14:17:56 -04:00
|
|
|
|
|
|
|
export async function setup(buildRootDirectory: string): Promise<void> {
|
|
|
|
const gradleUserHome = determineGradleUserHome(buildRootDirectory)
|
|
|
|
|
|
|
|
// 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 00:39:09 -04:00
|
|
|
const cacheListener = new CacheListener()
|
|
|
|
await caches.restore(gradleUserHome, cacheListener)
|
|
|
|
|
|
|
|
core.saveState(CACHE_LISTENER, cacheListener.stringify())
|
2022-06-02 14:17:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function complete(): Promise<void> {
|
2022-06-03 00:39:09 -04:00
|
|
|
core.info('Inside setupGradle.complete()')
|
2022-06-02 14:17:56 -04:00
|
|
|
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-05 11:07:34 -04:00
|
|
|
const buildResults = loadBuildResults()
|
|
|
|
|
|
|
|
core.info('Stopping all Gradle daemons')
|
|
|
|
await stopAllDaemons(getUniqueGradleHomes(buildResults))
|
|
|
|
|
2022-06-03 00:39:09 -04:00
|
|
|
core.info('In final post-action step, saving state and writing summary')
|
|
|
|
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
|
|
|
|
|
2022-06-02 14:17:56 -04:00
|
|
|
const gradleUserHome = core.getState(GRADLE_USER_HOME)
|
2022-06-03 00:39:09 -04:00
|
|
|
await caches.save(gradleUserHome, cacheListener)
|
|
|
|
|
2022-06-05 11:07:34 -04:00
|
|
|
writeJobSummary(buildResults, cacheListener)
|
2022-06-02 14:17:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function determineGradleUserHome(rootDir: string): string {
|
|
|
|
const customGradleUserHome = process.env['GRADLE_USER_HOME']
|
|
|
|
if (customGradleUserHome) {
|
|
|
|
return path.resolve(rootDir, customGradleUserHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.resolve(os.homedir(), '.gradle')
|
|
|
|
}
|
2022-06-05 11:07:34 -04:00
|
|
|
|
|
|
|
function getUniqueGradleHomes(buildResults: BuildResult[]): string[] {
|
|
|
|
const gradleHomes = buildResults.map(buildResult => buildResult.gradleHomeDir)
|
|
|
|
return Array.from(new Set(gradleHomes))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stopAllDaemons(gradleHomes: string[]): Promise<void> {
|
|
|
|
const executions: Promise<number>[] = []
|
|
|
|
const args = ['--stop']
|
|
|
|
|
|
|
|
for (const gradleHome of gradleHomes) {
|
|
|
|
const executable = path.resolve(gradleHome, 'bin', 'gradle')
|
|
|
|
if (!fs.existsSync(executable)) {
|
|
|
|
core.warning(`Gradle executable not found at ${executable}. Could not stop Gradle daemons.`)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
core.info(`Stopping Gradle daemons in ${gradleHome}`)
|
|
|
|
executions.push(
|
|
|
|
exec.exec(executable, args, {
|
|
|
|
ignoreReturnCode: true
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
await Promise.all(executions)
|
|
|
|
}
|