mirror of
https://github.com/gradle/gradle-build-action.git
synced 2024-12-27 01:02:11 -05:00
Merge pull request #302 from gradle/fix-config-cache
Fix save/restore of configuration-cache
This commit is contained in:
commit
8b56c4af06
9 changed files with 70 additions and 19 deletions
|
@ -195,7 +195,7 @@ By default, this action aims to cache any and all reusable state that may be spe
|
||||||
The state that is cached includes:
|
The state that is cached includes:
|
||||||
- Any distributions downloaded to satisfy a `gradle-version` parameter ;
|
- Any distributions downloaded to satisfy a `gradle-version` parameter ;
|
||||||
- A subset of the Gradle User Home directory, including downloaded dependencies, wrapper distributions, and the local build cache ;
|
- A subset of the Gradle User Home directory, including downloaded dependencies, wrapper distributions, and the local build cache ;
|
||||||
- Any [configuration-cache](https://docs.gradle.org/nightly/userguide/configuration_cache.html) data stored in the project `.gradle` directory.
|
- Any [configuration-cache](https://docs.gradle.org/nightly/userguide/configuration_cache.html) data stored in the project `.gradle` directory. (Only supported for Gradle 7 or higher.)
|
||||||
|
|
||||||
To reduce the space required for caching, this action makes a best effort to reduce duplication in cache entries.
|
To reduce the space required for caching, this action makes a best effort to reduce duplication in cache entries.
|
||||||
|
|
||||||
|
|
6
dist/main/index.js
vendored
6
dist/main/index.js
vendored
|
@ -64965,7 +64965,11 @@ class GradleStateCache {
|
||||||
initializeGradleUserHome(gradleUserHome, initScriptsDir) {
|
initializeGradleUserHome(gradleUserHome, initScriptsDir) {
|
||||||
const propertiesFile = path_1.default.resolve(gradleUserHome, 'gradle.properties');
|
const propertiesFile = path_1.default.resolve(gradleUserHome, 'gradle.properties');
|
||||||
fs_1.default.appendFileSync(propertiesFile, 'org.gradle.daemon=false');
|
fs_1.default.appendFileSync(propertiesFile, 'org.gradle.daemon=false');
|
||||||
const initScriptFilenames = ['build-result-capture.init.gradle', 'project-root-capture.init.gradle'];
|
const initScriptFilenames = [
|
||||||
|
'build-result-capture.init.gradle',
|
||||||
|
'project-root-capture.init.gradle',
|
||||||
|
'project-root-capture.plugin.groovy'
|
||||||
|
];
|
||||||
for (const initScriptFilename of initScriptFilenames) {
|
for (const initScriptFilename of initScriptFilenames) {
|
||||||
const initScriptContent = this.readResourceAsString(initScriptFilename);
|
const initScriptContent = this.readResourceAsString(initScriptFilename);
|
||||||
const initScriptPath = path_1.default.resolve(initScriptsDir, initScriptFilename);
|
const initScriptPath = path_1.default.resolve(initScriptsDir, initScriptFilename);
|
||||||
|
|
2
dist/main/index.js.map
vendored
2
dist/main/index.js.map
vendored
File diff suppressed because one or more lines are too long
6
dist/post/index.js
vendored
6
dist/post/index.js
vendored
|
@ -64016,7 +64016,11 @@ class GradleStateCache {
|
||||||
initializeGradleUserHome(gradleUserHome, initScriptsDir) {
|
initializeGradleUserHome(gradleUserHome, initScriptsDir) {
|
||||||
const propertiesFile = path_1.default.resolve(gradleUserHome, 'gradle.properties');
|
const propertiesFile = path_1.default.resolve(gradleUserHome, 'gradle.properties');
|
||||||
fs_1.default.appendFileSync(propertiesFile, 'org.gradle.daemon=false');
|
fs_1.default.appendFileSync(propertiesFile, 'org.gradle.daemon=false');
|
||||||
const initScriptFilenames = ['build-result-capture.init.gradle', 'project-root-capture.init.gradle'];
|
const initScriptFilenames = [
|
||||||
|
'build-result-capture.init.gradle',
|
||||||
|
'project-root-capture.init.gradle',
|
||||||
|
'project-root-capture.plugin.groovy'
|
||||||
|
];
|
||||||
for (const initScriptFilename of initScriptFilenames) {
|
for (const initScriptFilename of initScriptFilenames) {
|
||||||
const initScriptContent = this.readResourceAsString(initScriptFilename);
|
const initScriptContent = this.readResourceAsString(initScriptFilename);
|
||||||
const initScriptPath = path_1.default.resolve(initScriptsDir, initScriptFilename);
|
const initScriptPath = path_1.default.resolve(initScriptsDir, initScriptFilename);
|
||||||
|
|
2
dist/post/index.js.map
vendored
2
dist/post/index.js.map
vendored
File diff suppressed because one or more lines are too long
|
@ -170,7 +170,11 @@ export class GradleStateCache {
|
||||||
const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
|
const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
|
||||||
fs.appendFileSync(propertiesFile, 'org.gradle.daemon=false')
|
fs.appendFileSync(propertiesFile, 'org.gradle.daemon=false')
|
||||||
|
|
||||||
const initScriptFilenames = ['build-result-capture.init.gradle', 'project-root-capture.init.gradle']
|
const initScriptFilenames = [
|
||||||
|
'build-result-capture.init.gradle',
|
||||||
|
'project-root-capture.init.gradle',
|
||||||
|
'project-root-capture.plugin.groovy'
|
||||||
|
]
|
||||||
for (const initScriptFilename of initScriptFilenames) {
|
for (const initScriptFilename of initScriptFilenames) {
|
||||||
const initScriptContent = this.readResourceAsString(initScriptFilename)
|
const initScriptContent = this.readResourceAsString(initScriptFilename)
|
||||||
const initScriptPath = path.resolve(initScriptsDir, initScriptFilename)
|
const initScriptPath = path.resolve(initScriptsDir, initScriptFilename)
|
||||||
|
@ -179,7 +183,7 @@ export class GradleStateCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
private readResourceAsString(resource: string): string {
|
private readResourceAsString(resource: string): string {
|
||||||
// Resolving relative to __dirname will force the compiler to inline the content in the distribution
|
// Resolving relative to __dirname will allow node to find the resource at runtime
|
||||||
const absolutePath = path.resolve(__dirname, '..', '..', 'src', 'resources', resource)
|
const absolutePath = path.resolve(__dirname, '..', '..', 'src', 'resources', resource)
|
||||||
return fs.readFileSync(absolutePath, 'utf8')
|
return fs.readFileSync(absolutePath, 'utf8')
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,10 @@ if (isTopLevelBuild) {
|
||||||
|
|
||||||
def registerCallbacks(buildScanExtension, rootProjectName) {
|
def registerCallbacks(buildScanExtension, rootProjectName) {
|
||||||
buildScanExtension.with {
|
buildScanExtension.with {
|
||||||
|
def requestedTasks = gradle.startParameter.taskNames.join(" ")
|
||||||
|
def gradleVersion = GradleVersion.current().version
|
||||||
def buildFailed = false
|
def buildFailed = false
|
||||||
|
|
||||||
buildFinished { result ->
|
buildFinished { result ->
|
||||||
buildFailed = (result.failure != null)
|
buildFailed = (result.failure != null)
|
||||||
}
|
}
|
||||||
|
@ -38,8 +41,6 @@ def registerCallbacks(buildScanExtension, rootProjectName) {
|
||||||
|
|
||||||
def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + System.currentTimeMillis() + ".json")
|
def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + System.currentTimeMillis() + ".json")
|
||||||
|
|
||||||
def requestedTasks = gradle.startParameter.taskNames.join(" ")
|
|
||||||
def gradleVersion = GradleVersion.current().version
|
|
||||||
def buildScanUri = buildScan.buildScanUri.toASCIIString()
|
def buildScanUri = buildScan.buildScanUri.toASCIIString()
|
||||||
def buildResults = [
|
def buildResults = [
|
||||||
rootProject: rootProjectName,
|
rootProject: rootProjectName,
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
// Capture the build root directory for each executed Gradle build.
|
import org.gradle.util.GradleVersion
|
||||||
|
|
||||||
// Only run against root build. Do not run against included builds.
|
// Only run against root build. Do not run against included builds.
|
||||||
def isTopLevelBuild = gradle.getParent() == null
|
def isTopLevelBuild = gradle.getParent() == null
|
||||||
if (isTopLevelBuild) {
|
// Only record configuration-cache entries for Gradle 7+
|
||||||
settingsEvaluated { settings ->
|
def isAtLeastGradle7 = GradleVersion.current() >= GradleVersion.version('7.0')
|
||||||
def projectRootEntry = settings.rootDir.absolutePath + '\n'
|
|
||||||
def projectRootList = new File(settings.gradle.gradleUserHomeDir, "project-roots.txt")
|
if (isTopLevelBuild && isAtLeastGradle7) {
|
||||||
if (!projectRootList.exists() || !projectRootList.text.contains(projectRootEntry)) {
|
apply from: 'project-root-capture.plugin.groovy'
|
||||||
projectRootList << projectRootEntry
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
40
src/resources/project-root-capture.plugin.groovy
Normal file
40
src/resources/project-root-capture.plugin.groovy
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Capture the build root directory for each executed Gradle build.
|
||||||
|
* This is used to save/restore configuration-cache files, so:
|
||||||
|
* - The implementation only makes sense if it's config-cache compatible
|
||||||
|
* - We only need to support Gradle 7+
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.gradle.tooling.events.*
|
||||||
|
|
||||||
|
settingsEvaluated { settings ->
|
||||||
|
def rootDir = settings.rootDir.absolutePath
|
||||||
|
def rootListLocation = new File(settings.gradle.gradleUserHomeDir, "project-roots.txt").absolutePath
|
||||||
|
|
||||||
|
def projectTracker = gradle.sharedServices.registerIfAbsent("gradle-build-action-projectRootTracker", ProjectTracker, { spec ->
|
||||||
|
spec.getParameters().getRootDir().set(rootDir);
|
||||||
|
spec.getParameters().getRootListLocation().set(rootListLocation);
|
||||||
|
})
|
||||||
|
|
||||||
|
gradle.services.get(BuildEventsListenerRegistry).onTaskCompletion(projectTracker)
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class ProjectTracker implements BuildService<ProjectTracker.Params>, OperationCompletionListener, AutoCloseable {
|
||||||
|
interface Params extends BuildServiceParameters {
|
||||||
|
Property<String> getRootDir();
|
||||||
|
Property<String> getRootListLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onFinish(FinishEvent finishEvent) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
def rootDir = getParameters().getRootDir().get()
|
||||||
|
def rootDirEntry = rootDir + '\n'
|
||||||
|
def rootListFile = new File(getParameters().getRootListLocation().get())
|
||||||
|
if (!rootListFile.exists() || !rootListFile.text.contains(rootDirEntry)) {
|
||||||
|
rootListFile << rootDirEntry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue