-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
113 lines (91 loc) · 3.61 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import de.undercouch.gradle.tasks.download.Download
import java.net.HttpURLConnection
import java.net.URL
import java.util.*
plugins {
`maven-publish`
id("de.undercouch.download") version "5.5.0"
}
val mpsGroupId = "com.jetbrains.mps"
val mpsArtifactId = "mps-prerelease"
version = object {
override fun toString(): String {
return getenvRequired("ARTIFACT_VERSION")
}
}
tasks.register("checkAlreadyPublished") {
doLast {
val alreadyPublished = doesArtifactExistInRepository()
println("##teamcity[setParameter name='alreadyPublished' value='$alreadyPublished']")
val status = if (alreadyPublished)
"MPS $version already exists in the repository"
else
"MPS $version will be uploaded to the repository"
println("##teamcity[buildStatus text='$status']")
}
}
val download by tasks.registering(Download::class) {
src(::getArtifactDownloadUrl)
dest(layout.buildDirectory.dir("download"))
overwrite(false)
}
val repackage by tasks.registering(Zip::class) {
val downloadedFile = download.map { it.outputFiles.single() }
from(zipTree(downloadedFile))
archiveFileName = downloadedFile.map { it.name.substringBeforeLast('.') + "-repackaged.zip" }
destinationDirectory = layout.buildDirectory.dir("repackage")
eachFile {
this.path = this.sourcePath.substringAfter('/')
}
includeEmptyDirs = false
}
fun getArtifactDownloadUrl(): String {
val artifactBuildId = getenvRequired("ARTIFACT_BUILD_ID")
return "https://teamcity.jetbrains.com/guestAuth/app/rest/builds/id:${artifactBuildId}/artifacts/content/MPS-${version}.zip"
}
fun doesArtifactExistInRepository(): Boolean {
val url =
URL("${repo.url}/${mpsGroupId.replace('.', '/')}/${mpsArtifactId}/${version}/${mpsArtifactId}-${version}.pom")
logger.info("Checking URL $url")
val connection = url.openConnection() as HttpURLConnection
val credentials = repo.credentials
if (!credentials.username.isNullOrEmpty() && !credentials.password.isNullOrEmpty()) {
logger.info("Using Basic authentication with username ${credentials.username}")
val basicHeader = "Basic " + Base64.getEncoder()
.encodeToString("${credentials.username}:${credentials.password}".toByteArray())
connection.setRequestProperty("Authorization", basicHeader)
}
try {
connection.requestMethod = "HEAD"
connection.doOutput = false
val responseCode = connection.responseCode
logger.info("Received HTTP response code $responseCode")
return when (responseCode) {
200 -> true
404 -> false
else -> throw RuntimeException("Server returned unexpected response code $responseCode for HEAD $url")
}
} finally {
connection.disconnect()
}
}
val prereleasePublication = publishing.publications.create<MavenPublication>("mpsPrerelease") {
groupId = mpsGroupId
artifactId = mpsArtifactId
artifact(repackage)
}
val repo = publishing.repositories.maven("https://artifacts.itemis.cloud/repository/maven-mps-prereleases") {
if (project.findProperty("artifacts.itemis.cloud.user") != null) {
credentials {
username = project.findProperty("artifacts.itemis.cloud.user") as String?
password = project.findProperty("artifacts.itemis.cloud.pw") as String?
}
}
}
fun getenvRequired(name: String) =
System.getenv(name) ?: throw GradleException("Environment variable '$name' must be set")
tasks.publish {
doLast {
println("##teamcity[buildStatus text='MPS $version successfully published']")
}
}