allegro / axion-release-plugin

Gradle release & version management plugin.
https://axion-release-plugin.readthedocs.io/
Apache License 2.0
562 stars 155 forks source link

Single version across separate projects in a monorepo #845

Open tajobe opened 3 weeks ago

tajobe commented 3 weeks ago

I have a repository which contains multiple gradle projects (not subprojects, but independent builds) and I wish to keep them on the same version.

I understand it is possible to configure the repository path via:

scmVersion {
    repository {
        directory.set(rootProject.file("../").path)
    }
}

...which enables the non-root projects to see the git repo and pick up tags for versioning, however for incrementing and snapshots, it doesn't seem to take any commits outside its path into account. It seems like monorepoConfig is how we should be telling it how to include changes outside its dir when filtering the git log, but setting monorepoConfig.include(listOf(rootProject.file("../").path)) doesn't seem to help. I noticed in jgit that path filters are supposed to be relative to the root of the repository, so I tried using . as an include (eg monorepoConfig.dependenciesDirs.add(".")) to see changes in the whole repo, which also didn't work.

For example, I have a test project with the following layout:

❯ tree
.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── project1
    ├── build.gradle.kts
    ├── gradlew
    └── gradlew.bat

4 directories, 8 files

build.gradle.kts:

plugins {
    id("pl.allegro.tech.build.axion-release") version "1.18.13"
}

group = "com.example"
version = scmVersion.version

project1/build.gradle.kts:

plugins {
    id("pl.allegro.tech.build.axion-release") version "1.18.13"
}

scmVersion {
    repository {
        directory.set(rootProject.file("../").path)
    }
}

group = "com.example"
version = scmVersion.version

tasks.wrapper {
    setJarFile("../gradle/wrapper/gradle-wrapper.jar")
}

If I have a v0.1.0 release followed by some commit, eg adding a README to the root, the versions diverge:

❯ ./gradlew release -Prelease.localOnly

> Task :verifyRelease
Looking for uncommitted changes..
Skipping ahead of remote check
Checking for snapshot versions..

> Task :release
Creating tag: v0.1.0
Changes made to local repository only

BUILD SUCCESSFUL in 716ms
2 actionable tasks: 2 executed
❯ touch README.md
❯ git add README.md
❯ git commit -am "Add README"
[main 455a6e5] Add README
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
❯ ./gradlew currentVersion -q

Project version: 0.1.1-SNAPSHOT
❯ ./gradlew --project-dir project1 currentVersion -q

Project version: 0.1.0

The docs state that a -SNAPSHOT is suffixed when not on a tag, but the project1 version isn't being decorated despite the commit being ahead of the v0.1.0 tag. I'm guessing it has to do with the changes being made outside of the project1 directory, but including the root path via monorepoConfig didn't help, though it's certainly possible I misunderstand its configuration.

How can I get the non-root project to always have the same version?

tajobe commented 1 week ago

It seems once the plugin passes the project directory (non-root dir) here, there is no way to back out to the root of the repo with jgit path filters as . does nothing and/ is treated as empty and fails the non-empty check.

Would it be possible to have some flag option in the monorepo config for not passing the project dir? EG at https://github.com/allegro/axion-release-plugin/blob/876ab173185112ee25b2d7efb651e2f67abf625b/src/main/java/pl/allegro/tech/build/axion/release/infrastructure/git/GitRepository.java#L254

if (!includeProjectPath || path.isEmpty()) {