【问题标题】:Unable to retrieve branch name during the RELEASE build using git-commit-id.branch of git-commit-id-plugin使用 git-commit-id-plugin 的 git-commit-id.branch 在 RELEASE 构建期间无法检索分支名称
【发布时间】:2017-05-21 07:32:39
【问题描述】:

我正在尝试从 git 存储库中检索分支名称。当我在本地和 Dev 环境中构建它时,我能够检索分支名称。但是当第二次构建时,版本从 SNAPSHOT 更改为 RELEASE,分支名称使用 pom versions 初始化的项目。下面是使用的插件

                  <plugin>
                        <groupId>pl.project13.maven</groupId>
                        <artifactId>git-commit-id-plugin</artifactId>
                        <version>2.2.2</version>
                        <executions>
                            <execution>
                                <id>git-commit-id</id>
                                <goals>
                                    <goal>revision</goal>
                                </goals>
                                <phase>validate</phase>
                                <configuration>
                                    <dateFormat>EEE MMM dd hh:mm:ss zzz yyyy</dateFormat>
                                    <prefix>git-commit-id</prefix>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

下面是 properties 文件中的条目,在构建过程中,构建详细信息被加载到该文件中。

appsVersion=${pom.version}
appsBuildDateTime=${build-timestamp}
buildBranch=${git.branch}

现在实际的问题是,文件属性的 buildBranch 是用 appsVersion 设置的。就是这样,

     appsVersion=${pom.version}--->17.2.8
     buildBranch=${git.branch}--->is initialized with 17.2.8 instead of BRANCH NAME during 
RELEASE build.

我也尝试过其他插件,例如 Jgit(maven-jgit-buildnumber-plugin)org.codehaus.mojo(buildnumber-maven-plugin) 但在徒劳的。

请帮助我找出问题所在,因为我们没有在运行时调试的选项。任何帮助都将受到高度赞赏,因为我完全被这个问题所困扰。而且我不想尝试其他插件,因为我在这个问题上花费了大量时间。

【问题讨论】:

    标签: git build maven-plugin


    【解决方案1】:

    上述问题的一个可能的解决方案可能在这里

    https://github.com/ktoso/maven-git-commit-id-plugin/issues/359

    它对我来说就像一个魅力

    【讨论】:

      【解决方案2】:

      这可能类似于maven/git/GitDataProvider.java#determineBranchName()maven/git/GitDataProvider.java#determineBranchNameOnBuildServer()

      如果在 Jenkins/Hudson 中运行,请遵守通过 GIT_BRANCH env var 传递的分支名称。
      这是必要的,因为 Jenkins/Hudson 总是在分离的头部状态下调用构建。

      在您的情况下,可能不涉及 Jenkins,但检查 Git 的状态(git status)以查看 maven 构建是否在分支上运行(分离头或标签)

      以防万一,检查环境变量GIT_BRANCHGIT_LOCAL_BRANCH的值,这个maven插件应该使用。


      作为替代方案,Anand Sunderraman 建议in the comments 看看ktoso/maven-git-commit-id-plugin issue 359

      我在 VSTS 构建代理上遇到了同样的问题。
      事实证明 VSTS git checkout 基于特定分支上的最后一次提交以分离模式发生,因此用 commit-id 代替分支名称。

      我实施了一个解决方法:

      <configuration>
       <replacementProperties>
        <replacementProperty>
          <property>git.branch</property>
          <token>^.*$</token>
          <!--NOTE: required for VSTS env where checked out code is not on a branch-->
          <value>${env.BUILD_SOURCEBRANCH}</value>
          <regex>true</regex>
        </replacementProperty>
       </replacementProperties>
      </configuration>
      

      如果env.BUILD_SOURCEBRANCH 不可用,它也可以在本地工作,它会回退并使用git branch

      【讨论】:

      • 你好 VonC。我没有 maven/git/GitDataProvider.java#determineBranchName() 或 maven/git/GitDataProvider.java#determineBranchNameOnBuildServer() 的代码。即使我拥有它,我也无法调试它,因为我没有对 QA 环境进行调试的环境访问权限。你能确认我是否使用了正确的插件和正确的配置。
      • @Hiren“我没有代码”你的意思是我在答案中提供的链接不起作用吗?
      • 你好 VonC。我是一名普通的开发人员,试图获取分支名称并在仪表板上显示相同的名称。我只是简单地使用 git-commit-id-plugin(pl.project13.maven) 来获取分支名称。即使我检查了 git 代码(你已经提到过),我也可能无法调试它。你能指导我完成需要做什么吗?
      • 只是一个小的修正。我使用 buildBranch=${git-commit-id.branch} 而不是 buildBranch=${git.branch}
      • @AnandSunderraman 好收获!我已将您的评论包含在答案中以提高知名度。
      猜你喜欢
      • 2019-11-20
      • 2019-11-09
      • 2014-02-10
      • 2019-08-10
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多