Question

How to increment version prior to publishing in GitHub packages

How to increment version prior to publishing in GitHub packages. We update version attribute everytime before we do ./gradlew publish

Snippet from build.gradle

group = 'com.company.user'
version = '0.0.1'
description = """user-service"""

If we build and publish using GitHub action, how do I auto increment version prior to running the publish command

 4  111  4
1 Jan 1970

Solution

 1

I used the Gradle Release Plugin (https://github.com/researchgate/gradle-release) successfully.

It checks for SNAPSHOT-Dependencies before the release and also has a built in autoVersioning (look for the Working in Continuous Integration in the docs). If you need, you can specify the versions manually, but you don't have to.

This is not specially made for GitHub actions, but from my experience with such vendor-bindings for projects where you plan long-term development or support I would not work with special plugins there - it will make a potential migration to another system harder.

2024-07-11
cyberbrain

Solution

 0

A note about versioning:

Judging from your gradle snippet version = '0.0.1' you formally align to the major.minor.patch versioning pattern. With major versions representing breaking changes, minor versions bringing new features and patch versions bringing bug fixes. I doubt, that it is ensured that every publishing either contains major, minor or patch changes exclusively. A steady increasing of one of these version digits may not be best practice nor offers clarity of the versioning.

Instead, I see two approaches that may suite your CI (Continues Integration) setup:

  1. If you publish to a maven repository, you can use add a -SNAPSHOT suffix to you versioning for non-release builds. The maven-publish plugin will automatically add a build timestamp before uploading to the maven repository. The maven repository then manages to always serve the latest SNAPSHOT bild when the major.minor.path-SNAPSHOT artefact is requested.
  2. If you have a centralized CI tool (e.g. jenkins, teamcity, ...) and just want to dinstinguish different builds of the same version, you can add the build number as suffix to the version. Most of the CI tools provide the build number as environment variable or other type of property. If building and publishing takes place locally, you have to manage the build number on your own. May use an environment variable or a gradle project property. Register a new task to your build.gradle, get the current build number, increment it before publishing and update the build number origin to the new value.

gradle example (not tested)


// may use ext.publishVersion in case of value update issues
def publishVersion = project.version

publishing {
    publications {
        myArtifact(MavenPublication) {
            artifactId = artifactBaseName
            version = publishVersion
        }
    }
}

// example with gradle.properties file. Same approach (read, update) for env. variables
tasks.register("IncreaseBuildNumber") {
    // buildNumber property is stored in 'gradle.properties'
    newbuildNumber = project.property["buildNumber"] as Integer
    newbuildNumber = newbuildNumber + 1

    publishVersion = publishVersion + ".${newbuildNumber}"

    // update 'gradle.properties' file
    var propertiesFile = new File("${project.rootDir}/gradle.properties")

    doLast {
        var updatedContent = propertiesFile.getText('UTF-8').replace(
                "buildNumber=${project.version}",
                "buildNumber=${newbuildNumber }"
        )

        delete propertiesFile 
        new File(propertiesFile.toString()).write(updatedContent, 'UTF-8')
    }
}

tasks.named('publish').configure {
    dependsOn tasks.named('IncreaseBuildNumber')
}
2024-07-11
Max M

Solution

 0

Create a script in your repository

# Extract the current version from build.gradle
current_version=$(grep '^version =' build.gradle | sed 's/version = //' | tr -d \')

# Split the version into its components
IFS='.' read -r -a version_parts <<< "$current_version"

# Increment the patch version
new_patch_version=$((version_parts[2] + 1))

# Construct the new version string
new_version="${version_parts[0]}.${version_parts[1]}.$new_patch_version"

# Update build.gradle with the new version
sed -i "s/version = '$current_version'/version = '$new_version'/g" build.gradle

# Commit the changes
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git add build.gradle
git commit -m "Increment version to $new_version"

and allow it execute permissions

chmod +x increment_version.sh

Create a GitHub Actions Workflow file (.github/workflows/publish.yml)

name: Publish Package

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Set up JDK 11
      uses: actions/setup-java@v2
      with:
        java-version: '11'
        distribution: 'adopt'

    - name: Increment version
      run: ./increment_version.sh

    - name: Publish to GitHub Packages
      run: ./gradlew publish
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Push version increment commit
      uses: ad-m/github-push-action@v0.6.0
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
2024-07-18
user26383060