Support multiple invocations in dependency-graph init script

If an existing dependency graph file is present for the configured job correlator,
we now generate a unique correlator value for the invocation. This allows the action
to submit dependency snapshots for a series of Gradle invocations within the same Job.

This commit updates to `github-dependency-graph-gradle-plugin@v0.0.6`, which reduces
redundancy in the mapping of resolved Gradle dependencies to the GitHub Dependency Graph.
This commit is contained in:
daz 2023-07-15 18:06:38 -06:00
parent 3c11eee5f9
commit b69de5f2a9
No known key found for this signature in database
4 changed files with 100 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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
}