diff --git a/.github/workflows/integ-test-dependency-graph.yml b/.github/workflows/integ-test-dependency-graph.yml index 684778b..c215c1c 100644 --- a/.github/workflows/integ-test-dependency-graph.yml +++ b/.github/workflows/integ-test-dependency-graph.yml @@ -66,3 +66,33 @@ jobs: uses: ./ with: dependency-graph: download-and-submit + + multiple-builds: + runs-on: "ubuntu-latest" + steps: + - name: Checkout sources + uses: actions/checkout@v3 + - name: Download distribution if required + uses: ./.github/actions/download-dist + - name: Setup Gradle for dependency-graph generate + uses: ./ + with: + dependency-graph: generate + - name: Run assemble + run: ./gradlew assemble + working-directory: .github/workflow-samples/groovy-dsl + env: + GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR: job-correlator + - name: Run build + run: ./gradlew build + working-directory: .github/workflow-samples/groovy-dsl + env: + GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR: job-correlator + - name: Check generated dependency graphs + run: | + ls -l dependency-graph-reports + if ([ ! -e dependency-graph-reports/job-correlator.json ] || [ ! -e dependency-graph-reports/job-correlator-1.json ]) + then + echo "Did not find expected dependency graph files" + exit 1 + fi diff --git a/src/resources/init-scripts/github-dependency-graph-gradle-plugin-apply.groovy b/src/resources/init-scripts/github-dependency-graph-gradle-plugin-apply.groovy index 6f4202b..c031835 100644 --- a/src/resources/init-scripts/github-dependency-graph-gradle-plugin-apply.groovy +++ b/src/resources/init-scripts/github-dependency-graph-gradle-plugin-apply.groovy @@ -3,7 +3,7 @@ buildscript { maven { url "https://plugins.gradle.org/m2/" } } dependencies { - classpath "org.gradle:github-dependency-graph-gradle-plugin:0.0.5" + classpath "org.gradle:github-dependency-graph-gradle-plugin:0.0.6" } } apply plugin: org.gradle.github.GitHubDependencyGraphPlugin diff --git a/src/resources/init-scripts/github-dependency-graph.init.gradle b/src/resources/init-scripts/github-dependency-graph.init.gradle index 6d7eced..79c5b6c 100644 --- a/src/resources/init-scripts/github-dependency-graph.init.gradle +++ b/src/resources/init-scripts/github-dependency-graph.init.gradle @@ -1,19 +1,27 @@ import org.gradle.util.GradleVersion +// Only run against root build. Do not run against included builds. +def isTopLevelBuild = gradle.getParent() == null +if (!isTopLevelBuild) { + return +} + +// Only run when dependency graph is explicitly enabled if (System.env.GITHUB_DEPENDENCY_GRAPH_ENABLED != "true") { return } +// Do not run for unsupported versions of Gradle if (GradleVersion.current().baseVersion < GradleVersion.version("5.0")) { println "::warning::Dependency Graph is not supported for Gradle versions < 5.0. No dependency snapshot will be generated." return } +// Attempt to find a unique job correlator to use based on the environment variable def reportDir = System.env.GITHUB_DEPENDENCY_GRAPH_REPORT_DIR -def jobCorrelator = System.env.GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR -def reportFile = new File(reportDir, jobCorrelator + ".json") +def jobCorrelator = ensureUniqueJobCorrelator(reportDir, System.env.GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR) -if (reportFile.exists()) { +if (jobCorrelator == null) { println "::warning::No dependency snapshot generated for step: report file for '${jobCorrelator}' created in earlier step. Each build invocation requires a unique job correlator: specify GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR var for this step." return } @@ -22,3 +30,27 @@ println "Generating dependency graph for '${jobCorrelator}'" // TODO:DAZ This should be conditionally applied, since the script may be present when not required. apply from: 'github-dependency-graph-gradle-plugin-apply.groovy' + +/** + * Using the supplied jobCorrelator value: + * - Checks if report file already exists + * - If so, tries to find a unique value that does not yet have a corresponding report file. + * - When found, this value is set as a System property override. + */ +String ensureUniqueJobCorrelator(String reportDir, String jobCorrelator) { + def reportFile = new File(reportDir, jobCorrelator + ".json") + if (!reportFile.exists()) return jobCorrelator + + // Try at most 100 suffixes + for (int i = 1; i < 100; i++) { + def candidateCorrelator = jobCorrelator + "-" + i + def candidateFile = new File(reportDir, candidateCorrelator + ".json") + if (!candidateFile.exists()) { + System.properties['org.gradle.github.env.GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR'] = candidateCorrelator + return candidateCorrelator + } + } + + // Could not determine unique job correlator + return null +} diff --git a/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDependencyGraph.groovy b/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDependencyGraph.groovy index 4047c1b..235adbf 100644 --- a/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDependencyGraph.groovy +++ b/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDependencyGraph.groovy @@ -49,18 +49,46 @@ class TestDependencyGraph extends BaseInitScriptTest { testGradleVersion << NO_DEPENDENCY_GRAPH_VERSIONS } - def "warns and does not overwrite existing report file"() { + def "constructs unique job correlator for each build invocation"() { assumeTrue testGradleVersion.compatibleWithCurrentJvm + def reportFile1 = new File(reportsDir, "CORRELATOR-1.json") + def reportFile2 = new File(reportsDir, "CORRELATOR-2.json") + + buildFile << """ + task firstTask { + doLast { + println "First" + } + } + task secondTask { + doLast { + println "Second" + } + } + """ + when: - reportsDir.mkdirs() - reportFile << "DUMMY CONTENT" - def result = run(['help'], initScript, testGradleVersion.gradleVersion, [], envVars) + run(['help'], initScript, testGradleVersion.gradleVersion, [], envVars) then: - assert reportFile.text == "DUMMY CONTENT" - assert result.output.contains("::warning::No dependency snapshot generated for step") + assert reportFile.exists() + when: + run(['first'], initScript, testGradleVersion.gradleVersion, [], envVars) + + then: + assert reportFile.exists() + assert reportFile1.exists() + + when: + run(['second'], initScript, testGradleVersion.gradleVersion, [], envVars) + + then: + assert reportFile.exists() + assert reportFile1.exists() + assert reportFile2.exists() + where: testGradleVersion << DEPENDENCY_GRAPH_VERSIONS }