mirror of
https://github.com/gradle/actions.git
synced 2026-08-20 21:02:50 +00:00
Support experimental project-entry caching (configuration-cache + build-logic) (#994)
Pass develocityAccessToken and develocityServerUrl the `gradle-actions-caching`: required to support project-entry caching (build-logic + configuration-cache), which has experimental support in 'gradle-actions-cache@v0.8.0. This support is not yet released and will be available as a restricted trial. --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
bbaaec0da2
commit
9c445f57df
@@ -1,8 +1,7 @@
|
||||
import * as core from '@actions/core'
|
||||
import {DevelocityConfig} from '../configuration'
|
||||
import {setupToken} from './short-lived-token'
|
||||
|
||||
export async function setup(config: DevelocityConfig): Promise<void> {
|
||||
export function setup(config: DevelocityConfig): void {
|
||||
maybeExportVariable('DEVELOCITY_INJECTION_INIT_SCRIPT_NAME', 'gradle-actions.inject-develocity.init.gradle')
|
||||
maybeExportVariable('DEVELOCITY_INJECTION_CUSTOM_VALUE', 'gradle-actions')
|
||||
|
||||
@@ -39,12 +38,6 @@ export async function setup(config: DevelocityConfig): Promise<void> {
|
||||
maybeExportVariable('DEVELOCITY_INJECTION_TERMS_OF_USE_URL', config.getTermsOfUseUrl())
|
||||
maybeExportVariable('DEVELOCITY_INJECTION_TERMS_OF_USE_AGREE', config.getTermsOfUseAgree())
|
||||
}
|
||||
|
||||
return setupToken(
|
||||
config.getDevelocityAccessKey(),
|
||||
config.getDevelocityAllowUntrustedServer(),
|
||||
config.getDevelocityTokenExpiry()
|
||||
)
|
||||
}
|
||||
|
||||
function maybeExportVariable(variableName: string, value: unknown): void {
|
||||
|
||||
@@ -3,28 +3,41 @@ import * as httpm from '@actions/http-client'
|
||||
import {DevelocityConfig} from '../configuration'
|
||||
import {recordDeprecation} from '../deprecation-collector'
|
||||
|
||||
export async function setupToken(
|
||||
develocityAccessKey: string,
|
||||
develocityAllowUntrustedServer: boolean | undefined,
|
||||
develocityTokenExpiry: string
|
||||
): Promise<void> {
|
||||
if (develocityAccessKey) {
|
||||
try {
|
||||
core.debug('Fetching short-lived token...')
|
||||
const tokens = await getToken(develocityAccessKey, develocityAllowUntrustedServer, develocityTokenExpiry)
|
||||
if (tokens != null && !tokens.isEmpty()) {
|
||||
core.debug(`Got token(s), setting the access key env vars`)
|
||||
const token = tokens.raw()
|
||||
core.setSecret(token)
|
||||
exportAccessKeyEnvVars(token)
|
||||
} else {
|
||||
handleMissingAccessToken()
|
||||
}
|
||||
} catch (e) {
|
||||
handleMissingAccessToken()
|
||||
core.warning(`Failed to fetch short-lived token, reason: ${e}`)
|
||||
}
|
||||
/**
|
||||
* Exchange the configured Develocity access key(s) for short-lived tokens, export them as the access
|
||||
* key env vars, and return the short-lived token matching the configured Develocity server URL (for
|
||||
* use as the `develocityAccessToken` cache option). Returns `undefined` when there is no access key,
|
||||
* token fetching fails, or no token matches the configured server.
|
||||
*/
|
||||
export async function setupToken(config: DevelocityConfig): Promise<string | undefined> {
|
||||
const develocityAccessKey = config.getDevelocityAccessKey()
|
||||
if (!develocityAccessKey) {
|
||||
return undefined
|
||||
}
|
||||
try {
|
||||
core.debug('Fetching short-lived token...')
|
||||
const tokens = await getToken(
|
||||
develocityAccessKey,
|
||||
config.getDevelocityAllowUntrustedServer(),
|
||||
config.getDevelocityTokenExpiry()
|
||||
)
|
||||
if (tokens != null && !tokens.isEmpty()) {
|
||||
core.debug(`Got token(s), setting the access key env vars`)
|
||||
const token = tokens.raw()
|
||||
core.setSecret(token)
|
||||
exportAccessKeyEnvVars(token)
|
||||
for (const k of tokens.keys) {
|
||||
core.setSecret(k.key)
|
||||
}
|
||||
const serverUrl = config.getDevelocityUrl()
|
||||
return serverUrl ? resolveTokenForServer(tokens, serverUrl) : undefined
|
||||
}
|
||||
handleMissingAccessToken()
|
||||
} catch (e) {
|
||||
handleMissingAccessToken()
|
||||
core.warning(`Failed to fetch short-lived token, reason: ${e}`)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function exportAccessKeyEnvVars(value: string): void {
|
||||
@@ -174,3 +187,20 @@ export class DevelocityAccessCredentials {
|
||||
return this.accessKeyRegexp.test(allKeys)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the token whose hostname matches a given Develocity server URL. Returns `undefined`
|
||||
* (fail-closed) when the server URL is empty or no token matches the server's host.
|
||||
*/
|
||||
export function resolveTokenForServer(tokens: DevelocityAccessCredentials, serverUrl: string): string | undefined {
|
||||
if (!serverUrl) {
|
||||
return undefined
|
||||
}
|
||||
let host: string
|
||||
try {
|
||||
host = new URL(serverUrl).hostname
|
||||
} catch {
|
||||
host = serverUrl // tolerate a bare hostname (no scheme)
|
||||
}
|
||||
return tokens.keys.find(k => k.hostname === host)?.key
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user