Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 100 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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]+)?/
Expand Down
44 changes: 35 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
}

Expand All @@ -184,4 +191,23 @@ subprojects {
sign publishing.publications.mavenJava
}
}
}
}

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')
}
}
Loading