From 60c43cb5630976c9bde481234e849870a0bc2dc7 Mon Sep 17 00:00:00 2001 From: daz Date: Tue, 23 Jan 2024 16:44:34 -0700 Subject: [PATCH] Make it easy to publish to scans.gradle.com - Allow init-script to publish to scans.gradle.com - Add paramaters to enable build scan publishing - Test coverage for build scan publishing --- .../integ-test-inject-develocity.yml | 47 +++++++++++++++++-- action.yml | 15 ++++++ src/build-scan.ts | 33 +++++++++++++ src/input-params.ts | 12 +++++ ...build-action.inject-develocity.init.gradle | 24 +++++++--- src/setup-gradle.ts | 3 ++ 6 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/build-scan.ts diff --git a/.github/workflows/integ-test-inject-develocity.yml b/.github/workflows/integ-test-inject-develocity.yml index c7b6e18..37655c0 100644 --- a/.github/workflows/integ-test-inject-develocity.yml +++ b/.github/workflows/integ-test-inject-develocity.yml @@ -19,14 +19,15 @@ env: DOWNLOAD_DIST: ${{ inputs.download-dist }} GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: provision-gradle-versions-${{ inputs.cache-key-prefix }} GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true - DEVELOCITY_INJECTION_ENABLED: true - DEVELOCITY_URL: https://ge.solutions-team.gradle.com - DEVELOCITY_PLUGIN_VERSION: 3.16.1 - DEVELOCITY_CCUD_PLUGIN_VERSION: 1.12.1 - GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }} # This env var has not (yet) been renamed/aliased in GE plugin 3.16.1 jobs: inject-develocity: + env: + DEVELOCITY_INJECTION_ENABLED: true + DEVELOCITY_URL: https://ge.solutions-team.gradle.com + DEVELOCITY_PLUGIN_VERSION: 3.16.1 + DEVELOCITY_CCUD_PLUGIN_VERSION: 1.12.1 + GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }} # This env var has not (yet) been renamed/aliased in GE plugin 3.16.1 strategy: matrix: gradle: [current, 7.6.2, 6.9.4, 5.6.4] @@ -58,3 +59,39 @@ jobs: with: script: | core.setFailed('No Build Scan detected') + + build-scan-publish: + strategy: + matrix: + gradle: [current, 7.6.2, 6.9.4, 5.6.4] + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Download distribution if required + uses: ./.github/actions/download-dist + - name: Setup Java + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 8 + - name: Setup Gradle + id: setup-gradle + uses: ./ + with: + cache-read-only: false # For testing, allow writing cache entries on non-default branches + gradle-version: ${{ matrix.gradle }} + build-scan-publish: true + build-scan-terms-of-service-url: "https://gradle.com/terms-of-service" + build-scan-terms-of-service-agree: "yes" + - 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/action.yml b/action.yml index 13beb6e..da2e0f0 100644 --- a/action.yml +++ b/action.yml @@ -82,6 +82,21 @@ inputs: description: Specifies the number of days to retain any artifacts generated by the action. If not set, the default retention settings for the repository will apply. required: false + build-scan-publish: + description: | + Set to 'true' to automatically publish build results as a Build Scan on scans.gradle.com. + For publication to succeed without user input, you must also provide values for `build-scan-terms-of-service-url` and 'build-scan-terms-of-service-agree'. + required: false + default: false + + build-scan-terms-of-service-url: + description: The URL to the Build ScanĀ® terms of service. This input must be set to 'https://gradle.com/terms-of-service'. + required: false + + build-scan-terms-of-service-agree: + description: Indicate that you agree to the Build ScanĀ® terms of service. This input value must be "yes". + required: false + # DEPRECATED ACTION INPUTS arguments: description: Gradle command line arguments (supports multi-line input) diff --git a/src/build-scan.ts b/src/build-scan.ts new file mode 100644 index 0000000..290859c --- /dev/null +++ b/src/build-scan.ts @@ -0,0 +1,33 @@ +import * as core from '@actions/core' +import { + getBuildScanPublishEnabled, + getBuildScanTermsOfServiceUrl, + getBuildScanTermsOfServiceAgree +} from './input-params' + +export function setup(): void { + if (getBuildScanPublishEnabled() && verifyTermsOfServiceAgreement()) { + maybeExportVariable('DEVELOCITY_INJECTION_ENABLED', 'true') + maybeExportVariable('DEVELOCITY_PLUGIN_VERSION', '3.16.1') + maybeExportVariable('DEVELOCITY_CCUD_PLUGIN_VERSION', '1.12.1') + maybeExportVariable('BUILD_SCAN_TERMS_OF_SERVICE_URL', getBuildScanTermsOfServiceUrl()) + maybeExportVariable('BUILD_SCAN_TERMS_OF_SERVICE_AGREE', getBuildScanTermsOfServiceAgree()) + } +} + +function verifyTermsOfServiceAgreement(): boolean { + if ( + getBuildScanTermsOfServiceUrl() !== 'https://gradle.com/terms-of-service' || + getBuildScanTermsOfServiceAgree() !== 'yes' + ) { + core.warning(`Terms of service must be agreed in order to publish build scans.`) + return false + } + return true +} + +function maybeExportVariable(variableName: string, value: unknown): void { + if (!process.env[variableName]) { + core.exportVariable(variableName, value) + } +} diff --git a/src/input-params.ts b/src/input-params.ts index 4043aea..c7a1c39 100644 --- a/src/input-params.ts +++ b/src/input-params.ts @@ -75,6 +75,18 @@ export function getPRCommentOption(): JobSummaryOption { return parseJobSummaryOption('add-job-summary-as-pr-comment') } +export function getBuildScanPublishEnabled(): boolean { + return getBooleanInput('build-scan-publish') +} + +export function getBuildScanTermsOfServiceUrl(): string { + return core.getInput('build-scan-terms-of-service-url') +} + +export function getBuildScanTermsOfServiceAgree(): string { + return core.getInput('build-scan-terms-of-service-agree') +} + function parseJobSummaryOption(paramName: string): JobSummaryOption { const val = core.getInput(paramName) switch (val.toLowerCase().trim()) { diff --git a/src/resources/init-scripts/gradle-build-action.inject-develocity.init.gradle b/src/resources/init-scripts/gradle-build-action.inject-develocity.init.gradle index a393ea7..ab125d8 100644 --- a/src/resources/init-scripts/gradle-build-action.inject-develocity.init.gradle +++ b/src/resources/init-scripts/gradle-build-action.inject-develocity.init.gradle @@ -82,6 +82,8 @@ def geEnforceUrl = Boolean.parseBoolean(getInputParam('develocity.enforce-url')) def buildScanUploadInBackground = Boolean.parseBoolean(getInputParam('develocity.build-scan.upload-in-background')) def gePluginVersion = getInputParam('develocity.plugin.version') def ccudPluginVersion = getInputParam('develocity.ccud-plugin.version') +def buildScanTermsOfServiceUrl = getInputParam('build-scan.terms-of-service.url') +def buildScanTermsOfServiceAgree = getInputParam('build-scan.terms-of-service.agree') def atLeastGradle4 = GradleVersion.current() >= GradleVersion.version('4.0') @@ -103,10 +105,15 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) { } if (!scanPluginComponent) { logger.quiet("Applying $BUILD_SCAN_PLUGIN_CLASS via init script") - logger.quiet("Connection to Develocity: $geUrl, allowUntrustedServer: $geAllowUntrustedServer") applyPluginExternally(pluginManager, BUILD_SCAN_PLUGIN_CLASS) - buildScan.server = geUrl - buildScan.allowUntrustedServer = geAllowUntrustedServer + if (geUrl) { + logger.quiet("Connection to Develocity: $geUrl, allowUntrustedServer: $geAllowUntrustedServer") + buildScan.server = geUrl + buildScan.allowUntrustedServer = geAllowUntrustedServer + } else if (buildScanTermsOfServiceUrl && buildScanTermsOfServiceAgree) { + buildScan.termsOfServiceUrl = buildScanTermsOfServiceUrl + buildScan.termsOfServiceAgree = buildScanTermsOfServiceAgree + } buildScan.publishAlways() if (buildScan.metaClass.respondsTo(buildScan, 'setUploadInBackground', Boolean)) buildScan.uploadInBackground = buildScanUploadInBackground // uploadInBackground not available for build-scan-plugin 1.16 buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE @@ -139,11 +146,16 @@ if (GradleVersion.current() < GradleVersion.version('6.0')) { if (gePluginVersion) { if (!settings.pluginManager.hasPlugin(DEVELOCITY_PLUGIN_ID)) { logger.quiet("Applying $DEVELOCITY_PLUGIN_CLASS via init script") - logger.quiet("Connection to Develocity: $geUrl, allowUntrustedServer: $geAllowUntrustedServer") applyPluginExternally(settings.pluginManager, DEVELOCITY_PLUGIN_CLASS) extensionsWithPublicType(settings, DEVELOCITY_EXTENSION_CLASS).collect { settings[it.name] }.each { ext -> - ext.server = geUrl - ext.allowUntrustedServer = geAllowUntrustedServer + if (geUrl) { + logger.quiet("Connection to Develocity: $geUrl, allowUntrustedServer: $geAllowUntrustedServer") + ext.server = geUrl + ext.allowUntrustedServer = geAllowUntrustedServer + } else if (buildScanTermsOfServiceUrl && buildScanTermsOfServiceAgree) { + ext.buildScan.termsOfServiceUrl = buildScanTermsOfServiceUrl + ext.buildScan.termsOfServiceAgree = buildScanTermsOfServiceAgree + } ext.buildScan.publishAlways() ext.buildScan.uploadInBackground = buildScanUploadInBackground ext.buildScan.value CI_AUTO_INJECTION_CUSTOM_VALUE_NAME, CI_AUTO_INJECTION_CUSTOM_VALUE_VALUE diff --git a/src/setup-gradle.ts b/src/setup-gradle.ts index b1f1c93..18f9dd1 100644 --- a/src/setup-gradle.ts +++ b/src/setup-gradle.ts @@ -7,6 +7,7 @@ import * as layout from './repository-layout' import * as params from './input-params' import * as dependencyGraph from './dependency-graph' import * as jobSummary from './job-summary' +import * as buildScan from './build-scan' import {loadBuildResults} from './build-results' import {CacheListener} from './cache-reporting' @@ -41,6 +42,8 @@ export async function setup(): Promise { core.saveState(CACHE_LISTENER, cacheListener.stringify()) await dependencyGraph.setup(params.getDependencyGraphOption()) + + buildScan.setup() } export async function complete(): Promise {