This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
staging.groovy
183 lines (155 loc) · 6.74 KB
/
staging.groovy
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import groovy.json.JsonSlurper
class Staging {
int delayBetweenRetries = 30
int numberOfRetries = 20
String ossUserName
String ossPassword
Staging(String userName, String password) {
ossUserName = userName
ossPassword = password
}
static void main(String[] args) {
def stagingHelper = new Staging(System.getenv("CI_DEPLOY_USERNAME"), System.getenv("CI_DEPLOY_PASSWORD"))
stagingHelper.run(args[0])
}
private void run(String cmd) {
switch (cmd) {
case "close":
println "trying to close nexus repository ..."
doWithRetry(this.&close)
println " > done"
break
case "drop":
println "trying to drop nexus repository ..."
try {
doWithRetry(this.&drop)
} catch (Exception e) {
println "No repository to drop found? " + e
}
println " > done"
break
case "promote":
println "trying to promote nexus repository ..."
doWithRetry(this.&promote)
println " > done"
break
}
}
int doWithRetry(Closure operation) {
int counter = 0
int numberOfAttempts = Integer.valueOf(numberOfRetries)
while (true) {
try {
println "Attempt $counter/$numberOfAttempts..."
if (operation() == 0) {
return 0
}
} catch (Exception e) {
if (counter >= numberOfAttempts) {
println "Giving up."
throw e
} else {
waitBeforeNextAttempt()
}
} finally {
counter++
}
}
}
void waitBeforeNextAttempt() {
sleep(Integer.valueOf(delayBetweenRetries))
}
int drop() {
def stagingProfileId = getStagingProfileId()
def repositoryId = getRepositoryId(stagingProfileId, "released")
if (repositoryId == null) {
println("No more action.")
return 0
}
def data = getData(stagingProfileId, repositoryId)
// drop repository
def response = ['bash', '-c', "curl -sL -w \"%{http_code}\" -H \"Content-Type: application/json\" -X POST -d '" + data + "' https://" + ossUserName + ":" + ossPassword + "@oss.sonatype.org/service/local/staging/profiles/" + stagingProfileId + "/drop -o /dev/null"].execute().text
println response
if (Integer.valueOf(response) > 299) {
throw new IllegalArgumentException("HTTP request failed, getting status code: ${response}")
}
return Integer.valueOf(response)
}
int close() {
def stagingProfileId = getStagingProfileId()
def repositoryId = getRepositoryId(stagingProfileId, "open")
if (repositoryId == null) {
println("No more action.")
return 0
}
def data = getData(stagingProfileId, repositoryId)
// close repository
def response = ['bash', '-c', "curl -sL -w \"%{http_code}\" -H \"Content-Type: application/json\" -X POST -d '" + data + "' https://" + ossUserName + ":" + ossPassword + "@oss.sonatype.org/service/local/staging/profiles/" + stagingProfileId + "/finish -o /dev/null"].execute().text
println response
if (Integer.valueOf(response) > 299) {
throw new IllegalArgumentException("HTTP request failed, getting status code: ${response}")
}
return Integer.valueOf(response)
}
int promote() {
def stagingProfileId = getStagingProfileId()
def repositoryId = getRepositoryId(stagingProfileId, "closed")
if (repositoryId == null) {
println("No more action.")
return 0
}
def data = getData(stagingProfileId, repositoryId)
// promote repository
def response = ['bash', '-c', "curl -sL -w \"%{http_code}\" -H \"Content-Type: application/json\" -X POST -d '" + data + "' https://" + ossUserName + ":" + ossPassword + "@oss.sonatype.org/service/local/staging/profiles/" + stagingProfileId + "/promote -o /dev/null"].execute().text
if (Integer.valueOf(response) > 299) {
throw new IllegalArgumentException("HTTP request failed, getting status code: ${response}")
}
return Integer.valueOf(response)
}
String getStagingProfileId() {
def response = ['bash', '-c', "curl -s -H \"Accept: application/json\" -X GET https://" + ossUserName + ":" + ossPassword + "@oss.sonatype.org/service/local/staging/profiles"].execute().text
def json = new JsonSlurper().parseText(response)
def profileList = json.data
Integer found = 0
String stagingProfileId = ""
for (int index = 0; index < profileList.size(); index++) {
if (profileList[index].name.equals("org.swisspush")) {
found++;
stagingProfileId = profileList[index].id
}
}
if (found == 0) {
throw new IllegalArgumentException("No stagingProfileId found!")
} else if (found > 1) {
throw new IllegalArgumentException("Multiple stagingProfileId's found!")
}
println "Found stagingProfileId: " + stagingProfileId
return stagingProfileId
}
def getRepositoryId(String stagingProfileId, String state) {
def response = ['bash', '-c', "curl -s -H \"Accept: application/json\" -X GET https://" + ossUserName + ":" + ossPassword + "@oss.sonatype.org/service/local/staging/profile_repositories/" + stagingProfileId].execute().text
def json = new JsonSlurper().parseText(response)
println(response)
if (json.data.size() == 0) {
println("No staging repository found!")
return null
} else if (json.data.size() > 1) {
throw new IllegalArgumentException("Multiple staging repositories found!")
}
def repository = json.data[0]
// check state - close
if (state == "open" && repository.type != state) {
println("No open repository found!")
return null
}
if (state == "closed" && repository.type != state) {
println("No closed repository found!")
return null
}
println "Found repositoryId: " + repository.repositoryId
return repository.repositoryId
}
static String getData(String stagingProfileId, String repositoryId) {
return "{\"data\" : {\"stagedRepositoryId\" : " + repositoryId + ",\"description\" : \"Automatically released/promoted with Travis\", \"targetRepositoryId\" : " + stagingProfileId + " }}"
}
}