[Build] Replace build scan config script with plugin
See https://github.com/gradle/gradle-enterprise-build-config-samples/tree/129e82c556d7ef58527cd0acd89f58d1ff02d1f7/common-custom-user-data-gradle-plugin
This commit is contained in:
@@ -38,10 +38,6 @@ buildscript {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kotlinBuildProperties.buildScanServer != null) {
|
|
||||||
apply(from = "gradle/buildScanUserData.gradle")
|
|
||||||
}
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
base
|
base
|
||||||
idea
|
idea
|
||||||
|
|||||||
@@ -1,246 +0,0 @@
|
|||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This Gradle script captures data about the OS, IDE, CI, and Git and stores it in build scans via custom tags, custom links, and custom values.
|
|
||||||
*
|
|
||||||
* Proceed as following to benefit from this script in your Gradle build:
|
|
||||||
*
|
|
||||||
* - Copy this script to the root folder of your Gradle project, renaming it to 'build-scan-user-data.gradle'
|
|
||||||
* - Apply the Gradle Build Scan plugin
|
|
||||||
* - Point to your Gradle Enterprise server
|
|
||||||
* - Include this script in the root project's build.gradle(.kts) file via `apply from: 'build-scan-user-data.gradle'`
|
|
||||||
* - Further customize this script to your needs
|
|
||||||
*/
|
|
||||||
|
|
||||||
tagOs()
|
|
||||||
tagIde()
|
|
||||||
tagCiOrLocal()
|
|
||||||
addCiMetadata()
|
|
||||||
addGitMetadata()
|
|
||||||
addTestParallelization()
|
|
||||||
|
|
||||||
// Add here other scripts, if needed
|
|
||||||
//apply from:"${rootProject.projectDir}/<<other-script.gradle>>"
|
|
||||||
|
|
||||||
void tagOs() {
|
|
||||||
buildScan.tag System.getProperty('os.name')
|
|
||||||
}
|
|
||||||
|
|
||||||
void tagIde() {
|
|
||||||
if (project.hasProperty('android.injected.invoked.from.ide')) {
|
|
||||||
buildScan.tag 'Android Studio'
|
|
||||||
} else if (providers.systemProperty("idea.version").forUseAtConfigurationTime().present) {
|
|
||||||
buildScan.tag 'IntelliJ IDEA'
|
|
||||||
} else if (providers.systemProperty("eclipse.buildId").forUseAtConfigurationTime().present) {
|
|
||||||
buildScan.tag 'Eclipse'
|
|
||||||
} else if (!isCi()) {
|
|
||||||
buildScan.tag 'Cmd Line'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void tagCiOrLocal() {
|
|
||||||
buildScan.tag(isCi() ? 'CI' : 'LOCAL')
|
|
||||||
}
|
|
||||||
|
|
||||||
void addCiMetadata() {
|
|
||||||
if (isJenkins()) {
|
|
||||||
if (System.getenv('BUILD_URL')) {
|
|
||||||
buildScan.link 'Jenkins build', System.getenv('BUILD_URL')
|
|
||||||
}
|
|
||||||
if (System.getenv('BUILD_NUMBER')) {
|
|
||||||
buildScan.value 'CI build number', System.getenv('BUILD_NUMBER')
|
|
||||||
}
|
|
||||||
if (System.getenv('NODE_NAME')) {
|
|
||||||
def agentName = System.getenv('NODE_NAME') == 'master' ? 'master-node' : System.getenv('NODE_NAME')
|
|
||||||
buildScan.tag agentName
|
|
||||||
buildScan.value 'CI node name', agentName
|
|
||||||
}
|
|
||||||
if (System.getenv('JOB_NAME')) {
|
|
||||||
def jobNameLabel = 'CI job'
|
|
||||||
def jobName = System.getenv('JOB_NAME')
|
|
||||||
buildScan.value jobNameLabel, jobName
|
|
||||||
addCustomValueSearchLink buildScan, 'CI job build scans', [(jobNameLabel): jobName]
|
|
||||||
}
|
|
||||||
if (System.getenv('STAGE_NAME')) {
|
|
||||||
def stageNameLabel = 'CI stage'
|
|
||||||
def stageName = System.getenv('STAGE_NAME')
|
|
||||||
buildScan.value stageNameLabel, stageName
|
|
||||||
addCustomValueSearchLink buildScan, 'CI stage build scans', [(stageNameLabel): stageName]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isTeamCity()) {
|
|
||||||
def teamCityConfigurationFileProp = 'teamcity.configuration.properties.file'
|
|
||||||
if (project.hasProperty(teamCityConfigurationFileProp)) {
|
|
||||||
def properties = new Properties()
|
|
||||||
properties.load(new FileInputStream("${project.property(teamCityConfigurationFileProp)}"))
|
|
||||||
def teamCityServerUrl = properties.getProperty("teamcity.serverUrl")
|
|
||||||
if (teamCityServerUrl && project.hasProperty('build.number') && project.hasProperty('teamcity.buildType.id')) {
|
|
||||||
def teamCityBuildNumber = project.property('build.number')
|
|
||||||
def teamCityBuildTypeId = project.property('teamcity.buildType.id')
|
|
||||||
buildScan.link 'TeamCity build', "${appendIfMissing(teamCityServerUrl, '/')}viewLog.html?buildNumber=${URLEncoder.encode(teamCityBuildNumber, 'UTF-8')}&buildTypeId=${teamCityBuildTypeId}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (project.hasProperty('build.number')) {
|
|
||||||
buildScan.value 'CI build number', project.property('build.number')
|
|
||||||
}
|
|
||||||
if (project.hasProperty('agent.name')) {
|
|
||||||
def agentName = project.property('agent.name')
|
|
||||||
buildScan.tag agentName
|
|
||||||
buildScan.value 'CI agent name', agentName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isCircleCI()) {
|
|
||||||
if (System.getenv('CIRCLE_BUILD_URL')) {
|
|
||||||
buildScan.link 'CircleCI build', System.getenv('CIRCLE_BUILD_URL')
|
|
||||||
}
|
|
||||||
if (System.getenv('CIRCLE_BUILD_NUM')) {
|
|
||||||
buildScan.value 'CI build number', System.getenv('CIRCLE_BUILD_NUM')
|
|
||||||
}
|
|
||||||
if (System.getenv('CIRCLE_JOB')) {
|
|
||||||
def jobLabel = 'CI job'
|
|
||||||
def job = System.getenv('CIRCLE_JOB')
|
|
||||||
buildScan.value jobLabel, job
|
|
||||||
addCustomValueSearchLink buildScan, 'CI job build scans', [(jobLabel): job]
|
|
||||||
}
|
|
||||||
if (System.getenv('CIRCLE_WORKFLOW_ID')) {
|
|
||||||
def workflowIdLabel = 'CI workflow'
|
|
||||||
def workflowId = System.getenv('CIRCLE_WORKFLOW_ID')
|
|
||||||
buildScan.value workflowIdLabel, workflowId
|
|
||||||
addCustomValueSearchLink buildScan, 'CI workflow build scans', [(workflowIdLabel): workflowId]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isBamboo()) {
|
|
||||||
if (System.getenv('bamboo_resultsUrl')) {
|
|
||||||
buildScan.link 'Bamboo build', System.getenv('bamboo_resultsUrl')
|
|
||||||
}
|
|
||||||
if (System.getenv('bamboo_buildNumber')) {
|
|
||||||
buildScan.value 'CI build number', System.getenv('bamboo_buildNumber')
|
|
||||||
}
|
|
||||||
if (System.getenv('bamboo_planName')) {
|
|
||||||
def planNameLabel = 'CI plan'
|
|
||||||
def planName = System.getenv('bamboo_planName')
|
|
||||||
buildScan.value planNameLabel, planName
|
|
||||||
addCustomValueSearchLink buildScan, 'CI plan build scans', [(planNameLabel): planName]
|
|
||||||
}
|
|
||||||
if (System.getenv('bamboo_buildPlanName')) {
|
|
||||||
def jobNameLabel = 'CI job'
|
|
||||||
def jobName = System.getenv('bamboo_buildPlanName')
|
|
||||||
buildScan.value jobNameLabel, jobName
|
|
||||||
addCustomValueSearchLink buildScan, 'CI job build scans', [(jobNameLabel): jobName]
|
|
||||||
}
|
|
||||||
if (System.getenv('bamboo_agentId')) {
|
|
||||||
def agentId = System.getenv('bamboo_agentId')
|
|
||||||
buildScan.tag agentId
|
|
||||||
buildScan.value 'CI agent ID', agentId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Injected {
|
|
||||||
@Inject ExecOperations getExecOperations()
|
|
||||||
}
|
|
||||||
|
|
||||||
void addGitMetadata() {
|
|
||||||
def injected = project.objects.newInstance(Injected)
|
|
||||||
buildScan.background {
|
|
||||||
if (!isGitInstalled()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
def gitCommitId = execAndGetStdout(injected.execOperations, 'git', 'rev-parse', '--short=8', '--verify', 'HEAD')
|
|
||||||
def gitBranchName = execAndGetStdout(injected.execOperations, 'git', 'rev-parse', '--abbrev-ref', 'HEAD')
|
|
||||||
def gitStatus = execAndGetStdout(injected.execOperations, 'git', 'status', '--porcelain')
|
|
||||||
|
|
||||||
if (gitCommitId) {
|
|
||||||
def commitIdLabel = 'Git commit id'
|
|
||||||
value commitIdLabel, gitCommitId
|
|
||||||
addCustomValueSearchLink it, 'Git commit id build scans', [(commitIdLabel): gitCommitId]
|
|
||||||
def originUrl = execAndGetStdout(injected.execOperations, 'git', 'config', '--get', 'remote.origin.url')
|
|
||||||
if (originUrl.contains('github.com')) { // only for GitHub
|
|
||||||
def repoPath = (originUrl =~ /(.*)github\.com[\/|:](.*)/)[0][2]
|
|
||||||
if (repoPath.endsWith('.git')) {
|
|
||||||
repoPath = repoPath.substring(0, repoPath.length() - 4)
|
|
||||||
}
|
|
||||||
link 'Github Source', "https://github.com/$repoPath/tree/" + gitCommitId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (gitBranchName) {
|
|
||||||
tag gitBranchName
|
|
||||||
value 'Git branch', gitBranchName
|
|
||||||
}
|
|
||||||
if (gitStatus) {
|
|
||||||
tag 'Dirty'
|
|
||||||
value 'Git status', gitStatus
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void addTestParallelization() {
|
|
||||||
allprojects { p ->
|
|
||||||
p.tasks.withType(Test).configureEach { t -> doFirst { buildScan.value "${t.identityPath}#maxParallelForks", t.maxParallelForks.toString() } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isCi() {
|
|
||||||
isJenkins() || isTeamCity() || isCircleCI() || isBamboo()
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isJenkins() {
|
|
||||||
System.getenv('JENKINS_URL')
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isTeamCity() {
|
|
||||||
System.getenv('TEAMCITY_VERSION')
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isCircleCI() {
|
|
||||||
System.getenv('CIRCLECI')
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isBamboo() {
|
|
||||||
System.getenv('bamboo_resultsUrl')
|
|
||||||
}
|
|
||||||
|
|
||||||
static String execAndGetStdout(ExecOperations execOperations, String... args) {
|
|
||||||
def stdout = new ByteArrayOutputStream()
|
|
||||||
execOperations.exec {
|
|
||||||
it.commandLine(args)
|
|
||||||
it.standardOutput = stdout
|
|
||||||
}
|
|
||||||
trimAtEnd(stdout.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addCustomValueSearchLink(buildScan, String title, Map<String, String> search) {
|
|
||||||
if (buildScan.server) {
|
|
||||||
buildScan.link title, customValueSearchUrl(buildScan, search)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static String customValueSearchUrl(buildScan, Map<String, String> search) {
|
|
||||||
def query = search.collect { name, value ->
|
|
||||||
"search.names=${encodeURL(name)}&search.values=${encodeURL(value)}"
|
|
||||||
}.join('&')
|
|
||||||
"${appendIfMissing(buildScan.server, '/')}scans?$query"
|
|
||||||
}
|
|
||||||
|
|
||||||
static String encodeURL(String url) {
|
|
||||||
URLEncoder.encode(url, 'UTF-8')
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isGitInstalled() {
|
|
||||||
try {
|
|
||||||
"git --version".execute().waitFor() == 0
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static String appendIfMissing(String str, String suffix) {
|
|
||||||
str.endsWith(suffix) ? str : str + suffix
|
|
||||||
}
|
|
||||||
|
|
||||||
static String trimAtEnd(String str) {
|
|
||||||
('x' + str).trim().substring(1)
|
|
||||||
}
|
|
||||||
@@ -1935,6 +1935,13 @@
|
|||||||
<sha256 value="7846399b35c7cd642a9b3a000c3e2d62d04eb37a4547b6933cc8b18bcc2f086b" origin="Generated by Gradle"/>
|
<sha256 value="7846399b35c7cd642a9b3a000c3e2d62d04eb37a4547b6933cc8b18bcc2f086b" origin="Generated by Gradle"/>
|
||||||
</artifact>
|
</artifact>
|
||||||
</component>
|
</component>
|
||||||
|
<component group="com.gradle" name="common-custom-user-data-gradle-plugin" version="1.2.1">
|
||||||
|
<artifact name="common-custom-user-data-gradle-plugin-1.2.1.jar">
|
||||||
|
<md5 value="edd34dab71d10ddf2e92c7a1d2662ff3" origin="https://plugins.gradle.org/m2/com/gradle/common-custom-user-data-gradle-plugin/1.2.1/"/>
|
||||||
|
<sha1 value="396261260abae4172e64b94c1e49c68ae71dbd9b" origin="https://plugins.gradle.org/m2/com/gradle/common-custom-user-data-gradle-plugin/1.2.1/"/>
|
||||||
|
<sha256 value="50f51c195188b61a01a5fd809e1bd28407a848a0485e1d0dfb0d09755a8649c6" origin="Generated by Gradle"/>
|
||||||
|
</artifact>
|
||||||
|
</component>
|
||||||
<component group="com.gradle" name="gradle-enterprise-gradle-plugin" version="3.5">
|
<component group="com.gradle" name="gradle-enterprise-gradle-plugin" version="3.5">
|
||||||
<artifact name="gradle-enterprise-gradle-plugin-3.5.jar">
|
<artifact name="gradle-enterprise-gradle-plugin-3.5.jar">
|
||||||
<md5 value="a31898c773630c4b9a77eb6b47e977d4" origin="https://plugins.gradle.org/m2/com/gradle/gradle-enterprise-gradle-plugin/3.5/gradle-enterprise-gradle-plugin-3.5.jar"/>
|
<md5 value="a31898c773630c4b9a77eb6b47e977d4" origin="https://plugins.gradle.org/m2/com/gradle/gradle-enterprise-gradle-plugin/3.5/gradle-enterprise-gradle-plugin-3.5.jar"/>
|
||||||
|
|||||||
@@ -34,11 +34,16 @@ buildscript {
|
|||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id "com.gradle.enterprise" version "3.5"
|
id "com.gradle.enterprise" version "3.5"
|
||||||
|
id "com.gradle.common-custom-user-data-gradle-plugin" version "1.2.1" apply false
|
||||||
}
|
}
|
||||||
|
|
||||||
def buildProperties = BuildPropertiesKt.getKotlinBuildPropertiesForSettings(settings)
|
def buildProperties = BuildPropertiesKt.getKotlinBuildPropertiesForSettings(settings)
|
||||||
def projectVersions = file("./gradle/versions.properties").text
|
def projectVersions = file("./gradle/versions.properties").text
|
||||||
|
|
||||||
|
if (buildProperties.buildScanServer != null) {
|
||||||
|
apply plugin: com.gradle.CommonCustomUserDataGradlePlugin
|
||||||
|
}
|
||||||
|
|
||||||
gradleEnterprise {
|
gradleEnterprise {
|
||||||
def buildScanServer = buildProperties.buildScanServer
|
def buildScanServer = buildProperties.buildScanServer
|
||||||
def isTeamCity = buildProperties.isTeamcityBuild
|
def isTeamCity = buildProperties.isTeamcityBuild
|
||||||
|
|||||||
Reference in New Issue
Block a user