From d0a116fff52a680bc1541b7c4c87ca37d30abf00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Villar?= Date: Thu, 13 Jun 2024 11:42:47 -0700 Subject: [PATCH] Adding Develocity input actions (#244) Adding Develocity input actions. If an input is configured in the action, it will generate the environment variable, example: ```yaml - name: Setup Gradle uses: gradle/actions/setup-gradle@v3 with: develocity-injection-enabled: true develocity-url: https://develocity.your-server.com develocity-plugin-version: 3.17.4 - name: Run a Gradle build with Develocity injection enabled from input actions run: ./gradlew build ``` This configuration will create the environment variables: ``` DEVELOCITY_INJECTION_ENABLED=true DEVELOCITY_URL=https://develocity.your-server.com DEVELOCITY_PLUGIN_VERSION=3.17.4 ``` Relation variable-input available: | Variable | Input | |--------------------------------------|--------------------------------------| | DEVELOCITY_INJECTION_ENABLED | develocity-injection-enabled | | DEVELOCITY_URL | develocity-url | | DEVELOCITY_ALLOW_UNTRUSTED_SERVER | develocity-allow-untrusted-server | | DEVELOCITY_CAPTURE_FILE_FINGERPRINTS | develocity-capture-file-fingerprints | | DEVELOCITY_ENFORCE_URL | develocity-enforce-url | | DEVELOCITY_PLUGIN_VERSION | develocity-plugin-version | | DEVELOCITY_CCUD_PLUGIN_VERSION | develocity-ccud-plugin-version | | GRADLE_PLUGIN_REPOSITORY_URL | gradle-plugin-repository-url | | GRADLE_PLUGIN_REPOSITORY_USERNAME | gradle-plugin-repository-username | | GRADLE_PLUGIN_REPOSITORY_PASSWORD | gradle-plugin-repository-password | --- .../integ-test-inject-develocity.yml | 40 +++++++++++++ docs/setup-gradle.md | 60 ++++++++++++++----- setup-gradle/action.yml | 40 +++++++++++++ sources/src/configuration.ts | 52 ++++++++++++++++ sources/src/develocity/build-scan.ts | 17 ++++++ 5 files changed, 194 insertions(+), 15 deletions(-) diff --git a/.github/workflows/integ-test-inject-develocity.yml b/.github/workflows/integ-test-inject-develocity.yml index cc58bd1..c2088f8 100644 --- a/.github/workflows/integ-test-inject-develocity.yml +++ b/.github/workflows/integ-test-inject-develocity.yml @@ -159,3 +159,43 @@ jobs: run: "[ \"${DEVELOCITY_ACCESS_KEY}\" == \"\" ] || (echo 'DEVELOCITY_ACCESS_KEY has leaked!'; exit 1)" - name: Check access key is not blank (GRADLE_ENTERPRISE_ACCESS_KEY) run: "[ \"${GRADLE_ENTERPRISE_ACCESS_KEY}\" != \"\" ] || (echo 'GRADLE_ENTERPRISE_ACCESS_KEY is still supported in v3!'; exit 1)" + + inject-develocity-with-access-key-from-input-actions: + env: + DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }} + strategy: + fail-fast: false + matrix: + gradle: [ current, 7.6.2, 6.9.4, 5.6.4 ] + os: ${{fromJSON(inputs.runner-os)}} + plugin-version: [ 3.16.2, 3.17.4 ] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Initialize integ-test + uses: ./.github/actions/init-integ-test + - name: Setup Java + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 8 + - name: Setup Gradle + id: setup-gradle + uses: ./setup-gradle + with: + cache-read-only: false # For testing, allow writing cache entries on non-default branches + gradle-version: ${{ matrix.gradle }} + develocity-injection-enabled: true + develocity-url: 'https://ge.solutions-team.gradle.com' + develocity-plugin-version: ${{ matrix.plugin-version }} + - name: Run Gradle build + id: gradle + working-directory: .github/workflow-samples/no-ge + run: gradle help + - name: Check Build Scan url + if: ${{ !steps.gradle.outputs.build-scan-url }} + uses: actions/github-script@v7 + with: + script: | + core.setFailed('No Build Scan detected') diff --git a/docs/setup-gradle.md b/docs/setup-gradle.md index e0eea4c..2a0fe55 100644 --- a/docs/setup-gradle.md +++ b/docs/setup-gradle.md @@ -708,20 +708,20 @@ The same auto-injection behavior is available for the Common Custom User Data Gr ## Enabling Develocity injection -To enable Develocity injection for your build, you must provide the required configuration via environment variables. +To enable Develocity injection for your build, you must provide the required configuration via inputs. Here's a minimal example: ```yaml - name: Setup Gradle uses: gradle/actions/setup-gradle@v3 + with: + develocity-injection-enabled: true + develocity-url: https://develocity.your-server.com + develocity-plugin-version: 3.17.5 - name: Run a Gradle build with Develocity injection enabled run: ./gradlew build - env: - DEVELOCITY_INJECTION_ENABLED: true - DEVELOCITY_URL: https://develocity.your-server.com - DEVELOCITY_PLUGIN_VERSION: 3.17.5 ``` This configuration will automatically apply `v3.17.5` of the [Develocity Gradle plugin](https://docs.gradle.com/develocity/gradle-plugin/), and publish build scans to https://develocity.your-server.com. @@ -758,16 +758,46 @@ The `init-script` supports several additional configuration parameters that you | 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_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 | -| 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 | +| 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-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 | +| 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 | + +The input parameters can be expressed as environment variables following the relationships outlined in the table below: + +| Input | Environment Variable | +|--------------------------------------|--------------------------------------| +| develocity-injection-enabled | DEVELOCITY_INJECTION_ENABLED | +| develocity-url | DEVELOCITY_URL | +| develocity-allow-untrusted-server | DEVELOCITY_ALLOW_UNTRUSTED_SERVER | +| develocity-capture-file-fingerprints | DEVELOCITY_CAPTURE_FILE_FINGERPRINTS | +| develocity-enforce-url | DEVELOCITY_ENFORCE_URL | +| develocity-plugin-version | DEVELOCITY_PLUGIN_VERSION | +| develocity-ccud-plugin-version | DEVELOCITY_CCUD_PLUGIN_VERSION | +| gradle-plugin-repository-url | GRADLE_PLUGIN_REPOSITORY_URL | +| gradle-plugin-repository-username | GRADLE_PLUGIN_REPOSITORY_USERNAME | +| gradle-plugin-repository-password | GRADLE_PLUGIN_REPOSITORY_PASSWORD | + + +Here's an example using the env vars: + +```yaml + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v3 + + - name: Run a Gradle build with Develocity injection enabled with environment variables + run: ./gradlew build + env: + DEVELOCITY_INJECTION_ENABLED: true + DEVELOCITY_URL: https://develocity.your-server.com + DEVELOCITY_PLUGIN_VERSION: 3.17.5 +``` ## Publishing to scans.gradle.com diff --git a/setup-gradle/action.yml b/setup-gradle/action.yml index f768c52..1c93cad 100644 --- a/setup-gradle/action.yml +++ b/setup-gradle/action.yml @@ -108,6 +108,46 @@ inputs: description: The Develocity short-lived access tokens expiry in hours. Default is 2 hours. required: false + develocity-injection-enabled: + description: Enables Develocity injection. + required: false + + develocity-url: + description: The URL for the Develocity server. + required: false + + develocity-allow-untrusted-server: + description: Allow communication with an untrusted server; set to _true_ if your Develocity instance is using a self-signed. + required: false + + develocity-capture-file-fingerprints: + description: Enables capturing the paths and content hashes of each individual input file. + 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. + required: false + + develocity-plugin-version: + description: The version of the Develocity Gradle plugin to apply. + required: false + + develocity-ccud-plugin-version: + description: The version of the Common Custom User Data Gradle plugin to apply, if any. + 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 + + gradle-plugin-repository-username: + description: The username for the repository URL to use when resolving the Develocity and CCUD. + required: false + + gradle-plugin-repository-password: + description: 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. + required: false + # Wrapper validation configuration validate-wrappers: description: | diff --git a/sources/src/configuration.ts b/sources/src/configuration.ts index ba4552a..4621cf3 100644 --- a/sources/src/configuration.ts +++ b/sources/src/configuration.ts @@ -216,6 +216,46 @@ export class BuildScanConfig { return core.getInput('develocity-token-expiry') } + getDevelocityInjectionEnabled(): boolean | undefined { + return getMaybeBooleanInput('develocity-injection-enabled') + } + + getDevelocityUrl(): string { + return core.getInput('develocity-url') + } + + getDevelocityAllowUntrustedServer(): boolean | undefined { + return getMaybeBooleanInput('develocity-allow-untrusted-server') + } + + getDevelocityCaptureFileFingerprints(): boolean | undefined { + return getMaybeBooleanInput('develocity-capture-file-fingerprints') + } + + getDevelocityEnforceUrl(): boolean | undefined { + return getMaybeBooleanInput('develocity-enforce-url') + } + + getDevelocityPluginVersion(): string { + return core.getInput('develocity-plugin-version') + } + + getDevelocityCcudPluginVersion(): string { + return core.getInput('develocity-ccud-plugin-version') + } + + getGradlePluginRepositoryUrl(): string { + return core.getInput('gradle-plugin-repository-url') + } + + getGradlePluginRepositoryUsername(): string { + return core.getInput('gradle-plugin-repository-username') + } + + getGradlePluginRepositoryPassword(): string { + return core.getInput('gradle-plugin-repository-password') + } + private verifyTermsOfUseAgreement(): boolean { if ( (this.getBuildScanTermsOfUseUrl() !== 'https://gradle.com/terms-of-service' && @@ -329,3 +369,15 @@ function getBooleanInput(paramName: string, paramDefault = false): boolean { } throw TypeError(`The value '${paramValue} is not valid for '${paramName}. Valid values are: [true, false]`) } + +function getMaybeBooleanInput(paramName: string): boolean | undefined { + const paramValue = core.getInput(paramName) + switch (paramValue?.toLowerCase().trim()) { + case 'false': + return false + case 'true': + return true + default: + return undefined + } +} diff --git a/sources/src/develocity/build-scan.ts b/sources/src/develocity/build-scan.ts index df13690..b381a77 100644 --- a/sources/src/develocity/build-scan.ts +++ b/sources/src/develocity/build-scan.ts @@ -12,6 +12,17 @@ export async function setup(config: BuildScanConfig): Promise { maybeExportVariable('DEVELOCITY_TERMS_OF_USE_URL', config.getBuildScanTermsOfUseUrl()) maybeExportVariable('DEVELOCITY_TERMS_OF_USE_AGREE', config.getBuildScanTermsOfUseAgree()) } + + maybeExportVariableNotEmpty('DEVELOCITY_INJECTION_ENABLED', config.getDevelocityInjectionEnabled()) + maybeExportVariableNotEmpty('DEVELOCITY_URL', config.getDevelocityUrl()) + maybeExportVariableNotEmpty('DEVELOCITY_ALLOW_UNTRUSTED_SERVER', config.getDevelocityAllowUntrustedServer()) + maybeExportVariableNotEmpty('DEVELOCITY_CAPTURE_FILE_FINGERPRINTS', config.getDevelocityCaptureFileFingerprints()) + maybeExportVariableNotEmpty('DEVELOCITY_ENFORCE_URL', config.getDevelocityEnforceUrl()) + maybeExportVariableNotEmpty('DEVELOCITY_PLUGIN_VERSION', config.getDevelocityPluginVersion()) + maybeExportVariableNotEmpty('GRADLE_PLUGIN_REPOSITORY_URL', config.getGradlePluginRepositoryUrl()) + maybeExportVariableNotEmpty('GRADLE_PLUGIN_REPOSITORY_USERNAME', config.getGradlePluginRepositoryUsername()) + maybeExportVariableNotEmpty('GRADLE_PLUGIN_REPOSITORY_PASSWORD', config.getGradlePluginRepositoryPassword()) + setupToken( config.getDevelocityAccessKey(), config.getDevelocityTokenExpiry(), @@ -29,3 +40,9 @@ function maybeExportVariable(variableName: string, value: unknown): void { core.exportVariable(variableName, value) } } + +function maybeExportVariableNotEmpty(variableName: string, value: unknown): void { + if (value !== null && value !== undefined && value !== '') { + maybeExportVariable(variableName, value) + } +}