From 5329f902cca2928ac6bb0b4bc12e1e69d5766e2d Mon Sep 17 00:00:00 2001 From: Dominik Schraner Date: Tue, 11 Sep 2018 14:34:46 +0700 Subject: [PATCH] use a explicit staging repo id to prevent that multiple are created implicitly --- maybe-release.sh | 11 ++++--- pom.xml | 2 +- staging.groovy | 84 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 69 insertions(+), 28 deletions(-) diff --git a/maybe-release.sh b/maybe-release.sh index b3b0ff38..b81a3f88 100644 --- a/maybe-release.sh +++ b/maybe-release.sh @@ -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' diff --git a/pom.xml b/pom.xml index 6f1aabf8..345fbae9 100644 --- a/pom.xml +++ b/pom.xml @@ -246,7 +246,7 @@ sonatype-nexus-staging Nexus Release Repository - https://oss.sonatype.org/service/local/staging/deploy/maven2/ + https://oss.sonatype.org/service/local/staging/deployByRepositoryId/${repositoryId} diff --git a/staging.groovy b/staging.groovy index ee0bf671..042462d3 100644 --- a/staging.groovy +++ b/staging.groovy @@ -12,21 +12,50 @@ 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 } @@ -34,19 +63,19 @@ class Staging { 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) { @@ -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 @@ -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 @@ -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 = "Auto Release Staging" + 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 @@ -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) @@ -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 }