diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..8d0d42b7 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,305 @@ +# One-click release of the Android API: GitHub Packages + a GitHub Release. +# +# How to release +# Actions -> "Release Android API" -> Run workflow (on master). Both boxes can stay blank: +# - version blank = next patch after the last Android release +# (AndroidAPI_v3.2.9Beta -> 3.2.10_beta), or type one +# - driver_version the Shimmer-Java-Android-API release (shimmerdriver and +# shimmerbluetoothmanager) to build against. Blank = the newest API_v release +# +# Neither version is committed, so master's protection does not get in the way. The library's +# build files read -PreleaseVersion and -PdriverVersion, which this workflow passes. Without them a +# local build is versioned 'local' and uses the default driver in ShimmerAndroidInstrumentDriver/ +# build.gradle. The AndroidAPI_v tag plus the driver version named in its release notes rebuild the +# same .aar. +# +# One run does, in order, and creates nothing public until the library has built: +# build picks both versions; fails if this version is already tagged (in this or the older +# v3.2.xBeta form) or the driver version has no API_v release; builds the release .aar +# and every example app against that driver. Read-only (it reads the driver package): +# it runs on PRs too. +# publish real releases only, and the only job with packages:write. Builds the .aar the same way, +# publishes it to GitHub Packages, and keeps the .aar it published for the release +# release tags AndroidAPI_v on the commit it built and creates the GitHub Release, with +# the driver version, notes since the previous Android API release, and the .aar +# +# If only the release job fails, "Re-run failed jobs" re-runs just that job. +# +# The driver is read from Shimmer-Java-Android-API's GitHub Packages with this repo's GITHUB_TOKEN. +# If that is ever refused (401/403 resolving com.shimmerresearch:shimmerdriver), add a classic token +# with read:packages as the PACKAGES_READ_TOKEN secret and it is used instead. +# +# dry_run builds without publishing, from any branch. The workflow also dry-runs on any PR that +# changes this file, building against master's default driver. + +name: Release Android API + +on: + workflow_dispatch: + inputs: + version: + description: 'Version, e.g. 3.3.0_beta. Blank = next patch after the last release' + type: string + required: false + driver_version: + description: 'Java API (driver) version to build against, e.g. 0.11.9_beta. Blank = newest release' + type: string + required: false + dry_run: + description: 'Build only; publish nothing' + type: boolean + default: false + pull_request: + paths: + - '.github/workflows/release.yml' + +concurrency: + group: release-android-api + cancel-in-progress: false + +defaults: + run: + working-directory: ShimmerAndroidInstrumentDriver + +jobs: + build: + runs-on: ubuntu-latest + timeout-minutes: 30 # a hung Gradle daemon otherwise holds the runner for 6 hours + # Read-only on purpose: this job also runs on PRs, and any code in a job can recover that + # job's token. packages:read is for resolving the driver; only the publish job, which runs + # on real releases alone, can write packages. + permissions: + contents: read + packages: read + env: + DRY_RUN: ${{ github.event_name != 'workflow_dispatch' || inputs.dry_run }} + # Read by the repositories in the root build.gradle to resolve the driver. + USERNAME: ${{ github.actor }} + TOKEN: ${{ secrets.PACKAGES_READ_TOKEN || github.token }} + outputs: + version: ${{ steps.version.outputs.version }} + tag: ${{ steps.version.outputs.tag }} + previous_tag: ${{ steps.version.outputs.previous_tag }} + driver: ${{ steps.version.outputs.driver }} + steps: + - name: Release from master only + if: env.DRY_RUN != 'true' && github.ref != 'refs/heads/master' + working-directory: . + run: | + echo "::error::A release must run on master, not ${GITHUB_REF_NAME}. Tick dry_run to try another branch." + exit 1 + + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + fetch-depth: 0 # tags, to refuse a version that is already released + # Keep the token out of .git/config, where the Gradle build (and any plugin or dependency + # it runs) could read it. Nothing here needs git credentials after checkout; gh uses GH_TOKEN. + persist-credentials: false + + - name: Choose the version and the driver + id: version + env: + GH_TOKEN: ${{ github.token }} + REQUESTED_VERSION: ${{ inputs.version }} + REQUESTED_DRIVER: ${{ inputs.driver_version }} + run: | + # Earlier releases were tagged v3.2.xBeta, so match those too. + PREVIOUS_TAG=$(gh release list --repo "$GITHUB_REPOSITORY" --limit 200 --json tagName,createdAt \ + --jq '[.[] | select(.tagName | test("^(AndroidAPI_)?v[0-9]"))] | sort_by(.createdAt) | last | .tagName // ""') + if [ -n "$REQUESTED_VERSION" ]; then + VERSION=$REQUESTED_VERSION + else + # Next patch after the last release, keeping its suffix. The older 3.2.9Beta spelling + # counts as 3.2.9_beta, so AndroidAPI_v3.2.9Beta -> 3.2.10_beta. + last=${PREVIOUS_TAG#AndroidAPI_v}; last=${last#v}; last=${last/%Beta/_beta} + if ! [[ "$last" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(.*)$ ]]; then + echo "::error::Cannot work out the next version from '${PREVIOUS_TAG:-no previous release}'. Type one in the version box." + exit 1 + fi + VERSION="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.$((BASH_REMATCH[3] + 1))${BASH_REMATCH[4]}" + fi + if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+[A-Za-z0-9_.-]*$ ]]; then + echo "::error::'$VERSION' is not a version like 3.2.10_beta." + exit 1 + fi + TAG="AndroidAPI_v$VERSION" + # Check the older spellings too, or a version could be released twice under two tags. + legacy=${VERSION/_beta/Beta} + for t in "$TAG" "AndroidAPI_v$legacy" "v$legacy"; do + if git rev-parse -q --verify "refs/tags/$t" >/dev/null; then + if [ "$DRY_RUN" = true ]; then + echo "::warning::$VERSION is already released as $t, so a real release would stop here. Dry run continues." + break + fi + echo "::error::$VERSION is already released as $t. Leave the version blank for the next one, or type another." + exit 1 + fi + done + + # Driver: typed, else the newest Java API release. A PR dry run uses master's default. + default_driver=$(sed -nE "s/.*findProperty\('driverVersion'\) \?: '([^']+)'.*/\1/p" build.gradle) + newest=$(gh release list --repo "$GITHUB_REPOSITORY_OWNER/Shimmer-Java-Android-API" --limit 200 --json tagName,createdAt \ + --jq '[.[] | select(.tagName | startswith("API_v"))] | sort_by(.createdAt) | last | .tagName // "" | ltrimstr("API_v")') + if [ -n "$REQUESTED_DRIVER" ]; then + DRIVER=$REQUESTED_DRIVER + elif [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then + DRIVER=$newest + else + DRIVER=$default_driver + fi + if [ -z "$DRIVER" ]; then + echo "::error::No driver version: none typed, and none found. Type one in the driver_version box." + exit 1 + fi + if ! gh release view "API_v$DRIVER" --repo "$GITHUB_REPOSITORY_OWNER/Shimmer-Java-Android-API" >/dev/null 2>&1; then + echo "::error::Shimmer-Java-Android-API has no release API_v$DRIVER. Pick a released driver version." + exit 1 + fi + if [ -n "$newest" ] && [ "$DRIVER" != "$newest" ]; then + echo "::warning::Building against driver $DRIVER, but the newest Java API release is $newest." + fi + + { + echo "version=$VERSION" + echo "tag=$TAG" + echo "previous_tag=$PREVIOUS_TAG" + echo "driver=$DRIVER" + } >> "$GITHUB_OUTPUT" + echo "Releasing $VERSION as $TAG against driver $DRIVER (master's default: ${default_driver:-none}; notes since ${PREVIOUS_TAG:-the first commit})" + + # AGP 8 needs JDK 17. + - name: Set up JDK 17 + uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4.9.1 + with: + java-version: '17' + distribution: 'temurin' + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@ec92e829475ac0c2315ea8f9eced72db85bb337a # v3.0.0 + + # gradle.properties is gitignored here, so android.useAndroidX (needed for the androidx + # dependencies) and the daemon heap are passed on the command line. The default 512 MB heap + # runs out dexing the examples (see ci.yml); both steps pass the same value so they share + # one daemon. + - name: Build the release .aar + env: + VERSION: ${{ steps.version.outputs.version }} + DRIVER: ${{ steps.version.outputs.driver }} + run: | + ./gradlew :ShimmerAndroidInstrumentDriver:assembleRelease -Pandroid.useAndroidX=true \ + -PreleaseVersion="$VERSION" -PdriverVersion="$DRIVER" \ + -Dorg.gradle.jvmargs="-Xmx4g -XX:MaxMetaspaceSize=1g" + + # ci.yml builds the examples against master's default driver. A release can pick another, + # so build every example against the one this release ships with, before anything is + # published. --continue reports every failing module, and the step still fails. + - name: Build every example against the chosen driver + env: + DRIVER: ${{ steps.version.outputs.driver }} + run: | + ./gradlew assembleDebug --continue -Pandroid.useAndroidX=true -PdriverVersion="$DRIVER" \ + -Dorg.gradle.jvmargs="-Xmx4g -XX:MaxMetaspaceSize=1g" + + publish: + needs: build + if: github.event_name == 'workflow_dispatch' && !inputs.dry_run + runs-on: ubuntu-latest + timeout-minutes: 30 # a hung Gradle daemon otherwise holds the runner for 6 hours + permissions: + contents: read + packages: write # also covers reading the driver package + env: + VERSION: ${{ needs.build.outputs.version }} + DRIVER: ${{ needs.build.outputs.driver }} + # Read by the repositories in the root build.gradle to resolve the driver. + USERNAME: ${{ github.actor }} + TOKEN: ${{ secrets.PACKAGES_READ_TOKEN || github.token }} + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false # publish gets its token explicitly, below + + # AGP 8 needs JDK 17. + - name: Set up JDK 17 + uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4.9.1 + with: + java-version: '17' + distribution: 'temurin' + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@ec92e829475ac0c2315ea8f9eced72db85bb337a # v3.0.0 + + # Run assembleRelease first: the publication's artifact is the built .aar file, and publish + # does not build it itself. + - name: Build the release .aar + run: | + ./gradlew :ShimmerAndroidInstrumentDriver:assembleRelease -Pandroid.useAndroidX=true \ + -PreleaseVersion="$VERSION" -PdriverVersion="$DRIVER" + + # The library's publishing block reads GITHUB_ACTOR and GITHUB_TOKEN. + - name: Publish to GitHub Packages + env: + GITHUB_TOKEN: ${{ github.token }} + run: | + ./gradlew :ShimmerAndroidInstrumentDriver:publish -Pandroid.useAndroidX=true \ + -PreleaseVersion="$VERSION" -PdriverVersion="$DRIVER" + + # The .aar just published, so the GitHub Release carries the same file as GitHub Packages. + - name: Collect the published .aar + run: | + mkdir -p ../release-assets + cp ShimmerAndroidInstrumentDriver/build/outputs/aar/ShimmerAndroidInstrumentDriver-release.aar \ + "../release-assets/shimmerandroidinstrumentdriver-$VERSION.aar" + ls -l ../release-assets + + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: release-assets + path: release-assets/ + if-no-files-found: error + + release: + needs: [build, publish] + if: github.event_name == 'workflow_dispatch' && !inputs.dry_run + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: write # create the tag and the release + env: + GH_TOKEN: ${{ github.token }} + VERSION: ${{ needs.build.outputs.version }} + TAG: ${{ needs.build.outputs.tag }} + PREVIOUS_TAG: ${{ needs.build.outputs.previous_tag }} + DRIVER: ${{ needs.build.outputs.driver }} + steps: + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + name: release-assets + path: release-assets + + # The .aar's POM lists no dependencies, so the notes say which driver to pair it with. + - name: Create the tag and GitHub Release + working-directory: . + run: | + args=(-f tag_name="$TAG" -f target_commitish="$GITHUB_SHA") + if [ -n "$PREVIOUS_TAG" ]; then args+=(-f previous_tag_name="$PREVIOUS_TAG"); fi + driver_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY_OWNER/Shimmer-Java-Android-API/releases/tag/API_v$DRIVER" + { + echo "Built against \`com.shimmerresearch:shimmerdriver\` and \`com.shimmerresearch:shimmerbluetoothmanager\` **$DRIVER** ([API_v$DRIVER]($driver_url)). Add both, at that version, to your app's dependencies." + echo + # The driver's JNA is desktop-only; its two jars clash on META-INF/AL2.0 in an Android app. + echo "Exclude \`net.java.dev.jna\` from both, or the app fails packaging with \"2 files found with path 'META-INF/AL2.0'\". The exclusions this library builds with are in [ShimmerAndroidInstrumentDriver/build.gradle]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/blob/$TAG/ShimmerAndroidInstrumentDriver/build.gradle)." + echo + gh api "repos/$GITHUB_REPOSITORY/releases/generate-notes" "${args[@]}" --jq .body + } > notes.md + gh release create "$TAG" release-assets/* \ + --repo "$GITHUB_REPOSITORY" \ + --target "$GITHUB_SHA" \ + --title "$TAG" \ + --notes-file notes.md + { + echo "### Released Android API $VERSION" + echo "- Release: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/$TAG" + echo "- Package: com.shimmerresearch:shimmerandroidinstrumentdriver:$VERSION" + echo "- Built against driver $DRIVER" + } >> "$GITHUB_STEP_SUMMARY" diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/build.gradle b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/build.gradle index 672f4b69..7cdde38f 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/build.gradle +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/build.gradle @@ -77,13 +77,15 @@ publishing { def getArtificatId = { -> return "ShimmerAndroidInstrumentDriver" // Replace with library name ID } -/* +// Published by the "Release Android API" workflow (.github/workflows/release.yml), which picks the +// version and passes it as -PreleaseVersion; the AndroidAPI_v tags record what was released. Run +// assembleRelease before publish, because the artifact is the built .aar file. publishing { publications { bar(MavenPublication) { groupId = 'com.shimmerresearch' // Replace with your package's group/organization name artifactId = 'shimmerandroidinstrumentdriver' // Replace with the name of your package - version = '3.2.9_beta' // Replace with your package version + version = project.findProperty('releaseVersion') ?: 'local' // set by the Release Android API workflow artifact("$buildDir/outputs/aar/${getArtificatId()}-release.aar") } } @@ -91,16 +93,28 @@ publishing { repositories { maven { name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/ShimmerEngineering/ShimmerAndroidAPI") + url = uri("https://maven.pkg.github.com/ShimmerResearch/ShimmerAndroidAPI") credentials { - //Create github.properties in root project folder file with gpra.usr=GITHUB_USER_ID & gpra.key=PERSONAL_ACCESS_TOKEN - username = project.findProperty("gpr.user") ?: System.getenv("USERNAME") - password = project.findProperty("gpr.key") ?: System.getenv("TOKEN") + // Locally: gpr.user (or gpr.usr, the name the root build.gradle reads) and gpr.key in + // GRADLE_USER_HOME/gradle.properties, with a token that has write:packages. In the + // workflow: GITHUB_ACTOR and GITHUB_TOKEN. These are kept apart from USERNAME/TOKEN in + // the root build.gradle, which only need to read. + username = project.findProperty("gpr.user") ?: project.findProperty("gpr.usr") ?: System.getenv("GITHUB_ACTOR") + password = project.findProperty("gpr.key") ?: System.getenv("GITHUB_TOKEN") } } } } -*/ +// Without -PreleaseVersion the version is 'local', and GitHub Packages never lets a version be +// overwritten. Releases go through the Release Android API workflow; publishToMavenLocal is unaffected. +tasks.withType(PublishToMavenRepository).configureEach { + doFirst { + if (publication.version == 'local') { + throw new GradleException("Refusing to publish ${publication.artifactId} as version 'local'. " + + "Release with the Release Android API workflow, or pass -PreleaseVersion.") + } + } +} dependencies { implementation 'com.google.guava:guava:20.0' implementation 'java3d:vecmath:1.3.1' diff --git a/ShimmerAndroidInstrumentDriver/build.gradle b/ShimmerAndroidInstrumentDriver/build.gradle index e1e27086..10abd3f2 100644 --- a/ShimmerAndroidInstrumentDriver/build.gradle +++ b/ShimmerAndroidInstrumentDriver/build.gradle @@ -23,6 +23,11 @@ apply plugin: 'com.jfrog.artifactory' */ apply plugin: 'maven-publish' +// The shimmerdriver / shimmerbluetoothmanager version (Shimmer-Java-Android-API) that the library and +// every example build against. The default is what master builds with; the Release Android API +// workflow passes -PdriverVersion, by default the newest Java API release. +def shimmerDriverVersion = findProperty('driverVersion') ?: '0.11.7_beta' + allprojects { repositories { google() @@ -78,19 +83,25 @@ allprojects { implementation 'java3d:vecmath:1.3.1' implementation 'androidx.appcompat:appcompat:1.7.0' implementation 'com.github.Jasonchenlijian:FastBle:2.4.0' - implementation (group: 'com.shimmerresearch', name: 'shimmerbluetoothmanager', version:'0.11.7_beta'){ + implementation (group: 'com.shimmerresearch', name: 'shimmerbluetoothmanager', version: shimmerDriverVersion){ // excluding org.json which is provided by Android exclude group: 'io.netty' exclude group: 'com.google.protobuf' + // JNA (in the driver since 0.11.8_beta) is for the desktop USB debug listeners only; on + // Android its two jars clash on META-INF/AL2.0 and fail every app's packaging. + exclude group: 'net.java.dev.jna' exclude group: 'org.apache.commons.math' } - implementation (group: 'com.shimmerresearch', name: 'shimmerdriver', version:'0.11.7_beta'){ + implementation (group: 'com.shimmerresearch', name: 'shimmerdriver', version: shimmerDriverVersion){ // excluding org.json which is provided by Android exclude group: 'io.netty' exclude group: 'com.google.protobuf' + // JNA (in the driver since 0.11.8_beta) is for the desktop USB debug listeners only; on + // Android its two jars clash on META-INF/AL2.0 and fail every app's packaging. + exclude group: 'net.java.dev.jna' } } } diff --git a/ShimmerAndroidInstrumentDriver/gradlew b/ShimmerAndroidInstrumentDriver/gradlew old mode 100644 new mode 100755