-
Notifications
You must be signed in to change notification settings - Fork 4
/
staging.groovy
227 lines (199 loc) · 8.55 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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 cmdArg = ""
def repositoryIdArg = ""
//check arguments
if (args == null || args.length < 1) {
println "Usage: staging [CMD] [repositoryId]"
return
}
cmdArg = args[0]
switch (cmdArg) {
case "start":
case "drop":
break
case "close":
case "promote":
if (args.length != 2) {
println "Invalid argument supplied for: " + args[0]
println "Usage: staging [CMD] [repositoryId]"
return
}
repositoryIdArg = args[1]
break
default:
println "Unknown command: args[0]"
return
}
def stagingHelper = new Staging(System.getenv("CI_DEPLOY_USERNAME"), System.getenv("CI_DEPLOY_PASSWORD"))
stagingHelper.run(cmdArg, repositoryIdArg)
}
private void run(String cmd, String repositoryId) {
switch (cmd) {
case "start":
start()
break
case "close":
println "trying to close nexus repository ..."
//see: https://issues.sonatype.org/browse/OSSRH-27145
println("Waiting 10 secs before continue")
sleep(10000);
doWithRetry(this.&close, repositoryId)
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 ..."
//see: https://issues.sonatype.org/browse/OSSRH-27145
println("Waiting 30 secs before continue")
sleep(30000);
doWithRetry(this.&promote, repositoryId)
println " > done"
break
}
}
int doWithRetry(Closure operation, String repositoryId) {
int counter = 0
int numberOfAttempts = Integer.valueOf(numberOfRetries)
while (true) {
try {
println "Attempt $counter/$numberOfAttempts..."
if (operation.call(repositoryId) == 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(String repositoryId) {
def stagingProfileId = getStagingProfileId(false)
if (repositoryId.isEmpty()) {
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 0
}
int close(String repositoryId) {
def stagingProfileId = getStagingProfileId(false)
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 0
}
int start() {
def stagingProfileId = getStagingProfileId(true)
def stagingRepoDescription = "<promoteRequest><data><description>Auto Release Staging</description></data></promoteRequest>"
def response = ['bash', '-c', "curl -s -H \"Content-Type: application/xml\" -X POST -d '" + stagingRepoDescription + "' https://" + ossUserName + ":" + ossPassword + "@oss.sonatype.org/service/local/staging/profiles/" + stagingProfileId + "/start"].execute().text
def rootNode = new XmlSlurper().parseText(response)
println rootNode.data.stagedRepositoryId
return 0
}
int promote(String repositoryId) {
def stagingProfileId = getStagingProfileId(false)
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 0
}
String getStagingProfileId(boolean slient) {
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!")
}
if (!slient) {
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 + " }}"
}
}