From c60c795dd4fe2526a3b9ae431426f6ad7447076d Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:56:10 +0000 Subject: [PATCH] ci: publish to Maven Central through the Central Portal oss.sonatype.org no longer accepts uploads. Releases are staged locally, zipped by the centralBundle task and uploaded through the Central Publisher API; snapshots go to the Central Portal snapshot repository. Credentials and the signing key come from the 'Maven Central' context. Co-Authored-By: jason.han --- .circleci/config.yml | 104 +++++++++++++++++++++++++++++++++++++++++-- build.gradle | 44 ++++++++++++++---- 2 files changed, 135 insertions(+), 13 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1d56c0c84..13f8e41c8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,6 +10,37 @@ executors: _JAVA_OPTIONS: "-Xmx3g" GRADLE_OPTS: "-Dorg.gradle.daemon=false" +commands: + require_publishing_credentials: + steps: + - run: + name: Require the publishing credentials + command: | + # Names only, never values. + for name in CENTRAL_TOKEN_USERNAME CENTRAL_TOKEN_PASSWORD GPG_PRIVATE_KEY GPG_PASSPHRASE; do + if [ -z "${!name:-}" ]; then + echo "Error: ${name} is not set; the 'Maven Central' context supplies it." + exit 1 + fi + done + export_signing_key: + steps: + - run: + name: Hand the signing key to Gradle + command: | + # The context may hold the key ASCII-armoured or base64-encoded on one line; Gradle needs it armoured. + umask 077 + case "$GPG_PRIVATE_KEY" in + *"-----BEGIN PGP PRIVATE KEY BLOCK-----"*) printf '%s\n' "$GPG_PRIVATE_KEY" > /tmp/signing-key.asc ;; + *) printf '%s' "$GPG_PRIVATE_KEY" | tr -d ' \n' | base64 -d > /tmp/signing-key.asc ;; + esac + if ! grep -q -- '-----BEGIN PGP PRIVATE KEY BLOCK-----' /tmp/signing-key.asc; then + echo "Error: GPG_PRIVATE_KEY is neither an armoured private key nor its base64 encoding." + exit 1 + fi + echo 'export ORG_GRADLE_PROJECT_signingKey="$(cat /tmp/signing-key.asc)"' >> "$BASH_ENV" + echo 'export ORG_GRADLE_PROJECT_signingPassword="$GPG_PASSPHRASE"' >> "$BASH_ENV" + jobs: build_and_test: executor: @@ -88,24 +119,85 @@ jobs: - sonarcloud/scan + # Publishing uses the organization's 'Maven Central' context: a Central Portal user token + # (CENTRAL_TOKEN_USERNAME, CENTRAL_TOKEN_PASSWORD) and the release signing key + # (GPG_PRIVATE_KEY, GPG_PASSPHRASE). deploy_snapshot: executor: openjdk_executor working_directory: /home/circleci/mms steps: - attach_workspace: at: ~/ + - require_publishing_credentials + - export_signing_key - run: - name: Deploy snapshot to sonatype - command: ./gradlew -PossrhUsername=$OSSRH_USERNAME -PossrhPassword=$OSSRH_PASSWORD -PsigningKey="$SIGNING_KEY" -PsigningPassword=$SIGNING_PASSWORD publish + name: Deploy snapshot to the Central Portal snapshot repository + command: | + export ORG_GRADLE_PROJECT_centralUsername="$CENTRAL_TOKEN_USERNAME" + export ORG_GRADLE_PROJECT_centralPassword="$CENTRAL_TOKEN_PASSWORD" + ./gradlew publish + + # A Maven Central version can never be replaced, so everything that can fail runs before the + # upload: the credentials, the version check, and building and signing every module. deploy_release: executor: openjdk_executor working_directory: /home/circleci/mms steps: - attach_workspace: at: ~/ + - require_publishing_credentials + - run: + name: Refuse a version already on Maven Central + command: | + code=$(curl -s -o /dev/null -w '%{http_code}' "https://repo1.maven.org/maven2/org/openmbee/mms/core/${CIRCLE_TAG}/") + case "$code" in + 404) echo "org.openmbee.mms ${CIRCLE_TAG} is not on Maven Central yet" ;; + 200) echo "Error: org.openmbee.mms ${CIRCLE_TAG} is already on Maven Central and cannot be replaced."; exit 1 ;; + *) echo "Error: could not ask Maven Central (HTTP $code); nothing was published."; exit 1 ;; + esac + - export_signing_key + - run: + name: Build and sign the deployment bundle + command: | + ./gradlew -PbuildNumber=$CIRCLE_BUILD_NUM -PbuildTag=$CIRCLE_TAG --stacktrace cleanCentralStaging centralBundle + bundle=build/central/central-bundle.zip + poms=$(unzip -Z1 "$bundle" | grep -c '\.pom$') + signed=$(unzip -Z1 "$bundle" | grep -c '\.pom\.asc$') + echo "$poms modules, $signed signed" + if [ "$poms" -eq 0 ] || [ "$poms" -ne "$signed" ]; then + echo "Error: the bundle is empty or not every module is signed; nothing was published." + exit 1 + fi - run: - name: Deploy release to sonatype - command: ./gradlew -PbuildNumber=$CIRCLE_BUILD_NUM -PbuildTag=$CIRCLE_TAG -PossrhUsername=$OSSRH_USERNAME -PossrhPassword=$OSSRH_PASSWORD -PsigningKey="$SIGNING_KEY" -PsigningPassword=$SIGNING_PASSWORD --info --stacktrace publish + name: Upload the bundle and wait until Maven Central publishes it + no_output_timeout: 35m + command: | + # The token travels in a curl config file, never on a command line. + auth=$(mktemp) + printf 'header = "Authorization: Bearer %s"\n' "$(printf '%s:%s' "$CENTRAL_TOKEN_USERNAME" "$CENTRAL_TOKEN_PASSWORD" | base64 -w0)" > "$auth" + api=https://central.sonatype.com/api/v1/publisher + code=$(curl -sS -K "$auth" -o /tmp/deployment-id -w '%{http_code}' \ + --form bundle=@build/central/central-bundle.zip \ + "$api/upload?publishingType=AUTOMATIC&name=mms-${CIRCLE_TAG}") + if [ "$code" != 201 ]; then + echo "Error: the Central Portal refused the upload (HTTP $code)." + cat /tmp/deployment-id + exit 1 + fi + id=$(cat /tmp/deployment-id) + echo "Deployment $id uploaded" + for attempt in $(seq 1 180); do + status=$(curl -sS -K "$auth" -X POST "$api/status?id=$id" || true) + state=$(printf '%s' "$status" | jq -r '.deploymentState // empty' 2>/dev/null || true) + echo "$(date -u +%H:%M:%S) ${state:-no answer}" + case "$state" in + PUBLISHED) exit 0 ;; + FAILED) printf '%s\n' "$status"; exit 1 ;; + esac + sleep 10 + done + echo "Error: deployment $id was not published within 30 minutes; check it at https://central.sonatype.com/publishing/deployments." + exit 1 workflows: version: 2 @@ -126,12 +218,16 @@ workflows: - deploy_snapshot: requires: - build_and_test + context: + - Maven Central filters: branches: only: /((release|hotfix|support)/[0-9.]+(-(a|b|rc)[0-9]+)?|develop)/ - deploy_release: requires: - build_and_test + context: + - Maven Central filters: tags: only: /[0-9.]+(-(a|b|rc)[0-9]+)?/ diff --git a/build.gradle b/build.gradle index ba813c710..a18ffbc69 100644 --- a/build.gradle +++ b/build.gradle @@ -156,16 +156,23 @@ subprojects { } repositories { - maven { - def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" - def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/" - url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl - if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) { - credentials { - username = ossrhUsername - password = ossrhPassword + if (version.endsWith('SNAPSHOT')) { + maven { + name = 'centralSnapshots' + url = 'https://central.sonatype.com/repository/maven-snapshots/' + if (project.hasProperty('centralUsername') && project.hasProperty('centralPassword')) { + credentials { + username = centralUsername + password = centralPassword + } } } + } else { + // Maven Central takes a release as one bundle, so releases are staged here and zipped by centralBundle. + maven { + name = 'centralStaging' + url = rootProject.layout.buildDirectory.dir('central-staging') + } } } @@ -184,4 +191,23 @@ subprojects { sign publishing.publications.mavenJava } } -} \ No newline at end of file +} + +tasks.register('cleanCentralStaging', Delete) { + delete layout.buildDirectory.dir('central-staging') +} + +// The deployment bundle the Central Publisher API accepts: the staged Maven layout of every module. +tasks.register('centralBundle', Zip) { + dependsOn subprojects.collect { "${it.path}:publish" } + from layout.buildDirectory.dir('central-staging') + exclude '**/maven-metadata.xml*' + archiveFileName = 'central-bundle.zip' + destinationDirectory = layout.buildDirectory.dir('central') +} + +subprojects { + tasks.withType(PublishToMavenRepository).configureEach { + mustRunAfter rootProject.tasks.named('cleanCentralStaging') + } +}