From 3a6274c0827121ddeaa2420baa37657103c731b4 Mon Sep 17 00:00:00 2001 From: "tommaso@teklada.com" Date: Sat, 18 May 2024 11:51:18 +0000 Subject: [PATCH] TeamCity change in 'Spring Petclinic' project: runners of 'Deploy' build configuration were updated --- .teamcity/settings.kts | 95 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/.teamcity/settings.kts b/.teamcity/settings.kts index 96aec3512..cadc5a7bd 100644 --- a/.teamcity/settings.kts +++ b/.teamcity/settings.kts @@ -3,6 +3,7 @@ import jetbrains.buildServer.configs.kotlin.buildFeatures.PullRequests import jetbrains.buildServer.configs.kotlin.buildFeatures.commitStatusPublisher import jetbrains.buildServer.configs.kotlin.buildFeatures.perfmon import jetbrains.buildServer.configs.kotlin.buildFeatures.pullRequests +import jetbrains.buildServer.configs.kotlin.buildSteps.kotlinScript import jetbrains.buildServer.configs.kotlin.buildSteps.maven import jetbrains.buildServer.configs.kotlin.triggers.vcs @@ -34,6 +35,10 @@ project { buildType(Build) buildType(Deploy) + + params { + password("github_key", "credentialsJSON:405d3744-add6-49d5-a271-6dfd185492c2") + } } object Build : BuildType({ @@ -79,4 +84,94 @@ object Build : BuildType({ object Deploy : BuildType({ name = "Deploy" + + steps { + kotlinScript { + id = "kotlinScript" + content = """ + @file:DependsOn("com.squareup.okhttp3:okhttp:4.11.0") + @file:DependsOn("org.json:json:20211205") + + import okhttp3.* + import okhttp3.MediaType.Companion.toMediaType + import org.json.JSONObject + import java.io.File + import java.io.IOException + + + + val client = OkHttpClient() + + fun createRelease(client: OkHttpClient, token: String, owner: String, repo: String, tagName: String, name: String, body: String): Int { + val url = "https://api.github.com/repos/${'$'}owner/${'$'}repo/releases" + val json = JSONObject() + .put("tag_name", tagName) + .put("name", name) + .put("body", body) + + val requestBody = RequestBody.create("application/json; charset=utf-8".toMediaType(), json.toString()) + val request = Request.Builder() + .url(url) + .header("Authorization", "token ${'$'}token") + .post(requestBody) + .build() + + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw IOException("Unexpected code ${'$'}response") + val responseData = response.body?.string() + val jsonResponse = JSONObject(responseData) + return jsonResponse.getInt("id") + } + } + + fun uploadFileToRelease(client: OkHttpClient, token: String, owner: String, repo: String, releaseId: Int, filePath: String) { + val url = "https://uploads.github.com/repos/${'$'}owner/${'$'}repo/releases/${'$'}releaseId/assets?name=${'$'}{File(filePath).name}" + val file = File(filePath) + val requestBody = RequestBody.create(("application/zip").toMediaType(), file) + + val request = Request.Builder() + .url(url) + .header("Authorization", "token ${'$'}token") + .header("Content-Type", "application/zip") + .post(requestBody) + .build() + + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw IOException("Unexpected code ${'$'}response") + println("File uploaded successfully") + } + } + + + fun getArg(args: Array, argName: String): String { + return if (args.contains("-${'$'}argName")) args[1 + args.indexOf("-${'$'}argName")] + else "" + } + + val repoOwner = getArg(args,"repoOwner" ) + val repoName = getArg(args,"repoName" ) + val tagName = getArg(args,"tagName" ) + val releaseName = getArg(args,"releaseName" ) + val releaseDescription = getArg(args,"description" ) + val filePath = getArg(args,"filePath" ) + val githubToken = getArg(args,"token" ) + + val releaseId = createRelease(client, githubToken, repoOwner, repoName, tagName, releaseName, releaseDescription) + //uploadFileToRelease(client, githubToken, repoOwner, repoName, releaseId, filePath) + """.trimIndent() + arguments = """-repoOwner "TPG-Teklada" -repoName "spring-petclinic" -tagName "v1.0.0" -releaseName "Release 1.0.0" -description "Description of the release" -filePath "*.jar" -token "%github_key%"""" + } + } + + dependencies { + dependency(Build) { + snapshot { + onDependencyFailure = FailureAction.FAIL_TO_START + } + + artifacts { + artifactRules = "+:*.jar" + } + } + } })