Unverified Commit 579fbbe7 authored by Iurii Ignatko's avatar Iurii Ignatko Committed by GitHub
Browse files

Allow configuring credentials for custom Gradle plugin repository (#61)

parent b00d9dd5
Loading
Loading
Loading
Loading
+3 −1
Changes for setup-gradle/README.md: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -664,7 +664,7 @@ with a valid [Develocity access key](https://docs.gradle.com/enterprise/gradle-p
The `init-script` supports several additional configuration parameters that you may find useful. All configuration options (required and optional) are detailed below:

| Variable                          | Required | Description                                                                                                                                                              |
|-----------------------------------| --- | --- |
|-----------------------------------| --- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DEVELOCITY_INJECTION_ENABLED      | :white_check_mark: | enables Develocity injection                                                                                                                                             |
| DEVELOCITY_URL                    | :white_check_mark: | the URL of the Develocity server                                                                                                                                         |
| DEVELOCITY_ALLOW_UNTRUSTED_SERVER | | allow communication with an untrusted server; set to _true_ if your Develocity instance is using a self-signed certificate                                               |
@@ -672,6 +672,8 @@ The `init-script` supports several additional configuration parameters that you
| DEVELOCITY_PLUGIN_VERSION         | :white_check_mark: | the version of the [Develocity Gradle plugin](https://docs.gradle.com/enterprise/gradle-plugin/) to apply                                                                |
| DEVELOCITY_CCUD_PLUGIN_VERSION    |  | the version of the [Common Custom User Data Gradle plugin](https://github.com/gradle/common-custom-user-data-gradle-plugin) to apply, if any                             |
| GRADLE_PLUGIN_REPOSITORY_URL      |  | the URL of the repository to use when resolving the Develocity and CCUD plugins; the Gradle Plugin Portal is used by default                                             |
| GRADLE_PLUGIN_REPOSITORY_USERNAME |  | the username for the repository URL to use when resolving the Develocity and CCUD plugins                                                                                |
| GRADLE_PLUGIN_REPOSITORY_PASSWORD |  | the password for the repository URL to use when resolving the Develocity and CCUD plugins; Consider using secrets to pass the value to this variable                     |

## Publishing to scans.gradle.com

+15 −1
Changes for sources/src/resources/init-scripts/gradle-actions.inject-develocity.init.gradle: 15 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ initscript {
    }

    def pluginRepositoryUrl = getInputParam('gradle.plugin-repository.url')
    def pluginRepositoryUsername = getInputParam('gradle.plugin-repository.username')
    def pluginRepositoryPassword = getInputParam('gradle.plugin-repository.password')
    def gePluginVersion = getInputParam('develocity.plugin.version')
    def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version')

@@ -32,7 +34,19 @@ initscript {
        logger.lifecycle("Develocity plugins resolution: $pluginRepositoryUrl")

        repositories {
            maven { url pluginRepositoryUrl }
            maven {
                url pluginRepositoryUrl
                if (pluginRepositoryUsername && pluginRepositoryPassword) {
                    logger.lifecycle("Using credentials for plugin repository")
                    credentials {
                        username(pluginRepositoryUsername)
                        password(pluginRepositoryPassword)
                    }
                    authentication {
                        basic(BasicAuthentication)
                    }
                }
            }
        }
    }

+39 −1
Changes for sources/test/init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestDevelocityInjection.groovy: 39 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -205,6 +205,26 @@ class TestDevelocityInjection extends BaseInitScriptTest {
        testGradleVersion << ALL_VERSIONS
    }

    def "can configure alternative repository for plugins with credentials when Develocity plugin is applied by the init script"() {
        assumeTrue testGradleVersion.compatibleWithCurrentJvm

        when:
        def config = testConfig().withPluginRepository(new URI('https://plugins.grdev.net/m2')).withPluginRepositoryCredentials("john", "doe")
        def result = run(testGradleVersion, config)

        then:
        outputContainsDevelocityPluginApplicationViaInitScript(result, testGradleVersion.gradleVersion)
        outputContainsDevelocityConnectionInfo(result, mockScansServer.address.toString(), true)
        outputMissesCcudPluginApplicationViaInitScript(result)
        outputContainsPluginRepositoryInfo(result, 'https://plugins.grdev.net/m2', true)

        and:
        outputContainsBuildScanUrl(result)

        where:
        testGradleVersion << ALL_VERSIONS
    }

    def "stops gracefully when requested CCUD plugin version is <1.7"() {
        assumeTrue testGradleVersion.compatibleWithCurrentJvm

@@ -311,10 +331,16 @@ class TestDevelocityInjection extends BaseInitScriptTest {
        assert 1 == result.output.count(geConnectionInfo)
    }

    void outputContainsPluginRepositoryInfo(BuildResult result, String gradlePluginRepositoryUrl) {
    void outputContainsPluginRepositoryInfo(BuildResult result, String gradlePluginRepositoryUrl, boolean withCredentials = false) {
        def repositoryInfo = "Develocity plugins resolution: ${gradlePluginRepositoryUrl}"
        assert result.output.contains(repositoryInfo)
        assert 1 == result.output.count(repositoryInfo)

        if (withCredentials) {
            def credentialsInfo = "Using credentials for plugin repository"
            assert result.output.contains(credentialsInfo)
            assert 1 == result.output.count(credentialsInfo)
        }
    }

    void outputEnforcesDevelocityUrl(BuildResult result, String geUrl, boolean geAllowUntrustedServer) {
@@ -350,6 +376,8 @@ class TestDevelocityInjection extends BaseInitScriptTest {
        boolean enforceUrl = false
        String ccudPluginVersion = null
        String pluginRepositoryUrl = null
        String pluginRepoUsername = null
        String pluginRepoPassword = null

        TestConfig withCCUDPlugin(String version = CCUD_PLUGIN_VERSION) {
            ccudPluginVersion = version
@@ -367,6 +395,12 @@ class TestDevelocityInjection extends BaseInitScriptTest {
            return this
        }

        TestConfig withPluginRepositoryCredentials(String pluginRepoUsername, String pluginRepoPassword) {
            this.pluginRepoUsername = pluginRepoUsername
            this.pluginRepoPassword = pluginRepoPassword
            return this
        }

        def getEnvVars() {
            Map<String, String> envVars = [
                DEVELOCITY_INJECTION_ENABLED: "true",
@@ -378,6 +412,8 @@ class TestDevelocityInjection extends BaseInitScriptTest {
            if (enforceUrl) envVars.put("DEVELOCITY_ENFORCE_URL", "true")
            if (ccudPluginVersion != null) envVars.put("DEVELOCITY_CCUD_PLUGIN_VERSION", ccudPluginVersion)
            if (pluginRepositoryUrl != null) envVars.put("GRADLE_PLUGIN_REPOSITORY_URL", pluginRepositoryUrl)
            if (pluginRepoUsername != null) envVars.put("GRADLE_PLUGIN_REPOSITORY_USERNAME", pluginRepoUsername)
            if (pluginRepoPassword != null) envVars.put("GRADLE_PLUGIN_REPOSITORY_PASSWORD", pluginRepoPassword)

            return envVars
        }
@@ -394,6 +430,8 @@ class TestDevelocityInjection extends BaseInitScriptTest {
            if (enforceUrl) jvmArgs.add("-Ddevelocity.enforce-url=true")
            if (ccudPluginVersion != null) jvmArgs.add("-Ddevelocity.ccud-plugin.version=$ccudPluginVersion")
            if (pluginRepositoryUrl != null) jvmArgs.add("-Dgradle.plugin-repository.url=$pluginRepositoryUrl")
            if (pluginRepoUsername != null) jvmArgs.add("-Dgradle.plugin-repository.username=$pluginRepoUsername")
            if (pluginRepoPassword != null) jvmArgs.add("-Dgradle.plugin-repository.password=$pluginRepoPassword")

            return jvmArgs.collect { it.toString() } // Convert from GStrings
        }