Unverified Commit c36ba1fb authored by Eric Haag's avatar Eric Haag
Browse files

Configure the build cache through Develocity injection

Add setup-gradle inputs that configure the remote and local build cache
through Develocity injection. Each input is passed to the init script as
a DEVELOCITY_INJECTION_* environment variable. The remote build cache
uses the Develocity Build Cache connector.

The develocity-enforce-url description also explains how the enforced
URL applies to a Build Cache connector.
parent c0152e9a
Loading
Loading
Loading
Loading
+70 −3
Changes for docs/setup-gradle.md: 70 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -431,6 +431,8 @@ As well as reducing the content that needs to be saved to the GitHub Actions cac
this setup will ensure that your CI builds populate the remote cache and keep the cache entries fresh by reading these entries.
Local builds can then benefit from the remote cache.

If your build uses Develocity, `setup-gradle` can apply this setup for you with [Develocity injection](#configuring-the-build-cache-with-develocity-injection).

## Basic Caching

Basic Caching is an open-source (MIT) alternative that uses `@actions/cache` to provide straightforward, path-based caching of Gradle dependencies. It uses the same caching strategy as `actions/setup-java` with `cache: gradle`.
@@ -979,15 +981,15 @@ This access key will be used during the action execution to get a short-lived to

### Configuring Develocity injection

The `init-script` supports several additional configuration parameters that you may find useful. All configuration options (required and optional) are detailed below:
The `init-script` supports several additional configuration parameters that you may find useful. These 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                                                                                                                                                                                                               |
| develocity-capture-file-fingerprints | | enables capturing the paths and content hashes of each individual input file                                                                                                                                                                                                                                                             |
| develocity-enforce-url               | | enforce the configured Develocity URL over a URL configured in the project's build; set to _true_ to enforce publication of build scans to the configured Develocity URL |
| develocity-enforce-url               | | enforce the configured Develocity URL over a URL configured in the project's build; set to _true_ to enforce publication of build scans to the configured Develocity URL; a Develocity Build Cache connector without a server defined follows the enforced URL, but a server explicitly configured in the project's build is not changed |
| develocity-plugin-version            | :white_check_mark: | the version of the [Develocity Gradle plugin](https://docs.gradle.com/develocity/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                                                                                                                                                                                                             |
@@ -1026,6 +1028,71 @@ Here's an example using the env vars:
        DEVELOCITY_INJECTION_CCUD_PLUGIN_VERSION: '2.2.1'
```

### Configuring the build cache with Develocity injection

> [!IMPORTANT]
> Develocity injection can configure the local and remote build caches, but it cannot enable
> the [Gradle build cache](https://docs.gradle.org/current/userguide/build_cache.html) feature
> itself. Enable it with `org.gradle.caching=true` in `gradle.properties` or with the
> `--build-cache` command line option. Without it, the `setup-gradle` build cache configuration
> has no effect.

Develocity injection can also configure the build cache of your build.
The remote build cache is configured with the [Develocity Build Cache connector](https://docs.gradle.com/develocity/gradle-plugin/current/#using_build_caching), which uses the Develocity server unless another server is configured.
The connector authenticates with the same access key the Develocity Gradle plugin uses to publish Build Scans.

| Input                                                 | Description                                                                                                                 |
|-------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| develocity-enforce-build-cache                        | set to _true_ to replace a remote build cache configured in the project's build with the Develocity Build Cache connector   |
| develocity-remote-build-cache-enabled                 | enable the remote build cache                                                                                               |
| develocity-remote-build-cache-push                    | store task outputs in the remote build cache                                                                                |
| develocity-remote-build-cache-server                  | the URL of the remote build cache server; the Develocity server is used by default                                          |
| develocity-remote-build-cache-path                    | the path of the remote build cache on the server                                                                            |
| develocity-remote-build-cache-allow-insecure-protocol | allow communication with the remote build cache over plain HTTP                                                             |
| develocity-remote-build-cache-allow-untrusted-server  | allow communication with an untrusted remote build cache server                                                             |
| develocity-local-build-cache-enabled                  | enable the local build cache                                                                                                |
| develocity-local-build-cache-push                     | store task outputs in the local build cache                                                                                 |

The input parameters can be expressed as environment variables following the relationships outlined in the table below:

| Input                                                 | Environment Variable                                            |
|-------------------------------------------------------|-----------------------------------------------------------------|
| develocity-enforce-build-cache                        | DEVELOCITY_INJECTION_ENFORCE_BUILD_CACHE                        |
| develocity-remote-build-cache-enabled                 | DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_ENABLED                 |
| develocity-remote-build-cache-push                    | DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_PUSH                    |
| develocity-remote-build-cache-server                  | DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_SERVER                  |
| develocity-remote-build-cache-path                    | DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_PATH                    |
| develocity-remote-build-cache-allow-insecure-protocol | DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_ALLOW_INSECURE_PROTOCOL |
| develocity-remote-build-cache-allow-untrusted-server  | DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_ALLOW_UNTRUSTED_SERVER  |
| develocity-local-build-cache-enabled                  | DEVELOCITY_INJECTION_LOCAL_BUILD_CACHE_ENABLED                  |
| develocity-local-build-cache-push                     | DEVELOCITY_INJECTION_LOCAL_BUILD_CACHE_PUSH                     |

The remote build cache is only configured when `develocity-remote-build-cache-enabled` is set, and the local build cache only when `develocity-local-build-cache-enabled` is set.
If the project's build already configures a remote build cache, the existing build cache configuration is not modified unless `develocity-enforce-build-cache` is set to _true_.
If the remote build cache cannot be configured, the local build cache is not configured either, so a build is never left with a partial build cache configuration.

Build cache configuration requires Gradle 6.0 or later and the Develocity Gradle plugin 3.11 or later.
With the configuration cache enabled, it requires Gradle 7.5.1 or later.
Builds that do not meet these requirements keep their own build cache configuration.

Here's an example that has CI builds populate the remote build cache on the Develocity server and disables the local build cache:

```yaml
    - name: Setup Gradle
      uses: gradle/actions/setup-gradle@v6
      with:
        develocity-access-key: ${{ secrets.MY_DEVELOCITY_ACCESS_KEY }}
        develocity-injection-enabled: true
        develocity-url: 'https://develocity.your-server.com'
        develocity-plugin-version: '4.0'
        develocity-remote-build-cache-enabled: true
        develocity-remote-build-cache-push: true
        develocity-local-build-cache-enabled: false

    - name: Run a Gradle build with the Develocity remote build cache
      run: ./gradlew build --build-cache
```

# Dependency verification

Develocity injection, Build Scan publishing and Dependency Graph generation all work by applying external plugins to your build.
+27
Changes for setup-gradle/action-types.yml: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -136,6 +136,33 @@ inputs:
  develocity-ccud-plugin-version:
    type: string

  develocity-enforce-build-cache:
    type: boolean

  develocity-remote-build-cache-enabled:
    type: boolean

  develocity-remote-build-cache-push:
    type: boolean

  develocity-remote-build-cache-server:
    type: string

  develocity-remote-build-cache-path:
    type: string

  develocity-remote-build-cache-allow-insecure-protocol:
    type: boolean

  develocity-remote-build-cache-allow-untrusted-server:
    type: boolean

  develocity-local-build-cache-enabled:
    type: boolean

  develocity-local-build-cache-push:
    type: boolean

  gradle-plugin-repository-url:
    type: string

+37 −1
Changes for setup-gradle/action.yml: 37 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -174,7 +174,7 @@ inputs:
    required: false

  develocity-enforce-url:
    description: Enforce the configured Develocity URL over a URL configured in the project's build; set to _true_ to enforce publication of build scans to the configured Develocity URL.
    description: Enforce the configured Develocity URL over a URL configured in the project's build; set to _true_ to enforce publication of build scans to the configured Develocity URL; a Develocity Build Cache connector without a server defined follows the enforced URL, but a server explicitly configured in the project's build is not changed.
    required: false

  develocity-plugin-version:
@@ -185,6 +185,42 @@ inputs:
    description: The version of the Common Custom User Data Gradle plugin to apply, if any.
    required: false

  develocity-enforce-build-cache:
    description: Replace a remote build cache configured in the project's build with the Develocity Build Cache connector.
    required: false

  develocity-remote-build-cache-enabled:
    description: Enables the remote build cache.
    required: false

  develocity-remote-build-cache-push:
    description: Store task outputs in the remote build cache.
    required: false

  develocity-remote-build-cache-server:
    description: The URL of the remote build cache server; the Develocity server is used by default.
    required: false

  develocity-remote-build-cache-path:
    description: The path of the remote build cache on the server.
    required: false

  develocity-remote-build-cache-allow-insecure-protocol:
    description: Allow communication with the remote build cache over plain HTTP.
    required: false

  develocity-remote-build-cache-allow-untrusted-server:
    description: Allow communication with an untrusted remote build cache server.
    required: false

  develocity-local-build-cache-enabled:
    description: Enables the local build cache.
    required: false

  develocity-local-build-cache-push:
    description: Store task outputs in the local build cache.
    required: false

  gradle-plugin-repository-url:
    description: The URL of the repository to use when resolving the Develocity and CCUD plugins; the Gradle Plugin Portal is used by default.
    required: false
+36
Changes for sources/src/configuration.ts: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -312,6 +312,42 @@ export class DevelocityConfig {
        return core.getInput('develocity-ccud-plugin-version')
    }

    getDevelocityEnforceBuildCache(): boolean | undefined {
        return getOptionalBooleanInput('develocity-enforce-build-cache')
    }

    getDevelocityRemoteBuildCacheEnabled(): boolean | undefined {
        return getOptionalBooleanInput('develocity-remote-build-cache-enabled')
    }

    getDevelocityRemoteBuildCachePush(): boolean | undefined {
        return getOptionalBooleanInput('develocity-remote-build-cache-push')
    }

    getDevelocityRemoteBuildCacheServer(): string {
        return core.getInput('develocity-remote-build-cache-server')
    }

    getDevelocityRemoteBuildCachePath(): string {
        return core.getInput('develocity-remote-build-cache-path')
    }

    getDevelocityRemoteBuildCacheAllowInsecureProtocol(): boolean | undefined {
        return getOptionalBooleanInput('develocity-remote-build-cache-allow-insecure-protocol')
    }

    getDevelocityRemoteBuildCacheAllowUntrustedServer(): boolean | undefined {
        return getOptionalBooleanInput('develocity-remote-build-cache-allow-untrusted-server')
    }

    getDevelocityLocalBuildCacheEnabled(): boolean | undefined {
        return getOptionalBooleanInput('develocity-local-build-cache-enabled')
    }

    getDevelocityLocalBuildCachePush(): boolean | undefined {
        return getOptionalBooleanInput('develocity-local-build-cache-push')
    }

    getPluginRepository(): PluginRepositoryConfig {
        return new PluginRepositoryConfig()
    }
+33
Changes for sources/src/develocity/build-scan.ts: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,39 @@ export function setup(config: DevelocityConfig): void {
        'DEVELOCITY_INJECTION_PLUGIN_REPOSITORY_PASSWORD',
        config.getPluginRepository().getPassword()
    )
    maybeExportVariableNotEmpty('DEVELOCITY_INJECTION_ENFORCE_BUILD_CACHE', config.getDevelocityEnforceBuildCache())
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_ENABLED',
        config.getDevelocityRemoteBuildCacheEnabled()
    )
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_PUSH',
        config.getDevelocityRemoteBuildCachePush()
    )
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_SERVER',
        config.getDevelocityRemoteBuildCacheServer()
    )
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_PATH',
        config.getDevelocityRemoteBuildCachePath()
    )
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_ALLOW_INSECURE_PROTOCOL',
        config.getDevelocityRemoteBuildCacheAllowInsecureProtocol()
    )
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_REMOTE_BUILD_CACHE_ALLOW_UNTRUSTED_SERVER',
        config.getDevelocityRemoteBuildCacheAllowUntrustedServer()
    )
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_LOCAL_BUILD_CACHE_ENABLED',
        config.getDevelocityLocalBuildCacheEnabled()
    )
    maybeExportVariableNotEmpty(
        'DEVELOCITY_INJECTION_LOCAL_BUILD_CACHE_PUSH',
        config.getDevelocityLocalBuildCachePush()
    )

    // If build-scan-publish is enabled, ensure the environment variables are set
    // The DEVELOCITY_PLUGIN_VERSION and DEVELOCITY_CCUD_PLUGIN_VERSION are set to the latest versions
Loading