git-commit-id / git-commit-id-maven-plugin

Maven plugin which includes build-time git repository information into an POJO / *.properties). Make your apps tell you which version exactly they were built from! Priceless in large distributed deployments... :-)
http://www.kto.so
GNU Lesser General Public License v3.0
1.62k stars 299 forks source link

git.branch removed folder name #783

Closed SupritamACN closed 4 weeks ago

SupritamACN commented 1 month ago

Describe the question / problem (required)

Hi, I have a branch feature/my-git-branch, however the generated git.properties have the property git.branch as only my-git-branch. I want the full branch name displayed as feature/my-git-branch. Is there a workaround or do I need to use another property?

Context (optional)

I and using io.github.git-commit-id:git-commit-id-maven-plugin:9.0.1.

TheSnoozer commented 1 month ago

Can you post the full config of the plugin?

SupritamACN commented 1 month ago

Adding plugin config as requested. @TheSnoozer

         <plugin>
                <groupId>io.github.git-commit-id</groupId>
                <artifactId>git-commit-id-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>get-the-git-infos</id>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                    <prefix>git</prefix>
                    <verbose>false</verbose>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
                    </generateGitPropertiesFilename>
                    <!--<format>properties</format>-->
                    <gitDescribe>
                        <skip>false</skip>
                        <always>false</always>
                        <dirty>-dirty</dirty>
                    </gitDescribe>
                    <includeOnlyProperties>
                        <includeOnlyProperty>git.branch</includeOnlyProperty>
                        <includeOnlyProperty>git.commit.id</includeOnlyProperty>
                        <includeOnlyProperty>git.remote.origin.url</includeOnlyProperty>
                        <includeOnlyProperty>git.commit.user.name</includeOnlyProperty>
                        <includeOnlyProperty>git.commit.message.short</includeOnlyProperty>
                        <includeOnlyProperty>git.commit.time</includeOnlyProperty>
                    </includeOnlyProperties>
                </configuration>
            </plugin>
TheSnoozer commented 1 month ago

Mhh I can't reproduce this issue.

On what OS do you experience this?

What are the java version and maven version version involved? (mvn --version should tell you)

Are you sure you are looking at the correct git.properties?

Can you maybe change the <verbose>false</verbose> to a <verbose>true</verbose> and check what variables are being picked up?

On my end, just tested with a clone of the project and adjusting the demo profile to your config:

$ mvn clean initialize -Pdemo -Dskip.tests=true

[INFO] --- git-commit-id-maven-plugin:9.0.1:revision (get-the-git-infos) @ git-commit-id-maven-plugin ---
[INFO] dotGitDirectory '/git-commit-id/git-commit-id-maven-plugin/.git'
[INFO] Collected git.branch with value feature/my-git-branch
[INFO] Collected git.commit.id with value e70100d432502c5d4b4bece4974ced7943196c10
[INFO] Collected git.commit.user.name with value TheSnoozer
[INFO] Collected git.commit.message.short with value Merge pull request #782 from git-commit-id/dependabot/maven/org.apache.maven.plugins-maven-dependency-plugin-3.8.0
[INFO] Collected git.commit.time with value 2024-09-01T16:53:48+02:00
[INFO] Collected git.remote.origin.url with value git@github.com:git-commit-id/git-commit-id-maven-plugin.git
[INFO] including property 'git.commit.id' in results
[INFO] including property 'git.commit.message.short' in results
[INFO] including property 'git.commit.user.name' in results
[INFO] including property 'git.branch' in results
[INFO] including property 'git.commit.time' in results
[INFO] including property 'git.remote.origin.url' in results
[INFO] Writing properties file [/git-commit-id/git-commit-id-maven-plugin/target/classes/git.properties] (for project Git Commit Id Maven Plugin)...

Git-Branch is correctly extracted as feature/my-git-branch

SupritamACN commented 1 month ago

When I ran with verbose true, I see that it picks the correct branch value as hydra/git-info but in the final file in the packaged jar this is the content of the git.properties.

#Generated by Git-Commit-Id-Plugin
git.branch=git-info
git.commit.id=ba723a1f12894792f34ab6c7583c0e1a04a3543e
git.commit.message.short=verbose git-info
git.commit.time=2024-09-03T17\:52\:42-0700
git.commit.user.name=Supritam
git.remote.origin.url=git@github.abc.com\:abc/git-service.git

Is it possible that there is interference from another plugin? I am using the below plugins. image

TheSnoozer commented 4 weeks ago

My suspect would be spring boot, but I just tested locally with this sample_project.zip and everything works fine:

$ cat BOOT-INF/classes/git.properties 
#Generated by Git-Commit-Id-Plugin
git.branch=feature/testbranch
git.commit.id=0e2b4d63f478aca5c2841570963f58682c4200e9
git.commit.message.short=foo
git.commit.time=2024-09-04T19\:21\:52+02\:00
git.commit.user.name=TheSnoozer
git.remote.origin.url=Unknown

Please provide a project where the issue can be reproduced (e.g. by modifing the attached project).

SupritamACN commented 4 weeks ago

Ok, I figured out a workaround. As part of my Jenkins pipeline, my artifact build stage had a environment variable GIT.BRANCH. So during this stage the plugin was taking the value to the environment variable GIT.BRANCH which was set to just git-info. I used the below property to use default git property instead of the environment variable.

<useBranchNameFromBuildEnvironment>false</useBranchNameFromBuildEnvironment>
TheSnoozer commented 3 weeks ago

Great thanks for sharing the solution!

Edit just wanted to check if there is anything missing in the plugin, and I don't think there is anything the plugin can improve here. With a <verbose>true</verbose> the plugin would tell you that it is using the environment variables:

$ git branch
* branch/feature

$ export JENKINS_URL="foo"
$ export GIT_BRANCH="feature"
$ mvn clean initialize -Pdemo -Dskip.tests=true
...
[INFO] Using environment variable based branch name. GIT_BRANCH = feature
[INFO] Collected git.branch with value feature
[INFO] Collected git.commit.user.name with value TheSnoozer
[INFO] Collected git.commit.message.short with value foo
[INFO] Collected git.commit.time with value 2024-09-06T22:17:26+02:00
[INFO] Collected git.remote.origin.url with value null
[INFO] including property 'git.commit.id' in results
[INFO] including property 'git.commit.message.short' in results
[INFO] including property 'git.commit.user.name' in results
[INFO] including property 'git.branch' in results
[INFO] including property 'git.commit.time' in results
[INFO] including property 'git.remote.origin.url' in results
...