Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Publish

on:
release:
types: [ published ]

permissions:
contents: read

jobs:
publish:
runs-on: ubuntu-latest
environment: maven-central
timeout-minutes: 75

steps:
- uses: actions/checkout@v4

- 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
Comment thread
augustak marked this conversation as resolved.
Resolved
echo "Release tag $GITHUB_REF_NAME should be v$version, the version in gradle.properties"
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
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,21 @@ $ 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`.
Comment thread
augustak marked this conversation as resolved.
Resolved

The workflow runs in the `maven-central` environment, which holds these secrets:

* 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)
* `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
Comment thread
augustak marked this conversation as resolved.
Resolved
* `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

### Usage
To publish from your own machine instead, set the same four values as environment variables, with
Comment thread
augustak marked this conversation as resolved.
Resolved
`ORG_GRADLE_PROJECT_signingKey` and `ORG_GRADLE_PROJECT_signingPassword` for the key, and run:

```sh
$ make publish
```

`make bundle` builds and signs the bundle in build/central-bundle.zip without uploading it.
64 changes: 30 additions & 34 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id 'maven'
id 'maven-publish'
id 'signing'
}

Expand All @@ -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'
Expand Down Expand Up @@ -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
}
36 changes: 36 additions & 0 deletions upload-to-central.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/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 <bundle.zip> <deployment name>
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')"
Comment thread
augustak marked this conversation as resolved.
Resolved

deployment=$(curl --fail-with-body -sS -H "$auth" -F "bundle=@$bundle" \
"$api/upload?name=$name&publishingType=AUTOMATIC")
echo "Uploaded $bundle as deployment $deployment"

# Validation takes minutes, and publishing to Maven Central after it can take half an hour
for _ in $(seq 240); do
status=$(curl --fail-with-body -sS -X POST -H "$auth" "$api/status?id=$deployment")
Comment thread
augustak marked this conversation as resolved.
Resolved
Comment thread
augustak marked this conversation as resolved.
Resolved
state=$(printf '%s' "$status" | python3 -c 'import json, sys; print(json.load(sys.stdin)["deploymentState"])')
case $state in
PUBLISHED)
echo "Published"
exit 0
;;
FAILED)
echo "Deployment failed: $status"
exit 1
;;
esac
echo "Deployment is $state"
sleep 15
done

echo "Deployment $deployment was not published within an hour, see https://central.sonatype.com/publishing/deployments"
exit 1
Loading