From f4f7af32dcbd69fa941db9ae41be0fbe016a2f47 Mon Sep 17 00:00:00 2001 From: hfhbd Date: Mon, 29 Jan 2024 23:21:40 +0100 Subject: [PATCH] Configure --info and --stacktrace when GitHub ACTIONS_RUNNER_DEBUG is true Fixes #6 --- setup-gradle/README.md | 11 ++++++- sources/src/cache-base.ts | 21 +++++++++++++ sources/test/jest/cache-debug.test.ts | 30 +++++++++++++++++++ .../resources/gradle-home/empty/.gitignore | 1 + .../gradle-home/existing/gradle.properties | 1 + 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 sources/test/jest/cache-debug.test.ts create mode 100644 sources/test/jest/resources/gradle-home/empty/.gitignore create mode 100644 sources/test/jest/resources/gradle-home/existing/gradle.properties diff --git a/setup-gradle/README.md b/setup-gradle/README.md index 89c5789..ae59f33 100644 --- a/setup-gradle/README.md +++ b/setup-gradle/README.md @@ -193,6 +193,16 @@ Specifically: Using either of these mechanisms may interfere with the caching provided by this action. If you choose to use a different mechanism to save and restore the Gradle User Home, you should disable the caching provided by this action, as described above. +### GitHub Action Debug support +To debug a failed job, GitHub provides a [debug mode](https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging). +If this debug mode is active, this action adds `--info` and `--stacktrace` by writing these [Gradle properties](https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties) into the `${GRADLE_USER_HOME}/gradle.properties` file at the top. + +To opt-out of this behaviour you can override these properties manually and put them into your `gradle.properties` file: +```properties +# default lifecycle +org.gradle.logging.level=lifecycle +org.gradle.logging.stacktrace=internal +``` ### Cache debugging and analysis A report of all cache entries restored and saved is printed to the Job Summary when saving the cache entries. @@ -688,4 +698,3 @@ jobs: - name: Run a Gradle build - a build scan will be published automatically run: ./gradlew build ``` - diff --git a/sources/src/cache-base.ts b/sources/src/cache-base.ts index 1a84f55..5aad7a2 100644 --- a/sources/src/cache-base.ts +++ b/sources/src/cache-base.ts @@ -192,6 +192,10 @@ export class GradleStateCache { // Copy the default toolchain definitions to `~/.m2/toolchains.xml` this.registerToolchains() + + if (core.isDebug()) { + this.configureInfoLogLevel() + } } private copyInitScripts(): void { @@ -240,6 +244,23 @@ export class GradleStateCache { return fs.readFileSync(absolutePath, 'utf8') } + /** + * When the GitHub environment ACTIONS_RUNNER_DEBUG is true, run Gradle with --info and --stacktrace. + * see https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging + */ + configureInfoLogLevel(): void { + const infoProperties = `org.gradle.logging.level=info\norg.gradle.logging.stacktrace=all\n` + const propertiesFile = path.resolve(this.gradleUserHome, 'gradle.properties') + if (fs.existsSync(propertiesFile)) { + core.info(`Merged --info and --stacktrace into existing ${propertiesFile} file`) + const existingProperties = fs.readFileSync(propertiesFile, 'utf-8') + fs.writeFileSync(propertiesFile, `${infoProperties}\n${existingProperties}`) + } else { + core.info(`Created a new ${propertiesFile} with --info and --stacktrace`) + fs.writeFileSync(propertiesFile, infoProperties) + } + } + /** * When cache debugging is enabled, this method will give a detailed report * of the Gradle User Home contents. diff --git a/sources/test/jest/cache-debug.test.ts b/sources/test/jest/cache-debug.test.ts new file mode 100644 index 0000000..b86d671 --- /dev/null +++ b/sources/test/jest/cache-debug.test.ts @@ -0,0 +1,30 @@ +import {GradleStateCache} from "../../src/cache-base" +import * as path from 'path' +import * as fs from 'fs' + +describe("--info and --stacktrace", () => { + describe("will be created", () => { + it("when gradle.properties does not exists", async () => { + const emptyGradleHome = 'test/jest/resources/gradle-home/empty' + fs.rmSync(path.resolve(emptyGradleHome, "gradle.properties"), {force: true}) + + const stateCache = new GradleStateCache("ignored", emptyGradleHome) + stateCache.configureInfoLogLevel() + + expect(fs.readFileSync(path.resolve(emptyGradleHome, "gradle.properties"), 'utf-8')) + .toBe("org.gradle.logging.level=info\norg.gradle.logging.stacktrace=all\n") + }) + }) + describe("will be added", () => { + it("and gradle.properties does exists", async () => { + const existingGradleHome = 'test/jest/resources/gradle-home/existing' + fs.writeFileSync(path.resolve(existingGradleHome, "gradle.properties"), "org.gradle.logging.level=debug\n") + + const stateCache = new GradleStateCache("ignored", existingGradleHome) + stateCache.configureInfoLogLevel() + + expect(fs.readFileSync(path.resolve(existingGradleHome, "gradle.properties"), 'utf-8')) + .toBe("org.gradle.logging.level=info\norg.gradle.logging.stacktrace=all\n\norg.gradle.logging.level=debug\n") + }) + }) +}) diff --git a/sources/test/jest/resources/gradle-home/empty/.gitignore b/sources/test/jest/resources/gradle-home/empty/.gitignore new file mode 100644 index 0000000..f2dc359 --- /dev/null +++ b/sources/test/jest/resources/gradle-home/empty/.gitignore @@ -0,0 +1 @@ +gradle.properties diff --git a/sources/test/jest/resources/gradle-home/existing/gradle.properties b/sources/test/jest/resources/gradle-home/existing/gradle.properties new file mode 100644 index 0000000..d6587bf --- /dev/null +++ b/sources/test/jest/resources/gradle-home/existing/gradle.properties @@ -0,0 +1 @@ +org.gradle.logging.level=debug