|
| 1 | +import org.gradle.util.GradleVersion |
| 2 | +import java.nio.charset.StandardCharsets |
| 3 | + |
| 4 | +static getInputParam(String name) { |
| 5 | + def ENV_VAR_PREFIX = '' |
| 6 | + def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_') |
| 7 | + return System.getProperty(name) ?: System.getenv(envVarName) |
| 8 | +} |
| 9 | + |
| 10 | +def isTopLevelBuild = !gradle.parent |
| 11 | +if (!isTopLevelBuild) { |
| 12 | + return |
| 13 | +} |
| 14 | + |
| 15 | +def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan' |
| 16 | +def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise' |
| 17 | +def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity' |
| 18 | +def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin' |
| 19 | + |
| 20 | +def develocityUrl = getInputParam('develocity.url') |
| 21 | + |
| 22 | +def expDir = getInputParam('develocity.build-validation.expDir') |
| 23 | +def expId = getInputParam('develocity.build-validation.expId') |
| 24 | +def runId = getInputParam('develocity.build-validation.runId') |
| 25 | +def scriptsVersion = getInputParam('develocity.build-validation.scriptsVersion') |
| 26 | + |
| 27 | +def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0') |
| 28 | + |
| 29 | +// register build scan listeners to capture build scan URL/id and to track publishing errors |
| 30 | +def registerBuildScanActions = { def buildScan, def rootProjectName -> |
| 31 | + buildScan.buildScanPublished { publishedBuildScan -> |
| 32 | + // defer reading the runNum system property until execution time since it does not affect |
| 33 | + // the configuration of the build, and given its value changes between consecutive build invocations |
| 34 | + // it would always invalidate the configuration cache model from the first build invocation |
| 35 | + // in the second build invocation |
| 36 | + def getInputParam = { String name -> |
| 37 | + def ENV_VAR_PREFIX = '' |
| 38 | + def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_') |
| 39 | + return System.getProperty(name) ?: System.getenv(envVarName) |
| 40 | + } |
| 41 | + def runNum = getInputParam('develocity.build-validation.runNum') |
| 42 | + def buildScanUri = publishedBuildScan.buildScanUri |
| 43 | + def buildScanId = publishedBuildScan.buildScanId |
| 44 | + def port = (buildScanUri.port != -1) ? ':' + buildScanUri.port : '' |
| 45 | + def baseUrl = "${buildScanUri.scheme}://${buildScanUri.host}${port}" |
| 46 | + |
| 47 | + def scanFile = new File(expDir, 'build-scans.csv') |
| 48 | + scanFile.append("${runNum},${rootProjectName},${baseUrl},${buildScanUri},${buildScanId}\n") |
| 49 | + } |
| 50 | + |
| 51 | + buildScan.onError { error -> |
| 52 | + def errorFile = new File(expDir, 'errors.txt') |
| 53 | + errorFile.text = 'Build Scan publishing failed.' |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// add custom data identifying the experiment |
| 58 | +def addBuildScanCustomData = { def buildScan, def server -> |
| 59 | + addCustomValueAndSearchLink(buildScan, server, "Experiment id", expId) |
| 60 | + buildScan.tag(expId) |
| 61 | + |
| 62 | + addCustomValueAndSearchLink(buildScan, server, "Experiment run id", runId) |
| 63 | + |
| 64 | + buildScan.value("Build validation scripts", scriptsVersion) |
| 65 | +} |
| 66 | + |
| 67 | +// fail if no server is configured |
| 68 | +def failMissingDevelocityServerURL = { def docs -> |
| 69 | + def errorFile = new File(expDir, 'errors.txt') |
| 70 | + errorFile.text = 'The Develocity server URL has not been configured in the project or on the command line.' |
| 71 | + throw new IllegalStateException("The Develocity server URL is not configured.\n" |
| 72 | + + "Either configure it directly (see $docs) in the project,\n" |
| 73 | + + "or use --gradle-enterprise-server when running the build validation script.") |
| 74 | +} |
| 75 | + |
| 76 | +// fail if a plugin is not applied |
| 77 | +def failMissingPlugin = { def plugin, docs -> |
| 78 | + def errorFile = new File(expDir, 'errors.txt') |
| 79 | + errorFile.text = "The $plugin plugin is missing from the project." |
| 80 | + throw new IllegalStateException("The $plugin plugin is missing from the project.\n" + |
| 81 | + "Either apply it directly (see $docs),\n" + |
| 82 | + "or use --enable-gradle-enterprise when running the build validation script.") |
| 83 | +} |
| 84 | + |
| 85 | +// do not fail if the CCUD plugin is not applied but surface a warning |
| 86 | +def warnMissingCommonCustomUserDataGradlePlugin = { |
| 87 | + def warningFile = new File(expDir, 'warnings.txt') |
| 88 | + warningFile.append("The com.gradle.common-custom-user-data-gradle-plugin plugin is missing from " + |
| 89 | + "the project (see https://github.com/gradle/common-custom-user-data-gradle-plugin).\n") |
| 90 | +} |
| 91 | + |
| 92 | +if (GradleVersion.current() < GradleVersion.version('6.0')) { |
| 93 | + //noinspection GroovyAssignabilityCheck |
| 94 | + rootProject { |
| 95 | + afterEvaluate { |
| 96 | + if (!pluginManager.hasPlugin(BUILD_SCAN_PLUGIN_ID) && !pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) { |
| 97 | + if (atLeastGradle5) { |
| 98 | + failMissingPlugin(DEVELOCITY_PLUGIN_ID, 'https://docs.gradle.com/develocity/gradle-plugin/current/#gradle_5_x') |
| 99 | + } else { |
| 100 | + failMissingPlugin(BUILD_SCAN_PLUGIN_ID, 'https://docs.gradle.com/develocity/gradle-plugin/legacy/#gradle_2_1_4_10_3') |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!pluginManager.hasPlugin(CCUD_PLUGIN_ID)) { |
| 105 | + warnMissingCommonCustomUserDataGradlePlugin() |
| 106 | + } |
| 107 | + |
| 108 | + pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) { |
| 109 | + // only execute if Develocity plugin isn't applied |
| 110 | + if (pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) return |
| 111 | + |
| 112 | + if (develocityUrl) { |
| 113 | + buildScan.server = develocityUrl |
| 114 | + } |
| 115 | + |
| 116 | + if (!buildScan.server) { |
| 117 | + failMissingDevelocityServerURL('https://docs.gradle.com/develocity/gradle-plugin/legacy#gradle_5_x_2') |
| 118 | + } |
| 119 | + |
| 120 | + buildScan.publishAlways() |
| 121 | + registerBuildScanActions(buildScan, rootProject.name) |
| 122 | + addBuildScanCustomData(buildScan, buildScan.server) |
| 123 | + } |
| 124 | + |
| 125 | + pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) { |
| 126 | + if (develocityUrl) { |
| 127 | + develocity.server = develocityUrl |
| 128 | + } |
| 129 | + |
| 130 | + if (!develocity.server.present) { |
| 131 | + develocity.buildScan.publishing.onlyIf { false } // prevent publishing to scans.gradle.com |
| 132 | + failMissingDevelocityServerURL('https://docs.gradle.com/develocity/gradle-plugin/current/#connecting_to_develocity') |
| 133 | + } |
| 134 | + |
| 135 | + registerBuildScanActions(develocity.buildScan, rootProject.name) |
| 136 | + addBuildScanCustomData(develocity.buildScan, develocity.server.get()) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} else { |
| 141 | + gradle.settingsEvaluated { settings -> |
| 142 | + if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID) && !settings.pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) { |
| 143 | + failMissingPlugin(DEVELOCITY_PLUGIN_ID, 'https://docs.gradle.com/develocity/gradle-plugin/current/#gradle_6_x_and_later') |
| 144 | + } |
| 145 | + |
| 146 | + if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) { |
| 147 | + warnMissingCommonCustomUserDataGradlePlugin() |
| 148 | + } |
| 149 | + |
| 150 | + settings.pluginManager.withPlugin(GRADLE_ENTERPRISE_PLUGIN_ID) { |
| 151 | + // only execute if Develocity plugin isn't applied |
| 152 | + if (settings.pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) return |
| 153 | + |
| 154 | + if (develocityUrl) { |
| 155 | + settings.gradleEnterprise.server = develocityUrl |
| 156 | + } |
| 157 | + |
| 158 | + if (!settings.gradleEnterprise.server) { |
| 159 | + failMissingDevelocityServerURL('https://docs.gradle.com/develocity/gradle-plugin/legacy/#gradle_6_x_and_later_2') |
| 160 | + } |
| 161 | + |
| 162 | + settings.gradleEnterprise.buildScan.publishAlways() |
| 163 | + registerBuildScanActions(settings.gradleEnterprise.buildScan, settings.rootProject.name) |
| 164 | + addBuildScanCustomData(settings.gradleEnterprise.buildScan, settings.gradleEnterprise.server) |
| 165 | + } |
| 166 | + |
| 167 | + settings.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) { |
| 168 | + if (develocityUrl) { |
| 169 | + settings.develocity.server = develocityUrl |
| 170 | + } |
| 171 | + |
| 172 | + if (!settings.develocity.server.present) { |
| 173 | + settings.develocity.buildScan.publishing.onlyIf { false } // prevent publishing to scans.gradle.com |
| 174 | + failMissingDevelocityServerURL('https://docs.gradle.com/develocity/gradle-plugin/current/#connecting_to_develocity') |
| 175 | + } |
| 176 | + |
| 177 | + registerBuildScanActions(settings.develocity.buildScan, settings.rootProject.name) |
| 178 | + addBuildScanCustomData(settings.develocity.buildScan, settings.develocity.server.get()) |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +static void addCustomValueAndSearchLink(buildScan, String server, label, String value) { |
| 184 | + buildScan.value(label, value) |
| 185 | + String searchParams = "search.names=" + urlEncode(label) + "&search.values=" + urlEncode(value) |
| 186 | + String url = appendIfMissing(server, "/") + "scans?" + searchParams + "#selection.buildScanB=" + urlEncode("{SCAN_ID}") |
| 187 | + buildScan.link(label + " build scans", url) |
| 188 | +} |
| 189 | + |
| 190 | +static String appendIfMissing(String str, String suffix) { |
| 191 | + return str.endsWith(suffix) ? str : str + suffix |
| 192 | +} |
| 193 | + |
| 194 | +static String urlEncode(String str) { |
| 195 | + return URLEncoder.encode(str, StandardCharsets.UTF_8.name()) |
| 196 | +} |
0 commit comments