Update develocity-injection init script to v1.1

Updates the develocity-injection init script to the latest reference script content
from https://github.com/gradle/develocity-ci-injection.
This commit is contained in:
Bot Githubaction 2024-12-11 14:19:31 +00:00 committed by daz
parent 37bb14cb36
commit d0ca1a2b26
No known key found for this signature in database

View file

@ -1,6 +1,6 @@
/* /*
* Initscript for injection of Develocity into Gradle builds. * Initscript for injection of Develocity into Gradle builds.
* Version: v1.0 * Version: v1.1
*/ */
import org.gradle.util.GradleVersion import org.gradle.util.GradleVersion
@ -12,29 +12,29 @@ initscript {
return return
} }
def getInputParam = { String name -> def getInputParam = { Gradle gradle, String name ->
def ENV_VAR_PREFIX = '' def ENV_VAR_PREFIX = ''
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_') def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
return System.getProperty(name) ?: System.getenv(envVarName) return gradle.startParameter.systemPropertiesArgs[name] ?: System.getProperty(name) ?: System.getenv(envVarName)
} }
def requestedInitScriptName = getInputParam('develocity.injection.init-script-name') def requestedInitScriptName = getInputParam(gradle, 'develocity.injection.init-script-name')
def initScriptName = buildscript.sourceFile.name def initScriptName = buildscript.sourceFile.name
if (requestedInitScriptName != initScriptName) { if (requestedInitScriptName != initScriptName) {
return return
} }
// Plugin loading is only required for Develocity injection. Abort early if not enabled. // Plugin loading is only required for Develocity injection. Abort early if not enabled.
def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam("develocity.injection-enabled")) def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam(gradle, "develocity.injection-enabled"))
if (!develocityInjectionEnabled) { if (!develocityInjectionEnabled) {
return return
} }
def pluginRepositoryUrl = getInputParam('gradle.plugin-repository.url') def pluginRepositoryUrl = getInputParam(gradle, 'gradle.plugin-repository.url')
def pluginRepositoryUsername = getInputParam('gradle.plugin-repository.username') def pluginRepositoryUsername = getInputParam(gradle, 'gradle.plugin-repository.username')
def pluginRepositoryPassword = getInputParam('gradle.plugin-repository.password') def pluginRepositoryPassword = getInputParam(gradle, 'gradle.plugin-repository.password')
def develocityPluginVersion = getInputParam('develocity.plugin.version') def develocityPluginVersion = getInputParam(gradle, 'develocity.plugin.version')
def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version') def ccudPluginVersion = getInputParam(gradle, 'develocity.ccud-plugin.version')
def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0') def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0') def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')
@ -79,10 +79,10 @@ initscript {
} }
} }
static getInputParam(String name) { static getInputParam(Gradle gradle, String name) {
def ENV_VAR_PREFIX = '' def ENV_VAR_PREFIX = ''
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_') def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
return System.getProperty(name) ?: System.getenv(envVarName) return gradle.startParameter.systemPropertiesArgs[name] ?: System.getProperty(name) ?: System.getenv(envVarName)
} }
def isTopLevelBuild = !gradle.parent def isTopLevelBuild = !gradle.parent
@ -90,14 +90,14 @@ if (!isTopLevelBuild) {
return return
} }
def requestedInitScriptName = getInputParam('develocity.injection.init-script-name') def requestedInitScriptName = getInputParam(gradle, 'develocity.injection.init-script-name')
def initScriptName = buildscript.sourceFile.name def initScriptName = buildscript.sourceFile.name
if (requestedInitScriptName != initScriptName) { if (requestedInitScriptName != initScriptName) {
logger.quiet("Ignoring init script '${initScriptName}' as requested name '${requestedInitScriptName}' does not match") logger.quiet("Ignoring init script '${initScriptName}' as requested name '${requestedInitScriptName}' does not match")
return return
} }
def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam("develocity.injection-enabled")) def develocityInjectionEnabled = Boolean.parseBoolean(getInputParam(gradle, "develocity.injection-enabled"))
if (develocityInjectionEnabled) { if (develocityInjectionEnabled) {
enableDevelocityInjection() enableDevelocityInjection()
} }
@ -123,16 +123,16 @@ void enableDevelocityInjection() {
def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin' def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin'
def CCUD_PLUGIN_CLASS = 'com.gradle.CommonCustomUserDataGradlePlugin' def CCUD_PLUGIN_CLASS = 'com.gradle.CommonCustomUserDataGradlePlugin'
def develocityUrl = getInputParam('develocity.url') def develocityUrl = getInputParam(gradle, 'develocity.url')
def develocityAllowUntrustedServer = Boolean.parseBoolean(getInputParam('develocity.allow-untrusted-server')) def develocityAllowUntrustedServer = Boolean.parseBoolean(getInputParam(gradle, 'develocity.allow-untrusted-server'))
def develocityEnforceUrl = Boolean.parseBoolean(getInputParam('develocity.enforce-url')) def develocityEnforceUrl = Boolean.parseBoolean(getInputParam(gradle, 'develocity.enforce-url'))
def buildScanUploadInBackground = Boolean.parseBoolean(getInputParam('develocity.build-scan.upload-in-background')) def buildScanUploadInBackground = Boolean.parseBoolean(getInputParam(gradle, 'develocity.build-scan.upload-in-background'))
def develocityCaptureFileFingerprints = getInputParam('develocity.capture-file-fingerprints') ? Boolean.parseBoolean(getInputParam('develocity.capture-file-fingerprints')) : true def develocityCaptureFileFingerprints = getInputParam(gradle, 'develocity.capture-file-fingerprints') ? Boolean.parseBoolean(getInputParam(gradle, 'develocity.capture-file-fingerprints')) : true
def develocityPluginVersion = getInputParam('develocity.plugin.version') def develocityPluginVersion = getInputParam(gradle, 'develocity.plugin.version')
def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version') def ccudPluginVersion = getInputParam(gradle, 'develocity.ccud-plugin.version')
def buildScanTermsOfUseUrl = getInputParam('develocity.terms-of-use.url') def buildScanTermsOfUseUrl = getInputParam(gradle, 'develocity.terms-of-use.url')
def buildScanTermsOfUseAgree = getInputParam('develocity.terms-of-use.agree') def buildScanTermsOfUseAgree = getInputParam(gradle, 'develocity.terms-of-use.agree')
def ciAutoInjectionCustomValueValue = getInputParam('develocity.auto-injection.custom-value') def ciAutoInjectionCustomValueValue = getInputParam(gradle, 'develocity.auto-injection.custom-value')
def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0') def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0') def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')
@ -145,6 +145,14 @@ void enableDevelocityInjection() {
return geValue instanceof Closure<?> ? geValue() : geValue return geValue instanceof Closure<?> ? geValue() : geValue
} }
def printEnforcingDevelocityUrl = {
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
}
def printAcceptingGradleTermsOfUse = {
logger.lifecycle("Accepting Gradle Terms of Use: $buildScanTermsOfUseUrl")
}
// finish early if configuration parameters passed in via system properties are not valid/supported // finish early if configuration parameters passed in via system properties are not valid/supported
if (ccudPluginVersion && isNotAtLeast(ccudPluginVersion, '1.7')) { if (ccudPluginVersion && isNotAtLeast(ccudPluginVersion, '1.7')) {
logger.warn("Common Custom User Data Gradle plugin must be at least 1.7. Configured version is $ccudPluginVersion.") logger.warn("Common Custom User Data Gradle plugin must be at least 1.7. Configured version is $ccudPluginVersion.")
@ -163,8 +171,8 @@ void enableDevelocityInjection() {
} }
if (!scanPluginComponent) { if (!scanPluginComponent) {
def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, BUILD_SCAN_PLUGIN_CLASS) def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, BUILD_SCAN_PLUGIN_CLASS)
logger.lifecycle("Applying $pluginClass via init script") def pluginVersion = atLeastGradle5 ? develocityPluginVersion : "1.16"
applyPluginExternally(pluginManager, pluginClass) applyPluginExternally(pluginManager, pluginClass, pluginVersion)
def rootExtension = dvOrGe( def rootExtension = dvOrGe(
{ develocity }, { develocity },
{ buildScan } { buildScan }
@ -196,48 +204,52 @@ void enableDevelocityInjection() {
} }
} }
} }
}
if (develocityUrl && develocityEnforceUrl) { eachDevelocityProjectExtension(project,
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints") { develocity ->
}
pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
// Only execute if develocity plugin isn't applied.
if (gradle.rootProject.extensions.findByName("develocity")) return
afterEvaluate {
if (develocityUrl && develocityEnforceUrl) {
buildScan.server = develocityUrl
buildScan.allowUntrustedServer = develocityAllowUntrustedServer
}
}
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
}
}
pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
afterEvaluate { afterEvaluate {
if (develocityUrl && develocityEnforceUrl) { if (develocityUrl && develocityEnforceUrl) {
printEnforcingDevelocityUrl()
develocity.server = develocityUrl develocity.server = develocityUrl
develocity.allowUntrustedServer = develocityAllowUntrustedServer develocity.allowUntrustedServer = develocityAllowUntrustedServer
} }
} }
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) { if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
printAcceptingGradleTermsOfUse()
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
} }
},
{ buildScan ->
afterEvaluate {
if (develocityUrl && develocityEnforceUrl) {
printEnforcingDevelocityUrl()
buildScan.server = develocityUrl
buildScan.allowUntrustedServer = develocityAllowUntrustedServer
}
}
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
printAcceptingGradleTermsOfUse()
if (buildScan.metaClass.respondsTo(buildScan, 'setTermsOfServiceUrl', String)) {
buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
} else {
buildScan.licenseAgreementUrl = buildScanTermsOfUseUrl
buildScan.licenseAgree = buildScanTermsOfUseAgree
}
}
} }
} )
if (ccudPluginVersion && atLeastGradle4) { if (ccudPluginVersion && atLeastGradle4) {
def ccudPluginComponent = resolutionResult.allComponents.find { def ccudPluginComponent = resolutionResult.allComponents.find {
it.moduleVersion.with { group == "com.gradle" && name == "common-custom-user-data-gradle-plugin" } it.moduleVersion.with { group == "com.gradle" && name == "common-custom-user-data-gradle-plugin" }
} }
if (!ccudPluginComponent) { if (!ccudPluginComponent) {
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS via init script") logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS)) pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
} }
} }
@ -248,13 +260,18 @@ void enableDevelocityInjection() {
if (develocityPluginVersion) { if (develocityPluginVersion) {
if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID) && !settings.pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) { if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID) && !settings.pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) {
def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, GRADLE_ENTERPRISE_PLUGIN_CLASS) def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, GRADLE_ENTERPRISE_PLUGIN_CLASS)
logger.lifecycle("Applying $pluginClass via init script") applyPluginExternally(settings.pluginManager, pluginClass, develocityPluginVersion)
applyPluginExternally(settings.pluginManager, pluginClass)
if (develocityUrl) { if (develocityUrl) {
logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints") logger.lifecycle("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints")
eachDevelocitySettingsExtension(settings) { ext -> eachDevelocitySettingsExtension(settings) { ext ->
ext.server = develocityUrl // server and allowUntrustedServer must be configured via buildScan extension for gradle-enterprise-plugin 3.1.1 and earlier
ext.allowUntrustedServer = develocityAllowUntrustedServer if (ext.metaClass.respondsTo(ext, 'getServer')) {
ext.server = develocityUrl
ext.allowUntrustedServer = develocityAllowUntrustedServer
} else {
ext.buildScan.server = develocityUrl
ext.buildScan.allowUntrustedServer = develocityAllowUntrustedServer
}
} }
} }
@ -281,40 +298,46 @@ void enableDevelocityInjection() {
} }
) )
} }
}
if (develocityUrl && develocityEnforceUrl) { eachDevelocitySettingsExtension(settings,
logger.lifecycle("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer, captureFileFingerprints: $develocityCaptureFileFingerprints") { develocity ->
} if (develocityUrl && develocityEnforceUrl) {
printEnforcingDevelocityUrl()
develocity.server = develocityUrl
develocity.allowUntrustedServer = develocityAllowUntrustedServer
}
eachDevelocitySettingsExtension(settings, if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
{ develocity -> printAcceptingGradleTermsOfUse()
if (develocityUrl && develocityEnforceUrl) { develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl
develocity.server = develocityUrl develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree
develocity.allowUntrustedServer = develocityAllowUntrustedServer }
} },
{ gradleEnterprise ->
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) { if (develocityUrl && develocityEnforceUrl) {
develocity.buildScan.termsOfUseUrl = buildScanTermsOfUseUrl printEnforcingDevelocityUrl()
develocity.buildScan.termsOfUseAgree = buildScanTermsOfUseAgree // server and allowUntrustedServer must be configured via buildScan extension for gradle-enterprise-plugin 3.1.1 and earlier
} if (gradleEnterprise.metaClass.respondsTo(gradleEnterprise, 'getServer')) {
},
{ gradleEnterprise ->
if (develocityUrl && develocityEnforceUrl) {
gradleEnterprise.server = develocityUrl gradleEnterprise.server = develocityUrl
gradleEnterprise.allowUntrustedServer = develocityAllowUntrustedServer gradleEnterprise.allowUntrustedServer = develocityAllowUntrustedServer
} } else {
gradleEnterprise.buildScan.server = develocityUrl
if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) { gradleEnterprise.buildScan.allowUntrustedServer = develocityAllowUntrustedServer
gradleEnterprise.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
gradleEnterprise.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
} }
} }
)
} if (buildScanTermsOfUseUrl && buildScanTermsOfUseAgree) {
printAcceptingGradleTermsOfUse()
gradleEnterprise.buildScan.termsOfServiceUrl = buildScanTermsOfUseUrl
gradleEnterprise.buildScan.termsOfServiceAgree = buildScanTermsOfUseAgree
}
}
)
if (ccudPluginVersion) { if (ccudPluginVersion) {
if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) { if (!settings.pluginManager.hasPlugin(CCUD_PLUGIN_ID)) {
logger.lifecycle("Applying $CCUD_PLUGIN_CLASS via init script") logger.lifecycle("Applying $CCUD_PLUGIN_CLASS with version $ccudPluginVersion via init script")
settings.pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS)) settings.pluginManager.apply(initscript.classLoader.loadClass(CCUD_PLUGIN_CLASS))
} }
} }
@ -322,7 +345,9 @@ void enableDevelocityInjection() {
} }
} }
void applyPluginExternally(def pluginManager, String pluginClassName) { void applyPluginExternally(def pluginManager, String pluginClassName, String pluginVersion) {
logger.lifecycle("Applying $pluginClassName with version $pluginVersion via init script")
def externallyApplied = 'develocity.externally-applied' def externallyApplied = 'develocity.externally-applied'
def externallyAppliedDeprecated = 'gradle.enterprise.externally-applied' def externallyAppliedDeprecated = 'gradle.enterprise.externally-applied'
def oldValue = System.getProperty(externallyApplied) def oldValue = System.getProperty(externallyApplied)
@ -367,6 +392,32 @@ static def eachDevelocitySettingsExtension(def settings, def dvAction, def geAct
} }
} }
/**
* Apply the `dvAction` to the 'develocity' extension.
* If no 'develocity' extension is found, apply the `bsAction` to the 'buildScan' extension.
* (The develocity plugin creates both extensions, and we want to prefer configuring 'develocity').
*/
static def eachDevelocityProjectExtension(def project, def dvAction, def bsAction = dvAction) {
def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
def configureDvOrBsExtension = {
if (project.extensions.findByName("develocity")) {
dvAction(project.develocity)
} else {
bsAction(project.buildScan)
}
}
project.pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID, configureDvOrBsExtension)
project.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
// Proper extension will be configured by the build-scan callback.
if (project.pluginManager.hasPlugin(BUILD_SCAN_PLUGIN_ID)) return
configureDvOrBsExtension()
}
}
static boolean isAtLeast(String versionUnderTest, String referenceVersion) { static boolean isAtLeast(String versionUnderTest, String referenceVersion) {
GradleVersion.version(versionUnderTest) >= GradleVersion.version(referenceVersion) GradleVersion.version(versionUnderTest) >= GradleVersion.version(referenceVersion)
} }
@ -376,21 +427,13 @@ static boolean isNotAtLeast(String versionUnderTest, String referenceVersion) {
} }
void enableBuildScanLinkCapture(BuildScanCollector collector) { void enableBuildScanLinkCapture(BuildScanCollector collector) {
def BUILD_SCAN_PLUGIN_ID = 'com.gradle.build-scan'
def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
// Conditionally apply and configure the Develocity plugin // Conditionally apply and configure the Develocity plugin
if (GradleVersion.current() < GradleVersion.version('6.0')) { if (GradleVersion.current() < GradleVersion.version('6.0')) {
rootProject { rootProject {
pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) { eachDevelocityProjectExtension(project,
// Only execute if develocity plugin isn't applied. { develocity -> buildScanPublishedAction(develocity.buildScan, collector) },
if (gradle.rootProject.extensions.findByName("develocity")) return { buildScan -> buildScanPublishedAction(buildScan, collector) }
buildScanPublishedAction(buildScan, collector) )
}
pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
buildScanPublishedAction(develocity.buildScan, collector)
}
} }
} else { } else {
gradle.settingsEvaluated { settings -> gradle.settingsEvaluated { settings ->