From 73d32386447e2a9136ddc7d17493d8c34f2b1716 Mon Sep 17 00:00:00 2001 From: Denny Sheirer Date: Fri, 13 Nov 2026 03:32:08 -0500 Subject: [PATCH] #2634 Gradle build now uses git tag for versioning (#2635) Co-authored-by: denny --- .github/workflows/nightly.yml | 5 ++- .github/workflows/release.yml | 26 ++++++------ README.md | 12 +++--- build.gradle | 41 ++++++++++++++++++- gradle.properties | 17 ++++---- .../github/dsheirer/log/ApplicationLog.java | 3 +- .../dsheirer/monitor/DiagnosticMonitor.java | 3 +- .../dsheirer/properties/SystemProperties.java | 9 +++- 8 files changed, 85 insertions(+), 31 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 1ff57dec..72e80250 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -14,6 +14,9 @@ jobs: steps: - uses: actions/checkout@v7 + with: + # Full history and tags, so the build can describe the commit relative to the latest version tag + fetch-depth: 0 - name: Set up JDK 27 uses: actions/setup-java@v6 @@ -34,7 +37,7 @@ jobs: - name: Create Build Info file run: | - echo -e "ref: $GITHUB_REF \ncommit: $GITHUB_SHA\nbuild: $(date +"%Y-%m-%dT%H:%M:%SZ")" > build/image/build_info.txt + echo -e "ref: $GITHUB_REF \ncommit: $GITHUB_SHA\ndescribe: $(git describe --tags --match 'v[0-9]*' --dirty)\nbuild: $(date +"%Y-%m-%dT%H:%M:%SZ")" > build/image/build_info.txt - name: Add build artifacts to nightly release uses: softprops/action-gh-release@v3 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e26303d2..6766edaa 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,6 +1,6 @@ -# Publishes a release when a version tag is pushed. The tag has to match -# projectVersion in gradle.properties with a leading 'v': v0.6.2 for a final -# release, or v0.6.2-beta-1 / v0.6.2-alpha-1 for a pre-release. +# Publishes a release when a version tag is pushed: v0.6.2 for a final release, +# or v0.6.2-beta-1 / v0.6.2-alpha-1 for a pre-release. The build derives the +# version from the tag (see build.gradle). name: Release on: @@ -22,15 +22,6 @@ jobs: # Full history and tags, to find the previous release tag for the release notes fetch-depth: 0 - - id: version - run: | - expected="v$(sed -n 's/^projectVersion=//p' gradle.properties | tr -d '[:space:]')" - if [ "$GITHUB_REF_NAME" != "$expected" ]; then - echo "::error::Tag $GITHUB_REF_NAME does not match projectVersion in gradle.properties ($expected)" - exit 1 - fi - echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT" - # Release notes list the PRs since the previous version tag (never 'nightly'). A final release # starts from the previous final release, so it covers every alpha and beta in between. - id: previous @@ -54,6 +45,17 @@ jobs: - name: Set up Gradle uses: gradle/actions/setup-gradle@v6 + # The version comes from 'git describe', which can pick a different tag when more than one + # version tag points at the same commit. Fail rather than publish zips with the wrong version. + - id: version + run: | + version="$(./gradlew -q printVersion --console=plain | tail -n 1)" + if [ "v$version" != "$GITHUB_REF_NAME" ]; then + echo "::error::Build version $version does not match tag $GITHUB_REF_NAME" + exit 1 + fi + echo "version=$version" >> "$GITHUB_OUTPUT" + - name: Build and test run: ./gradlew clean build --console=plain diff --git a/README.md b/README.md index 1b0f7efe..a32cabc8 100644 --- a/README.md +++ b/README.md @@ -54,17 +54,19 @@ committed to the code base, so it's not really 'nightly' as much as it is 'curre Releases are built and published automatically by the [Release](.github/workflows/release.yml) GitHub workflow whenever a version tag is pushed to the repository. -1. Update `projectVersion` in `gradle.properties` to the new version, following the naming convention described in -that file (e.g. `0.6.2-alpha-1`, `0.6.2-beta-1` or `0.6.2`). -2. Commit and push the change to `master`. -3. Create and push a tag that matches the version with a leading `v`: +The version is taken from the tag, so there is no version number to update in the code. Create and push a tag +with a leading `v`, following the naming convention described in `gradle.properties` (e.g. `v0.6.2-alpha-1`, +`v0.6.2-beta-1` or `v0.6.2`): ``` git tag v0.6.2-beta-1 git push origin v0.6.2-beta-1 ``` +Builds made from an untagged commit get a version like `0.6.2-beta-1-14-g4626d567` (14 commits after the +`v0.6.2-beta-1` tag, at commit `4626d567`), with `-dirty` appended when there are uncommitted changes. + The workflow then: -* Verifies that the tag matches `projectVersion` in `gradle.properties` and fails if it does not. +* Verifies that the version the build derives from git matches the tag and fails if it does not. * Builds and tests the code, then creates the release zips for Linux, Mac and Windows (x86-64 and aarch64). * Creates a GitHub release titled `Version ` with auto-generated release notes (a "What's Changed" list of the PRs merged since the previous version tag), and attaches the six zip diff --git a/build.gradle b/build.gradle index fa7d6441..b62f83ce 100644 --- a/build.gradle +++ b/build.gradle @@ -52,8 +52,44 @@ plugins { id 'org.beryx.runtime' version '2.0.1' } -//Version is defined in gradle.properties so that we can CLI override it for nightly builds. -version = project.findProperty('projectVersion') +/** + * Version is derived from the most recent 'v' git tag (e.g. v1.0.0-beta-1 -> 1.0.0-beta-1). Commits after + * the tag produce a version like 1.0.0-beta-1-14-g4626d567 and uncommitted changes append '-dirty'. The moving + * 'nightly' tag is ignored. Override with -PprojectVersion= (the nightly build uses 'nightly' so that the + * nightly release asset names stay the same). Falls back to 'unknown' when built outside a git checkout. + */ +String gitDescribe = describeGitVersion() +version = project.findProperty('projectVersion') ?: (gitDescribe ? gitDescribe.substring(1) : 'unknown') + +String describeGitVersion() +{ + try + { + def describe = providers.exec { + commandLine 'git', 'describe', '--tags', '--match', 'v[0-9]*', '--dirty' + ignoreExitValue = true + } + + if(describe.result.get().exitValue == 0) + { + return describe.standardOutput.asText.get().trim() + } + } + catch(Exception e) + { + logger.warn("Unable to determine version from git tags: " + e.message) + } + + return null +} + +tasks.register('printVersion') { + description = 'Prints the project version' + def projectVersion = version + doLast { + println projectVersion + } +} //Java 27 is required for this version of the Project Panama preview/incubator feature java { @@ -202,6 +238,7 @@ jar { attributes ( 'Implementation-Title' : 'sdrtrunk project', 'Implementation-Version' : archiveVersion, + 'Git-Describe' : gitDescribe ?: 'unknown', 'Created-By' : "Gradle ${gradle.gradleVersion}", 'Build-Timestamp' : new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()), 'Build-JDK' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']}", diff --git a/gradle.properties b/gradle.properties index 2fb17020..0c6d7faf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,15 +17,16 @@ # ***************************************************************************** # -# Version naming conventions: -# Alpha: maj.min.patch-alpha-build -# Beta: maj.min.patch-beta-build -# Release: maj.min.patch +# The project version is no longer defined here. It is derived from the most recent 'v' git tag (see +# build.gradle) and can be overridden with -PprojectVersion=. # -# Example: 0.6.0-beta-5 +# Version tag naming conventions: +# Alpha: vmaj.min.patch-alpha-build +# Beta: vmaj.min.patch-beta-build +# Release: vmaj.min.patch # -# Note: this is so that the file names for the resultant release build products match the GitLab release asset +# Example: v0.6.0-beta-5 +# +# Note: this is so that the file names for the resultant release build products match the GitHub release asset # tag name, specifically the dashes inserted before and after the alpha/beta labels. # See: https://github.com/DSheirer/sdrtrunk/issues/1651 - -projectVersion=1.0.0-beta-1 \ No newline at end of file diff --git a/src/main/java/io/github/dsheirer/log/ApplicationLog.java b/src/main/java/io/github/dsheirer/log/ApplicationLog.java index 155504d0..594aa903 100644 --- a/src/main/java/io/github/dsheirer/log/ApplicationLog.java +++ b/src/main/java/io/github/dsheirer/log/ApplicationLog.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -133,6 +133,7 @@ public class ApplicationLog Attributes atts = findManifestAttributes(); if (atts != null) { mLog.info("SDRTrunk Version : " + atts.getValue("Implementation-Version")); + mLog.info("Git Describe : " + atts.getValue("Git-Describe")); mLog.info("Gradle Version : " + atts.getValue("Created-By")); mLog.info("Build Timestamp : " + atts.getValue("Build-Timestamp")); mLog.info("Build-JDK : " + atts.getValue("Build-JDK")); diff --git a/src/main/java/io/github/dsheirer/monitor/DiagnosticMonitor.java b/src/main/java/io/github/dsheirer/monitor/DiagnosticMonitor.java index aaccad21..8aa6c446 100644 --- a/src/main/java/io/github/dsheirer/monitor/DiagnosticMonitor.java +++ b/src/main/java/io/github/dsheirer/monitor/DiagnosticMonitor.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -247,6 +247,7 @@ public class DiagnosticMonitor Attributes atts = findManifestAttributes(); if (atts != null) { sb.append("\nVersion : " + atts.getValue("Implementation-Version")); + sb.append("\nGit Describe : " + atts.getValue("Git-Describe")); sb.append("\nGradle Version : " + atts.getValue("Created-By")); sb.append("\nBuild Timestamp : " + atts.getValue("Build-Timestamp")); sb.append("\nBuild-JDK : " + atts.getValue("Build-JDK")); diff --git a/src/main/java/io/github/dsheirer/properties/SystemProperties.java b/src/main/java/io/github/dsheirer/properties/SystemProperties.java index f81ed3d5..15edf2d2 100644 --- a/src/main/java/io/github/dsheirer/properties/SystemProperties.java +++ b/src/main/java/io/github/dsheirer/properties/SystemProperties.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -48,6 +48,7 @@ public class SystemProperties private static String PROPERTIES_FILENAME = "SDRTrunk.properties"; private static String MANIFEST_VERSION = "Implementation-Version"; private static String BUILD_TIMESTAMP = "Build-Timestamp"; + private static String GIT_DESCRIBE = "Git-Describe"; private static SystemProperties INSTANCE; private static Properties mProperties; @@ -217,12 +218,18 @@ public class SystemProperties { String version = manifest.getMainAttributes().getValue(MANIFEST_VERSION); String timestamp = manifest.getMainAttributes().getValue(BUILD_TIMESTAMP); + String gitDescribe = manifest.getMainAttributes().getValue(GIT_DESCRIBE); if(version != null) { if(version.contains("nightly") && timestamp != null) { sb.append( " nightly - ").append(timestamp); + + if(gitDescribe != null && !gitDescribe.equals("unknown")) + { + sb.append(" (").append(gitDescribe).append(")"); + } } else {