mirror of
https://github.com/gradle/actions.git
synced 2025-08-18 23:01:27 +08:00
Move execution-related code into subpackage
This commit is contained in:
parent
528fe78d31
commit
3335c16182
@ -9,9 +9,8 @@ import type {PullRequestEvent} from '@octokit/webhooks-types'
|
|||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
import * as layout from './repository-layout'
|
|
||||||
import {PostActionJobFailure} from './errors'
|
import {PostActionJobFailure} from './errors'
|
||||||
import {DependencyGraphConfig, DependencyGraphOption, getGithubToken} from './input-params'
|
import {DependencyGraphConfig, DependencyGraphOption, getGithubToken, getWorkspaceDirectory} from './input-params'
|
||||||
|
|
||||||
const DEPENDENCY_GRAPH_PREFIX = 'dependency-graph_'
|
const DEPENDENCY_GRAPH_PREFIX = 'dependency-graph_'
|
||||||
|
|
||||||
@ -34,10 +33,10 @@ export async function setup(config: DependencyGraphConfig): Promise<void> {
|
|||||||
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_JOB_ID', github.context.runId)
|
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_JOB_ID', github.context.runId)
|
||||||
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_REF', github.context.ref)
|
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_REF', github.context.ref)
|
||||||
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_SHA', getShaFromContext())
|
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_SHA', getShaFromContext())
|
||||||
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_WORKSPACE', layout.workspaceDirectory())
|
maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_WORKSPACE', getWorkspaceDirectory())
|
||||||
maybeExportVariable(
|
maybeExportVariable(
|
||||||
'DEPENDENCY_GRAPH_REPORT_DIR',
|
'DEPENDENCY_GRAPH_REPORT_DIR',
|
||||||
path.resolve(layout.workspaceDirectory(), 'dependency-graph-reports')
|
path.resolve(getWorkspaceDirectory(), 'dependency-graph-reports')
|
||||||
)
|
)
|
||||||
|
|
||||||
// To clear the dependency graph, we generate an empty graph by excluding all projects and configurations
|
// To clear the dependency graph, we generate an empty graph by excluding all projects and configurations
|
||||||
@ -74,7 +73,7 @@ export async function complete(config: DependencyGraphConfig): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function findGeneratedDependencyGraphFiles(): Promise<string[]> {
|
async function findGeneratedDependencyGraphFiles(): Promise<string[]> {
|
||||||
const workspaceDirectory = layout.workspaceDirectory()
|
const workspaceDirectory = getWorkspaceDirectory()
|
||||||
return await findDependencyGraphFiles(workspaceDirectory)
|
return await findDependencyGraphFiles(workspaceDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +84,7 @@ async function uploadDependencyGraphs(dependencyGraphFiles: string[], config: De
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const workspaceDirectory = layout.workspaceDirectory()
|
const workspaceDirectory = getWorkspaceDirectory()
|
||||||
|
|
||||||
const artifactClient = new DefaultArtifactClient()
|
const artifactClient = new DefaultArtifactClient()
|
||||||
for (const dependencyGraphFile of dependencyGraphFiles) {
|
for (const dependencyGraphFile of dependencyGraphFiles) {
|
||||||
@ -157,7 +156,7 @@ async function submitDependencyGraphFile(jsonFile: string): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function downloadDependencyGraphs(): Promise<string[]> {
|
async function downloadDependencyGraphs(): Promise<string[]> {
|
||||||
const workspaceDirectory = layout.workspaceDirectory()
|
const workspaceDirectory = getWorkspaceDirectory()
|
||||||
|
|
||||||
const findBy = github.context.payload.workflow_run
|
const findBy = github.context.payload.workflow_run
|
||||||
? {
|
? {
|
||||||
@ -220,7 +219,7 @@ function getOctokit(): InstanceType<typeof GitHub> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getRelativePathFromWorkspace(file: string): string {
|
function getRelativePathFromWorkspace(file: string): string {
|
||||||
const workspaceDirectory = layout.workspaceDirectory()
|
const workspaceDirectory = getWorkspaceDirectory()
|
||||||
return path.relative(workspaceDirectory, file)
|
return path.relative(workspaceDirectory, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
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 * as execution from '../execution'
|
import * as gradle from '../execution/gradle'
|
||||||
import * as provisioner from '../provision'
|
|
||||||
import * as layout from '../repository-layout'
|
|
||||||
import * as dependencyGraph from '../dependency-graph'
|
import * as dependencyGraph from '../dependency-graph'
|
||||||
|
|
||||||
import {parseArgsStringToArgv} from 'string-argv'
|
import {parseArgsStringToArgv} from 'string-argv'
|
||||||
@ -26,9 +24,6 @@ export async function run(): Promise<void> {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download and install Gradle if required
|
|
||||||
const executable = await provisioner.provisionGradle()
|
|
||||||
|
|
||||||
// Only execute if arguments have been provided
|
// Only execute if arguments have been provided
|
||||||
const additionalArgs = core.getInput('additional-arguments')
|
const additionalArgs = core.getInput('additional-arguments')
|
||||||
const executionArgs = `
|
const executionArgs = `
|
||||||
@ -38,10 +33,8 @@ export async function run(): Promise<void> {
|
|||||||
:ForceDependencyResolutionPlugin_resolveAllDependencies
|
:ForceDependencyResolutionPlugin_resolveAllDependencies
|
||||||
${additionalArgs}
|
${additionalArgs}
|
||||||
`
|
`
|
||||||
|
|
||||||
const args: string[] = parseArgsStringToArgv(executionArgs)
|
const args: string[] = parseArgsStringToArgv(executionArgs)
|
||||||
const buildRootDirectory = layout.buildRootDirectory()
|
await gradle.provisionAndMaybeExecute(args)
|
||||||
await execution.executeGradleBuild(executable, buildRootDirectory, args)
|
|
||||||
|
|
||||||
await dependencyGraph.complete(config)
|
await dependencyGraph.complete(config)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
import * as core from '@actions/core'
|
|
||||||
import * as exec from '@actions/exec'
|
|
||||||
import * as gradlew from './gradlew'
|
|
||||||
|
|
||||||
export async function executeGradleBuild(executable: string | undefined, root: string, args: string[]): Promise<void> {
|
|
||||||
// Use the provided executable, or look for a Gradle wrapper script to run
|
|
||||||
const toExecute = executable ?? gradlew.gradleWrapperScript(root)
|
|
||||||
|
|
||||||
const status: number = await exec.exec(toExecute, args, {
|
|
||||||
cwd: root,
|
|
||||||
ignoreReturnCode: true
|
|
||||||
})
|
|
||||||
|
|
||||||
if (status !== 0) {
|
|
||||||
core.setFailed(`Gradle build failed: see console output for details`)
|
|
||||||
}
|
|
||||||
}
|
|
42
sources/src/execution/gradle.ts
Normal file
42
sources/src/execution/gradle.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import * as core from '@actions/core'
|
||||||
|
import * as exec from '@actions/exec'
|
||||||
|
import * as path from 'path'
|
||||||
|
|
||||||
|
import * as params from '../input-params'
|
||||||
|
import * as provisioner from './provision'
|
||||||
|
import * as gradlew from './gradlew'
|
||||||
|
import {getWorkspaceDirectory} from '../input-params'
|
||||||
|
|
||||||
|
export async function provisionAndMaybeExecute(args: string[]): Promise<void> {
|
||||||
|
// Download and install Gradle if required
|
||||||
|
const executable = await provisioner.provisionGradle()
|
||||||
|
|
||||||
|
// Only execute if arguments have been provided
|
||||||
|
if (args.length > 0) {
|
||||||
|
await executeGradleBuild(executable, buildRootDirectory(), args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function executeGradleBuild(executable: string | undefined, root: string, args: string[]): Promise<void> {
|
||||||
|
// Use the provided executable, or look for a Gradle wrapper script to run
|
||||||
|
const toExecute = executable ?? gradlew.gradleWrapperScript(root)
|
||||||
|
|
||||||
|
const status: number = await exec.exec(toExecute, args, {
|
||||||
|
cwd: root,
|
||||||
|
ignoreReturnCode: true
|
||||||
|
})
|
||||||
|
|
||||||
|
if (status !== 0) {
|
||||||
|
core.setFailed(`Gradle build failed: see console output for details`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildRootDirectory(): string {
|
||||||
|
const baseDirectory = getWorkspaceDirectory()
|
||||||
|
const buildRootDirectoryInput = params.getBuildRootDirectory()
|
||||||
|
const resolvedBuildRootDirectory =
|
||||||
|
buildRootDirectoryInput === ''
|
||||||
|
? path.resolve(baseDirectory)
|
||||||
|
: path.resolve(baseDirectory, buildRootDirectoryInput)
|
||||||
|
return resolvedBuildRootDirectory
|
||||||
|
}
|
@ -7,9 +7,9 @@ import * as cache from '@actions/cache'
|
|||||||
import * as toolCache from '@actions/tool-cache'
|
import * as toolCache from '@actions/tool-cache'
|
||||||
|
|
||||||
import * as gradlew from './gradlew'
|
import * as gradlew from './gradlew'
|
||||||
import * as params from './input-params'
|
import * as params from '../input-params'
|
||||||
import {handleCacheFailure} from './caching/cache-utils'
|
import {handleCacheFailure} from '../caching/cache-utils'
|
||||||
import {CacheConfig} from './input-params'
|
import {CacheConfig} from '../input-params'
|
||||||
|
|
||||||
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
||||||
|
|
@ -240,6 +240,10 @@ export function getGithubToken(): string {
|
|||||||
return core.getInput('github-token', {required: true})
|
return core.getInput('github-token', {required: true})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getWorkspaceDirectory(): string {
|
||||||
|
return process.env[`GITHUB_WORKSPACE`] || ''
|
||||||
|
}
|
||||||
|
|
||||||
export function parseNumericInput(paramName: string, paramValue: string, paramDefault: number): number {
|
export function parseNumericInput(paramName: string, paramValue: string, paramDefault: number): number {
|
||||||
if (paramValue.length === 0) {
|
if (paramValue.length === 0) {
|
||||||
return paramDefault
|
return paramDefault
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
import * as params from './input-params'
|
|
||||||
import * as path from 'path'
|
|
||||||
|
|
||||||
export function workspaceDirectory(): string {
|
|
||||||
return process.env[`GITHUB_WORKSPACE`] || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildRootDirectory(): string {
|
|
||||||
const baseDirectory = workspaceDirectory()
|
|
||||||
const buildRootDirectoryInput = params.getBuildRootDirectory()
|
|
||||||
const resolvedBuildRootDirectory =
|
|
||||||
buildRootDirectoryInput === ''
|
|
||||||
? path.resolve(baseDirectory)
|
|
||||||
: path.resolve(baseDirectory, buildRootDirectoryInput)
|
|
||||||
return resolvedBuildRootDirectory
|
|
||||||
}
|
|
@ -3,14 +3,13 @@ import * as exec from '@actions/exec'
|
|||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as caches from './caching/caches'
|
import * as caches from './caching/caches'
|
||||||
import * as layout from './repository-layout'
|
|
||||||
import * as jobSummary from './job-summary'
|
import * as jobSummary from './job-summary'
|
||||||
import * as buildScan from './build-scan'
|
import * as buildScan from './build-scan'
|
||||||
|
|
||||||
import {loadBuildResults} from './build-results'
|
import {loadBuildResults} from './build-results'
|
||||||
import {CacheListener, generateCachingReport} from './caching/cache-reporting'
|
import {CacheListener, generateCachingReport} from './caching/cache-reporting'
|
||||||
import {DaemonController} from './daemon-controller'
|
import {DaemonController} from './daemon-controller'
|
||||||
import {BuildScanConfig, CacheConfig, SummaryConfig} from './input-params'
|
import {BuildScanConfig, CacheConfig, SummaryConfig, getWorkspaceDirectory} from './input-params'
|
||||||
|
|
||||||
const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
|
const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
|
||||||
const USER_HOME = 'USER_HOME'
|
const USER_HOME = 'USER_HOME'
|
||||||
@ -72,7 +71,7 @@ export async function complete(cacheConfig: CacheConfig, summaryConfig: SummaryC
|
|||||||
async function determineGradleUserHome(): Promise<string> {
|
async function determineGradleUserHome(): Promise<string> {
|
||||||
const customGradleUserHome = process.env['GRADLE_USER_HOME']
|
const customGradleUserHome = process.env['GRADLE_USER_HOME']
|
||||||
if (customGradleUserHome) {
|
if (customGradleUserHome) {
|
||||||
const rootDir = layout.workspaceDirectory()
|
const rootDir = getWorkspaceDirectory()
|
||||||
return path.resolve(rootDir, customGradleUserHome)
|
return path.resolve(rootDir, customGradleUserHome)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
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 * as execution from '../execution'
|
import * as gradle from '../execution/gradle'
|
||||||
import * as provisioner from '../provision'
|
|
||||||
import * as layout from '../repository-layout'
|
|
||||||
import * as dependencyGraph from '../dependency-graph'
|
import * as dependencyGraph from '../dependency-graph'
|
||||||
import {BuildScanConfig, CacheConfig, DependencyGraphConfig, getArguments} from '../input-params'
|
import {BuildScanConfig, CacheConfig, DependencyGraphConfig, getArguments} from '../input-params'
|
||||||
|
|
||||||
@ -18,15 +16,8 @@ export async function run(): Promise<void> {
|
|||||||
// Configure the dependency graph submission
|
// Configure the dependency graph submission
|
||||||
await dependencyGraph.setup(new DependencyGraphConfig())
|
await dependencyGraph.setup(new DependencyGraphConfig())
|
||||||
|
|
||||||
// Download and install Gradle if required
|
|
||||||
const executable = await provisioner.provisionGradle()
|
|
||||||
|
|
||||||
// Only execute if arguments have been provided
|
|
||||||
const args: string[] = getArguments()
|
const args: string[] = getArguments()
|
||||||
if (args.length > 0) {
|
await gradle.provisionAndMaybeExecute(args)
|
||||||
const buildRootDirectory = layout.buildRootDirectory()
|
|
||||||
await execution.executeGradleBuild(executable, buildRootDirectory, args)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(String(error))
|
core.setFailed(String(error))
|
||||||
if (error instanceof Error && error.stack) {
|
if (error instanceof Error && error.stack) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user