Inject Develocity plugin for Gradle 5+ and plugin version 3.17+

This commit is contained in:
Pavlo Shevchenko 2024-02-07 16:32:03 +01:00
parent 4e8c9b3d32
commit 90add9b5de
No known key found for this signature in database
GPG Key ID: A52E2C6DAD8A5DDC

View File

@ -24,6 +24,7 @@ initscript {
def develocityPluginVersion = getInputParam('develocity.plugin.version') def develocityPluginVersion = getInputParam('develocity.plugin.version')
def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version') def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version')
def shouldApplyDevelocityPlugin = GradleVersion.version(develocityPluginVersion) >= GradleVersion.version("3.17")
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')
@ -39,8 +40,8 @@ initscript {
dependencies { dependencies {
if (develocityPluginVersion) { if (develocityPluginVersion) {
classpath atLeastGradle5 ? classpath atLeastGradle5 ?
"com.gradle:gradle-enterprise-gradle-plugin:$develocityPluginVersion" : shouldApplyDevelocityPlugin ? "com.gradle:develocity-gradle-plugin:$develocityPluginVersion" : "com.gradle:gradle-enterprise-gradle-plugin:$develocityPluginVersion" :
"com.gradle:build-scan-plugin:1.16" "com.gradle:build-scan-plugin:1.16"
} }
if (ccudPluginVersion && atLeastGradle4) { if (ccudPluginVersion && atLeastGradle4) {
@ -54,7 +55,14 @@ def BUILD_SCAN_PLUGIN_CLASS = 'com.gradle.scan.plugin.BuildScanPlugin'
def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise' def GRADLE_ENTERPRISE_PLUGIN_ID = 'com.gradle.enterprise'
def GRADLE_ENTERPRISE_PLUGIN_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin' def GRADLE_ENTERPRISE_PLUGIN_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin'
def GRADLE_ENTERPRISE_EXTENSION_CLASS = 'com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension'
def DEVELOCITY_PLUGIN_ID = 'com.gradle.develocity'
def DEVELOCITY_PLUGIN_CLASS = 'com.gradle.develocity.agent.gradle.DevelocityPlugin'
def SETTINGS_EXTENSION_CLASSES = [
'com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension',
'com.gradle.develocity.agent.gradle.DevelocityConfiguration'
]
def CI_AUTO_INJECTION_CUSTOM_VALUE_NAME = 'CI auto injection' def CI_AUTO_INJECTION_CUSTOM_VALUE_NAME = 'CI auto injection'
def CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE = 'gradle-actions' def CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE = 'gradle-actions'
def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin' def CCUD_PLUGIN_ID = 'com.gradle.common-custom-user-data-gradle-plugin'
@ -85,7 +93,16 @@ def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version')
def buildScanTermsOfServiceUrl = getInputParam('build-scan.terms-of-service.url') def buildScanTermsOfServiceUrl = getInputParam('build-scan.terms-of-service.url')
def buildScanTermsOfServiceAgree = getInputParam('build-scan.terms-of-service.agree') def buildScanTermsOfServiceAgree = getInputParam('build-scan.terms-of-service.agree')
def atLeastGradle5 = GradleVersion.current() >= GradleVersion.version('5.0')
def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0') def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0')
def shouldApplyDevelocityPlugin = atLeastGradle5 && isAtLeast(develocityPluginVersion, '3.17')
def dvOrGe = { def dvValue, def geValue ->
if (shouldApplyDevelocityPlugin) {
return dvValue instanceof Closure<?> ? dvValue() : dvValue
}
return geValue instanceof Closure<?> ? geValue() : geValue
}
// 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')) {
@ -101,34 +118,57 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
if (develocityPluginVersion) { if (develocityPluginVersion) {
def scanPluginComponent = resolutionResult.allComponents.find { def scanPluginComponent = resolutionResult.allComponents.find {
it.moduleVersion.with { group == "com.gradle" && (name == "build-scan-plugin" || name == "gradle-enterprise-gradle-plugin") } it.moduleVersion.with { group == "com.gradle" && ['build-scan-plugin', 'gradle-enterprise-gradle-plugin', 'develocity-gradle-plugin'].contains(name) }
} }
if (!scanPluginComponent) { if (!scanPluginComponent) {
logger.quiet("Applying $BUILD_SCAN_PLUGIN_CLASS via init script") def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, BUILD_SCAN_PLUGIN_CLASS)
applyPluginExternally(pluginManager, BUILD_SCAN_PLUGIN_CLASS) logger.quiet("Applying $pluginClass via init script")
applyPluginExternally(pluginManager, pluginClass)
def rootExtension = dvOrGe(
{ develocity },
{ buildScan }
)
def buildScanExtension = dvOrGe(
{ rootExtension.buildScan },
{ rootExtension }
)
if (develocityUrl) { if (develocityUrl) {
logger.quiet("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer") logger.quiet("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
buildScan.server = develocityUrl rootExtension.server = develocityUrl
buildScan.allowUntrustedServer = develocityAllowUntrustedServer rootExtension.allowUntrustedServer = develocityAllowUntrustedServer
} }
buildScan.publishAlways() if (!shouldApplyDevelocityPlugin) {
if (buildScan.metaClass.respondsTo(buildScan, 'setUploadInBackground', Boolean)) buildScan.uploadInBackground = buildScanUploadInBackground // uploadInBackground not available for build-scan-plugin 1.16 // Develocity plugin publishes scans by default
buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE buildScanExtension.publishAlways()
}
// uploadInBackground not available for build-scan-plugin 1.16
if (buildScanExtension.metaClass.respondsTo(buildScanExtension, 'setUploadInBackground', Boolean)) buildScanExtension.uploadInBackground = buildScanUploadInBackground
buildScanExtension.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE
} }
if (develocityUrl && develocityEnforceUrl) { if (develocityUrl && develocityEnforceUrl) {
pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) { pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
afterEvaluate { afterEvaluate {
logger.quiet("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer") logger.quiet("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer for Build Scan plugin")
buildScan.server = develocityUrl buildScan.server = develocityUrl
buildScan.allowUntrustedServer = develocityAllowUntrustedServer buildScan.allowUntrustedServer = develocityAllowUntrustedServer
} }
} }
pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
afterEvaluate {
logger.quiet("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer for Develocity plugin")
develocity.server = develocityUrl
develocity.allowUntrustedServer = develocityAllowUntrustedServer
}
}
} }
if (buildScanTermsOfServiceUrl && buildScanTermsOfServiceAgree) { if (buildScanTermsOfServiceUrl && buildScanTermsOfServiceAgree) {
buildScan.termsOfServiceUrl = buildScanTermsOfServiceUrl def buildScanExtension = atLeastGradle5
buildScan.termsOfServiceAgree = buildScanTermsOfServiceAgree ? dvOrGe(develocity.buildScan, buildScan)
: buildScan
buildScanExtension.termsOfServiceUrl = buildScanTermsOfServiceUrl
buildScanExtension.termsOfServiceAgree = buildScanTermsOfServiceAgree
} }
} }
@ -146,23 +186,27 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
} else { } else {
gradle.settingsEvaluated { settings -> gradle.settingsEvaluated { settings ->
if (develocityPluginVersion) { if (develocityPluginVersion) {
if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID)) { if (!settings.pluginManager.hasPlugin(GRADLE_ENTERPRISE_PLUGIN_ID) && !settings.pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) {
logger.quiet("Applying $GRADLE_ENTERPRISE_PLUGIN_CLASS via init script") def pluginClass = dvOrGe(DEVELOCITY_PLUGIN_CLASS, GRADLE_ENTERPRISE_PLUGIN_CLASS)
applyPluginExternally(settings.pluginManager, GRADLE_ENTERPRISE_PLUGIN_CLASS) logger.quiet("Applying $pluginClass via init script")
eachDevelocityExtension(settings, GRADLE_ENTERPRISE_EXTENSION_CLASS) { ext -> applyPluginExternally(settings.pluginManager, pluginClass)
eachDevelocitySettingsExtension(settings, SETTINGS_EXTENSION_CLASSES) { ext ->
if (develocityUrl) { if (develocityUrl) {
logger.quiet("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer") logger.quiet("Connection to Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
ext.server = develocityUrl ext.server = develocityUrl
ext.allowUntrustedServer = develocityAllowUntrustedServer ext.allowUntrustedServer = develocityAllowUntrustedServer
} }
ext.buildScan.publishAlways() if (!shouldApplyDevelocityPlugin) {
// Develocity plugin publishes build scans by default
ext.buildScan.publishAlways()
}
ext.buildScan.uploadInBackground = buildScanUploadInBackground ext.buildScan.uploadInBackground = buildScanUploadInBackground
ext.buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE ext.buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE
} }
} }
if (develocityUrl && develocityEnforceUrl) { if (develocityUrl && develocityEnforceUrl) {
eachDevelocityExtension(settings, GRADLE_ENTERPRISE_EXTENSION_CLASS) { ext -> eachDevelocitySettingsExtension(settings, SETTINGS_EXTENSION_CLASSES) { ext ->
logger.quiet("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer") logger.quiet("Enforcing Develocity: $develocityUrl, allowUntrustedServer: $develocityAllowUntrustedServer")
ext.server = develocityUrl ext.server = develocityUrl
ext.allowUntrustedServer = develocityAllowUntrustedServer ext.allowUntrustedServer = develocityAllowUntrustedServer
@ -170,7 +214,7 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) {
} }
if (buildScanTermsOfServiceUrl && buildScanTermsOfServiceAgree) { if (buildScanTermsOfServiceUrl && buildScanTermsOfServiceAgree) {
eachDevelocityExtension(settings, GRADLE_ENTERPRISE_EXTENSION_CLASS) { ext -> eachDevelocitySettingsExtension(settings, SETTINGS_EXTENSION_CLASSES) { ext ->
ext.buildScan.termsOfServiceUrl = buildScanTermsOfServiceUrl ext.buildScan.termsOfServiceUrl = buildScanTermsOfServiceUrl
ext.buildScan.termsOfServiceAgree = buildScanTermsOfServiceAgree ext.buildScan.termsOfServiceAgree = buildScanTermsOfServiceAgree
} }
@ -201,11 +245,16 @@ void applyPluginExternally(def pluginManager, String pluginClassName) {
} }
} }
static def eachDevelocityExtension(def settings, def publicType, def action) { static def eachDevelocitySettingsExtension(def settings, List<String> publicTypes, def action) {
settings.extensions.extensionsSchema.elements.findAll { it.publicType.concreteClass.name == publicType } settings.extensions.extensionsSchema.elements.findAll { publicTypes.contains(it.publicType.concreteClass.name) }
.collect { settings[it.name] }.each(action) .collect { settings[it.name] }
.each(action)
}
static boolean isAtLeast(String versionUnderTest, String referenceVersion) {
GradleVersion.version(versionUnderTest) >= GradleVersion.version(referenceVersion)
} }
static boolean isNotAtLeast(String versionUnderTest, String referenceVersion) { static boolean isNotAtLeast(String versionUnderTest, String referenceVersion) {
GradleVersion.version(versionUnderTest) < GradleVersion.version(referenceVersion) !isAtLeast(versionUnderTest, referenceVersion)
} }