From 6c99b6fc2b9c1beffabee01edb488efa15fbfb62 Mon Sep 17 00:00:00 2001 From: Shuya Ma Date: Fri, 19 Apr 2024 11:20:22 -0700 Subject: [PATCH] copy teamcity debug logs to GCS --- .../builds/build_configuration_per_package.kt | 1 + .../components/builds/build_steps.kt | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/mmv1/third_party/terraform/.teamcity/components/builds/build_configuration_per_package.kt b/mmv1/third_party/terraform/.teamcity/components/builds/build_configuration_per_package.kt index f9be546f7eec..cfa5af5246f4 100644 --- a/mmv1/third_party/terraform/.teamcity/components/builds/build_configuration_per_package.kt +++ b/mmv1/third_party/terraform/.teamcity/components/builds/build_configuration_per_package.kt @@ -71,6 +71,7 @@ class PackageDetails(private val packageName: String, private val displayName: S configureGoEnv() downloadTerraformBinary() runAcceptanceTests() + saveArtifactsToGCS() } features { diff --git a/mmv1/third_party/terraform/.teamcity/components/builds/build_steps.kt b/mmv1/third_party/terraform/.teamcity/components/builds/build_steps.kt index 7286b2b82f7c..4158ab9e66f9 100644 --- a/mmv1/third_party/terraform/.teamcity/components/builds/build_steps.kt +++ b/mmv1/third_party/terraform/.teamcity/components/builds/build_steps.kt @@ -131,4 +131,44 @@ fun BuildSteps.runAcceptanceTests() { """.trimIndent() }) } +} + +fun BuildSteps.saveArtifactsToGCS() { + step(ScriptBuildStep { + name = "Tasks after running nightly tests: push artifacts(debug logs) to GCS" + scriptContent = """ + #!/bin/bash + echo "Post-test step - storge artifacts(debug logs) to GCS" + + # Authenticate gcloud CLI + echo "${'$'}{GOOGLE_CREDENTIALS}" > google-account.json + chmod 600 google-account.json + gcloud auth activate-service-account --key-file=google-account.json + + # Detect Trigger Method + TRIGGERED_BY_USERNAME=%teamcity.build.triggeredBy.username% + BRANCH_NAME=%teamcity.build.branch% + if [[ "${'$'}TRIGGERED_BY_USERNAME" = "n/a" ]] ; then + echo "Build was triggered as part of automated testing. We know this because the `triggeredBy.username` value was `n/a`, value: ${'$'}{TRIGGERED_BY_USERNAME}" + FOLDER="nightly" + else + echo "Build was triggered manually. We know this because `triggeredBy.username` has a non- `n/a` value: ${'$'}{TRIGGERED_BY_USERNAME}" + FOLDER="manual/${'$'}BRANCH_NAME" + fi + + + # Copy logs to GCS + gsutil -m cp %teamcity.build.checkoutDir%/debug* gs://teamcity-nightly-logs/%PROVIDER_NAME%/${'$'}{FOLDER}/%env.BUILD_NUMBER%/ + + # Cleanup + rm google-account.json + gcloud auth application-default revoke + gcloud auth revoke --all + + echo "Finished" + """.trimIndent() + // ${'$'} is required to allow creating a script in TeamCity that contains + // parts like ${GIT_HASH_SHORT} without having Kotlin syntax issues. For more info see: + // https://youtrack.jetbrains.com/issue/KT-2425/Provide-a-way-for-escaping-the-dollar-sign-symbol-in-multiline-strings-and-string-templates + }) } \ No newline at end of file