Skip to content

Commit

Permalink
use a explicit staging repo id to prevent that multiple are created i…
Browse files Browse the repository at this point in the history
…mplicitly
  • Loading branch information
dominik-cnx committed Sep 11, 2018
1 parent 6be6414 commit 5329f90
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
11 changes: 7 additions & 4 deletions maybe-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ then
then
echo 'problem when trying to drop, ignored'
fi
mvn -B -Prelease jgitflow:release-start jgitflow:release-finish --settings settings.xml
echo 'starting a new nexus repository ...'
OUTPUT=$(groovy staging.groovy start)
echo "repository Id: $OUTPUT"
mvn -B -Prelease -PpublicRepos jgitflow:release-start jgitflow:release-finish --settings settings.xml -DrepositoryId=${OUTPUT}
rc=$?
if [ $rc -eq 0 ]
then
groovy staging.groovy close
groovy staging.groovy promote
groovy staging.groovy close ${OUTPUT}
groovy staging.groovy promote ${OUTPUT}
rc=$?
if [ $rc -ne 0 ]
then
echo 'Release failed: cannot promote stage'
echo 'Release failed, cannot promote stage'
exit rc
fi
echo 'Release done, will push'
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>
https://oss.sonatype.org/service/local/staging/deploy/maven2/
https://oss.sonatype.org/service/local/staging/deployByRepositoryId/${repositoryId}
</url>
</repository>
</distributionManagement>
Expand Down
84 changes: 61 additions & 23 deletions staging.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,70 @@ class Staging {
}

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(args[0])
stagingHelper.run(cmdArg, repositoryIdArg)
}

private void run(String cmd) {
private void run(String cmd, String repositoryId) {
switch (cmd) {
case "start":
start()
break
case "close":
println "trying to close nexus repository ..."
doWithRetry(this.&close)
doWithRetry(this.&close, repositoryId)
println " > done"
break
case "drop":
println "trying to drop nexus repository ..."
try {
doWithRetry(this.&drop)
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)
doWithRetry(this.&promote, repositoryId)
println " > done"
break
}
}

int doWithRetry(Closure operation) {
int doWithRetry(Closure operation, String repositoryId) {
int counter = 0
int numberOfAttempts = Integer.valueOf(numberOfRetries)
while (true) {
try {
println "Attempt $counter/$numberOfAttempts..."
if (operation() == 0) {
if (operation.call(repositoryId) == 0) {
return 0
}
} catch (Exception e) {
Expand All @@ -66,9 +95,11 @@ class Staging {
sleep(Integer.valueOf(delayBetweenRetries))
}

int drop() {
def stagingProfileId = getStagingProfileId()
def repositoryId = getRepositoryId(stagingProfileId, "released")
int drop(String repositoryId) {
def stagingProfileId = getStagingProfileId(false)
if (repositoryId.isEmpty()) {
repositoryId = getRepositoryId(stagingProfileId, "released")
}
if (repositoryId == null) {
println("No more action.")
return 0
Expand All @@ -82,13 +113,12 @@ class Staging {
if (Integer.valueOf(response) > 299) {
throw new IllegalArgumentException("HTTP request failed, getting status code: ${response}")
}
return Integer.valueOf(response)
return 0
}


int close() {
def stagingProfileId = getStagingProfileId()
def repositoryId = getRepositoryId(stagingProfileId, "open")
int close(String repositoryId) {
def stagingProfileId = getStagingProfileId(false)
if (repositoryId == null) {
println("No more action.")
return 0
Expand All @@ -102,12 +132,20 @@ class Staging {
if (Integer.valueOf(response) > 299) {
throw new IllegalArgumentException("HTTP request failed, getting status code: ${response}")
}
return Integer.valueOf(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() {
def stagingProfileId = getStagingProfileId()
def repositoryId = getRepositoryId(stagingProfileId, "closed")
int promote(String repositoryId) {
def stagingProfileId = getStagingProfileId(false)
if (repositoryId == null) {
println("No more action.")
return 0
Expand All @@ -119,10 +157,10 @@ class Staging {
if (Integer.valueOf(response) > 299) {
throw new IllegalArgumentException("HTTP request failed, getting status code: ${response}")
}
return Integer.valueOf(response)
return 0
}

String getStagingProfileId() {
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)
Expand All @@ -141,9 +179,9 @@ class Staging {
} else if (found > 1) {
throw new IllegalArgumentException("Multiple stagingProfileId's found!")
}

println "Found stagingProfileId: " + stagingProfileId

if (!slient) {
println "Found stagingProfileId: " + stagingProfileId
}
return stagingProfileId
}

Expand Down

0 comments on commit 5329f90

Please sign in to comment.