diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..8d50dd9 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,49 @@ +name: Publish + +on: + release: + types: [ published ] + +permissions: + contents: read + +jobs: + publish: + runs-on: ubuntu-latest + environment: maven-central + timeout-minutes: 75 + + steps: + # With all history, to check that the release is of a commit on master + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 8 + + - name: Check that the release tag is the version + run: | + version=$(sed -n 's/^version=//p' gradle.properties) + if [ "$GITHUB_REF_NAME" != "v$version" ]; then + echo "Release tag $GITHUB_REF_NAME should be v$version, the version in gradle.properties" + exit 1 + fi + + # Only reviewed code that passed CI is released + - name: Check that the release is of a commit on master + run: | + if ! git merge-base --is-ancestor HEAD origin/master; then + echo "Release tag $GITHUB_REF_NAME is on commit $(git rev-parse HEAD), which isn't on master" + exit 1 + fi + + - name: Publish to Maven Central + env: + ORG_GRADLE_PROJECT_signingKey: ${{ secrets.SIGNING_KEY }} + ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.SIGNING_PASSWORD }} + CENTRAL_USERNAME: ${{ secrets.CENTRAL_USERNAME }} + CENTRAL_PASSWORD: ${{ secrets.CENTRAL_PASSWORD }} + run: make publish diff --git a/Makefile b/Makefile index a1798e1..50cf059 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,19 @@ .PHONY: * +VERSION := $(shell sed -n 's/^version=//p' gradle.properties) + build: ./gradlew build test: ./gradlew --info test -publish: - ./gradlew clean test - ./gradlew clean uploadArchives +bundle: + ./gradlew clean publish + cd build/staging-deploy && zip -qr ../central-bundle.zip ai/lucidtech/las-sdk-java/$(VERSION) + +publish: bundle + ./upload-to-central.sh build/central-bundle.zip las-sdk-java-$(VERSION) java-docs: ./gradlew clean javadocJar diff --git a/README.md b/README.md index 06a74c9..7f0a947 100644 --- a/README.md +++ b/README.md @@ -81,17 +81,28 @@ $ pmd -d . -R ruleset.xml ## Publishing -### Prerequisites +Releases are published to Maven Central by the [Publish workflow](.github/workflows/publish.yml) when a GitHub +release is published. The release's tag must be the version in gradle.properties prefixed with `v`, such as `v4.0.1`. + +The workflow runs in the `maven-central` environment, which holds these secrets: + +* `CENTRAL_USERNAME` and `CENTRAL_PASSWORD`: a user token from [the Central Portal](https://central.sonatype.com/account) + for an account with access to the `ai.lucidtech` namespace +* `SIGNING_KEY` and `SIGNING_PASSWORD`: the ASCII-armored private PGP key that signs the release, and its passphrase. + Its public key must be on a public keyserver, such as keyserver.ubuntu.com -* Gradle [help](https://docs.gradle.org/current/userguide/userguide.html) -* PGP key [help](https://central.sonatype.org/pages/working-with-pgp-signatures.html) -* gradle.properties [help](https://central.sonatype.org/pages/gradle.html) -* GRADLE_USER_HOME defined to point at directory containing above file -* Adjust version in gradle.properties in project root -* Use suffix "-SNAPSHOT" in version to test publish before publishing to production repo. (0.0.1-SNAPSHOT etc) +If the workflow fails, first look for the deployment in [the Portal](https://central.sonatype.com/publishing/deployments), +as one it accepted may still be published. If the version wasn't published, delete the GitHub release and its tag, +merge the fix, and publish the release again. Until the tag is deleted, the version check treats the version as +released, and re-running the workflow builds the tagged commit without the fix. -### Usage +To publish from your own machine instead, set the same four values as environment variables, with +`ORG_GRADLE_PROJECT_signingKey` and `ORG_GRADLE_PROJECT_signingPassword` for the key, and run: ```sh $ make publish ``` + +Then push the tag `v` on the commit you published, so that the version check treats the version as released. + +`make bundle` builds and signs the bundle in build/central-bundle.zip without uploading it. diff --git a/build.gradle b/build.gradle index 2c023a1..19848f9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'maven' + id 'maven-publish' id 'signing' } @@ -27,42 +27,19 @@ test { } } -task javadocJar(type: Jar) { - classifier = 'javadoc' - from javadoc +java { + withJavadocJar() + withSourcesJar() } -task sourcesJar(type: Jar) { - classifier = 'sources' - from sourceSets.main.allSource -} - -artifacts { - archives javadocJar, sourcesJar -} - -signing { - required { !version.endsWith('SNAPSHOT') && gradle.taskGraph.hasTask('uploadArchives') } - sign configurations.archives -} +publishing { + publications { + mavenJava(MavenPublication) { + artifactId = archivesBaseName + from components.java -uploadArchives { - repositories { - mavenDeployer { - beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - - if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) { - repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') { - authentication(userName: ossrhUsername, password: ossrhPassword) - } - - snapshotRepository(url: 'https://oss.sonatype.org/content/repositories/snapshots/') { - authentication(userName: ossrhUsername, password: ossrhPassword) - } - } - - pom.project { - name = "${project.group}:${project.name}" + pom { + name = "${project.group}:${archivesBaseName}" packaging = 'jar' description = 'Java SDK for Lucidtech AI Services' url = 'https://lucidtech.ai' @@ -95,4 +72,23 @@ uploadArchives { } } } + + // Maven Central takes a release as one bundle of signed files in the Maven repository layout, built here + // and uploaded by upload-to-central.sh + repositories { + maven { + name = 'staging' + url = "$buildDir/staging-deploy" + } + } +} + +// The key is passed as the signingKey and signingPassword properties (ORG_GRADLE_PROJECT_signingKey and +// ORG_GRADLE_PROJECT_signingPassword in CI), or else read from the signing.* properties in ~/.gradle/gradle.properties +signing { + def signingKey = findProperty('signingKey') + if (signingKey) { + useInMemoryPgpKeys(signingKey, findProperty('signingPassword')) + } + sign publishing.publications.mavenJava } diff --git a/upload-to-central.sh b/upload-to-central.sh new file mode 100755 index 0000000..dd33195 --- /dev/null +++ b/upload-to-central.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# Uploads a bundle to Maven Central through the Central Portal and waits until it is published. +# CENTRAL_USERNAME and CENTRAL_PASSWORD are a user token from https://central.sonatype.com/account +# +# Usage: upload-to-central.sh +set -euo pipefail + +bundle=$1 +name=$2 +api=https://central.sonatype.com/api/v1/publisher +auth="Authorization: Bearer $(printf '%s:%s' "$CENTRAL_USERNAME" "$CENTRAL_PASSWORD" | base64 | tr -d '\n')" + +# curl reads the token's header from a file descriptor, which keeps it out of the process list +portal() { + curl -sS --fail-with-body --connect-timeout 30 -H @<(printf '%s\n' "$auth") "$@" +} + +# Not retried, as a retried upload could create a second deployment of the same version +if ! deployment=$(portal --max-time 600 -F "bundle=@$bundle" "$api/upload?name=$name&publishingType=AUTOMATIC"); then + echo "Upload failed: $deployment" + exit 1 +fi +echo "Uploaded $bundle as deployment $deployment" + +# Validation takes minutes, and publishing to Maven Central after it can take half an hour. The Portal goes on with +# the deployment whatever happens here, and rejects uploading the same version again, so a failed status check is +# retried until the deadline instead of ending the script +deadline=$((SECONDS + 3600)) +while [ "$SECONDS" -lt "$deadline" ]; do + if status=$(portal --max-time 60 -X POST "$api/status?id=$deployment") && + state=$(printf '%s' "$status" | python3 -c 'import json, sys; print(json.load(sys.stdin)["deploymentState"])' 2>/dev/null); then + case $state in + PUBLISHED) + echo "Published" + exit 0 + ;; + FAILED) + echo "Deployment failed: $status" + exit 1 + ;; + esac + echo "Deployment is $state" + else + echo "Could not get the deployment's status, retrying: $status" + fi + sleep 15 +done + +echo "Deployment $deployment was not published within an hour, see https://central.sonatype.com/publishing/deployments" +exit 1