Git-commit-id-maven-plugin: using plugin generated values inside build pipeline

Created on 28 Jun 2019  路  4Comments  路  Source: git-commit-id/git-commit-id-maven-plugin

Not realy a bug, more a "I dont get this" thing.
How can I use the generated properites inside my build pipeline. I am using gitlab and droid.io at the moment. So a sollution for either of this would be fine.

I have put together a pretty simple droid pipeline where I would like to access these properties in the pipeline:
git.commit.time 20190628-132238
git.commit.id.abbrev 25e26f0

this is my pipeline

kind: pipeline
name: default

steps:
- name: build
  image: maven:3-jdk-11
  commands:
  - mvn -gs settings.xml -B -X clean install -pl '!qa'

- name: docker
  image: plugins/docker
  settings:
    repo: some.reg/name/name2
    tags:
      - latest
      - ${DRONE_COMMIT_SHA}
    registry: some.reg
    username:
      from_secret: harbor_username
    password:
      from_secret: harbor_password
    insecure: true

image_pull_secrets:
- dockerconfigjson

I would like to change DRONE_COMMIT_SHA with the above values.

not-a-bug

Most helpful comment

@Santonian,
do you wanna get the shorten SHA in drone CI?
if yes, you can revise your drone.yml as the following:

- name: docker
  image: plugins/docker
  settings:
    repo: some.reg/name/name2
    tags:
      - latest
      - ${DRONE_COMMIT_SHA:0:7}

then you can get 25e26f0 in Drone CI

All 4 comments

Hello,
thank you for opening such ticket.

In theory this plugin was designed in such a way that it will extract the properties during your build and may make them available in properties file inside your application. This essentially allows your bundeled application to use the generated properties whenever needed (e.g. to display a version number).
The plugin was not designed to support your use-case, but there are ways to achieve that.

The first suggestion I would have is to ask if you really need a property that was generated by this plugin. The most popular CI environments may already make the variables you need available without using the plugin. For gitlab you get access to the following without using the plugin:

export CI_COMMIT_SHA="1ecfd275763eff1d6b4844ea3168962458c9f27a"
export CI_COMMIT_SHORT_SHA="1ecfd275"
export CI_COMMIT_REF_NAME="master"
export CI_REPOSITORY_URL="https://gitlab-ci-token:[email protected]/gitlab-org/gitlab-ce.git"
export CI_COMMIT_TAG="1.0.0"

So what happens if you still need a property that was generated by the plugin? Well here comes the annoying part. Either you write a bash/shell script that reads the generated properties file (e.g. https://stackoverflow.com/a/28831442/7890667 or similar solutions with using grep) OR you simply run the underlying git command directly.

Just as an example: if the plugin tries to extract the total commit count we run git rev-list HEAD --count.

In summary you may ask why should I use the plugin in the first place when my release CI has all the variables I need. Well in such case you don't need to. The plugin offers you a more flexible way of extracting the information you want. CI environment usually expose just a portion of it.

Hope this helps.

PS:
How to generate a properties file:

<plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <executions>
                    <execution>
                        <id>get-the-git-infos</id>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>

                </executions>
                <configuration>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <generateGitPropertiesFilename>
                        ${project.build.outputDirectory}/git.properties
                    </generateGitPropertiesFilename>
                </configuration>
</plugin>

@Santonian,
do you wanna get the shorten SHA in drone CI?
if yes, you can revise your drone.yml as the following:

- name: docker
  image: plugins/docker
  settings:
    repo: some.reg/name/name2
    tags:
      - latest
      - ${DRONE_COMMIT_SHA:0:7}

then you can get 25e26f0 in Drone CI

@hp-huang-tw Thanks for the tip. Seems like an other undocumented feature of drone. In drone I could acomplish this with this statement:
echo -n $(git rev-parse --short=8 HEAD)-$(git show -s --format=%cd --date=format:%Y%m%d-%H%M%S HEAD),latest > .tags
The .tags file is read by the docker build plugin (also undocumented...)

Anyway, the tip from @TheSnoozer was relevant. In my CI pipeline I simply use git to get what I want and I am using the plugin to generate the information in the git.properties to show them in my actuator /info endpoint at runtime.
Problem solved! :-)

Thanks for sharing your tip @hp-huang-tw !
Sorry that I can't be much more of a help here, but the plugin was simply not designed for such use-case...well you found a way to get your information so should be all good :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Punkoivan picture Punkoivan  路  3Comments

darrenbkl picture darrenbkl  路  3Comments

delanym picture delanym  路  7Comments

dandragut picture dandragut  路  7Comments

danh-fissara picture danh-fissara  路  4Comments