Slackbot
06/07/2023, 9:08 AMChris Lee
06/07/2023, 10:06 AMDarragh Bailey
06/07/2023, 11:39 AMproviders.gradleProperty(<propertyName>).getOrElse(<default>)
method and that provides access to the properties with expected and consistent behaviour, though I did find that I needed to modify the script to get it to work consistently. From settings.gradle I needed to switch to use the same buildscript block:
buildscript {
apply from: "custom/build.gradle"
}
And then within the included script I could use the following to have a single repositories block that is applied correctly irrespective if it's loaded from the settings.gradle and needs to apply to the pluginManagement object or from projects and just needs to apply to the project object:
def configObject
if (this.hasProperty('settings')) {
configObject = settings.pluginManagement
} else {
configObject = this
}
configObject.repositories {
maven {
name repoName
url "https://${domain}-${domainOwner}.d.codeartifact.${region}.<http://amazonaws.com/maven/${repoName}/|amazonaws.com/maven/${repoName}/>"
credentials {
username 'aws'
password getArtifactToken
}
}
}
Darragh Bailey
06/07/2023, 11:40 AMephemient
06/07/2023, 9:38 PMrepositories {
maven(url = "https://$domain-$domainOwner.d.codeartifact.$<http://region.amazonaws.com/maven/$repoName%22|region.amazonaws.com/maven/$repoName">) {
name = "CodeArtifact"
credentials(PasswordCredentials::class)
}
}
and set the environment outside of Gradle with
ORG_GRADLE_PROJECT_CodeArtifactUsername=aws
ORG_GRADLE_PROJECT_CodeArtifactPassword=$(aws codeartifact get-authorization-token --domain $domain --domain-owner $domainOwner --region $region --query authorizationToken --output text)
then won't everything just work?ephemient
06/07/2023, 9:40 PMThomas Broyer
06/08/2023, 7:46 AMdependencyResolutionManagement
in the the settings script rather than declaring the repository in every build script (not always applicable of course)Darragh Bailey
06/08/2023, 10:07 AMDarragh Bailey
06/12/2023, 3:08 PMdef domain = providers.gradleProperty('domain').getOrElse('<defaultDomain>')
def domainOwner = providers.gradleProperty('domainOwner').getOrElse('<defaultDomainOwner>')
def region = providers.gradleProperty('region').getOrElse('<defaultRegion>')
def repoName = providers.gradleProperty('repoName').get()
def artifactAuthToken = {
if (settings.startParameter.offline == true) {
this.logger.debug("running offline, set dummy password to skip generation")
return 'OFFLINE_MODE_NO_TOKEN_NEEDED'
}
if (System.env.ARTIFACT_AUTH_TOKEN) {
this.logger.debug('using auth token from environment variable ARTIFACT_AUTH_TOKEN')
return System.env.ARTIFACT_AUTH_TOKEN
}
this.logger.debug('calling "aws codeartifact .." CLI to get fresh token')
def process = "aws codeartifact get-authorization-token --domain $domain --domain-owner $domainOwner --region $region --query authorizationToken --output text".execute()
def stdout = new StringBuffer(), stderr = new StringBuffer()
process.consumeProcessOutput(stdout, stderr)
process.waitForOrKill(3000)
if (stderr.toString().contains("Unable to locate credentials.")) {
throw new GradleException('You do not have a valid AWS session in the environment, please login')
} else if (process.exitValue() != 0) {
// handles if process gets stuck and has to be terminated
this.logger.error("Failed to retrieve token, check if logged in:\nstderr: ${stderr.toString()}\nstdout: ${stdout.toString()}")
throw new GradleException('Failed to retrieve token, check if logged in')
}
return stdout.toString()
}
settings.dependencyResolutionManagement.repositories {
maven {
name repoName.replace('-', '')
url "https://${domain}-${domainOwner}.d.codeartifact.${region}.<http://amazonaws.com/maven/${repoName}/|amazonaws.com/maven/${repoName}/>"
credentials {
username 'aws'
password artifactAuthToken
}
}
}
settings.dependencyResolutionManagement.repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
So reduced it down by using the settings.dependencyResolutionManagement.repositories
to a single block, thanks @Thomas Broyer for the suggestion.