-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravis-init.kts
85 lines (63 loc) · 2.71 KB
/
travis-init.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
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.nio.file.Files
import java.nio.file.Files.newInputStream
import java.nio.file.Paths
import java.util.*
println("Please, login to travis before running this script")
println("travis login --com")
println("Travis version: ${execute("travis", "version")}")
val userHome = System.getProperty("user.home")
val gradlePropertiesFile = Paths.get(userHome, "/.gradle/gradle.properties")
println("Looking for properties in: $gradlePropertiesFile")
val gradleProperties = Properties().apply { newInputStream(gradlePropertiesFile).use { load(it) } }
val secureItems = listOf("repositoryUrl", "repositoryUser", "repositoryPassword", "signingKeyId", "signingPassword")
println("Found:")
for (item in secureItems) {
val value = gradleProperties[item]
println("$item=$value")
requireNotNull(value){
"Property $item should not be null"
}
}
println("Encrypt properties")
val secure = ArrayList<String>()
for (item in secureItems) {
secure.add(execute("travis", "encrypt", "${item}=${gradleProperties[item]}").trim())
}
println("encrypt secring.gpg")
val secringFile = "${userHome}/.gnupg/secring.gpg"
println("Looking for secring.gpg in: ${secringFile}")
if (!Files.exists(Paths.get(secringFile))) {
println("secring.gpg not found")
System.exit(1)
}
val secringFileEnc = Paths.get("secring.gpg.enc")
if (Files.exists(secringFileEnc)) {
println("${secringFileEnc} already exist. Removing it")
Files.delete(secringFileEnc)
}
val fileEncryptionOutput = execute("travis", "encrypt-file", secringFile)
println("looking for key in output:\n $fileEncryptionOutput")
val key = "encrypted_([^_]+)_key".toRegex().find(fileEncryptionOutput)!!.groupValues[1]
println("#build")
println("before_script: if [[ \$encrypted_${key}_key ]]; then openssl aes-256-cbc -K \$encrypted_${key}_key -iv \$encrypted_${key}_iv -in secring.gpg.enc -out secring.gpg -d; fi")
println("#deploy")
println("before_script: openssl aes-256-cbc -K \$encrypted_${key}_key -iv \$encrypted_${key}_iv -in secring.gpg.enc -out secring.gpg -d")
for (item in secure) {
println("- secure: $item")
}
fun execute(vararg command: String): String {
println(command.joinToString(separator = " "))
val process = ProcessBuilder(*command)
.start()
val output = BufferedReader(InputStreamReader(process.inputStream)).useLines { lines ->
StringBuilder().also { for (line in lines) it.appendln(line) }
}
val errors = BufferedReader(InputStreamReader(process.errorStream)).useLines { lines ->
StringBuilder().also { for (line in lines) it.appendln(line) }
}
System.err.println(errors)
return output.toString()
}