GitLab-CI get pom version in pipeline
Asked Answered
L

12

19

I'd like to build docker image via gitlab CI with project's version as a tag:

docker build -t dockerimage:VERSION-IN-POM .

In jenkins' pipeline i'm getting the version like that:

${pom.version}

Is it possible to read the version in a similar, convenient way from gitlab CI? Or do I have to write scripts for that?

Libriform answered 5/9, 2018 at 15:11 Comment(2)
I don't believe there is one, however you can write a before_script that would sed the version from your pom file. In Jenkins there are plugins that can simplify such tasks, however GitLab CI doesn't really have such.Frantz
What was the accepted answer or how did you solve this?Granulose
L
14

Assuming you have maven in build environment, you could use maven help plugin and grep to extract version.

VERSION=$(mvn --non-recursive help:evaluate -Dexpression=project.version | grep -v '\[.*')
echo $VERSION
Lollygag answered 9/9, 2018 at 5:23 Comment(3)
i had to tail the output, my final statement: VERSION=$(mvn --batch-mode --non-recursive help:evaluate -Dexpression=project.version | grep -v '[.*' | tail -1)Loredo
Is the backslash in the grep argument really correct? Didn't work for me. See the other comment without backslash now and wonder if the answer is even really wrong. The answer https://mcmap.net/q/630864/-gitlab-ci-get-pom-version-in-pipeline is much more robust anyway I'd say.Quartern
If you use mvn --non-recursive help:evaluate -Dexpression=project.version -q -DforceStdout Then you get the version without all the noisy mvn output. No need to grep!Tiberius
A
12

This work for my variable: gitlab-ci.yml

mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q
Americanism answered 26/5, 2019 at 8:11 Comment(0)
R
8

Gitlab-CI doesn't offer such comforts, instead it offers you to do whatever you want with the shell script. It's not very hard to do it in command script. You can install xmllint (apt install libxml2-utils on Ubuntu) and then you can get it by a simple query:

xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml

So it can all be solved by these two lines:

- apt install libxml2-utils
- docker build -t dockerimage:$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml) .
Radiant answered 5/9, 2018 at 20:10 Comment(1)
Not really, what if it inherits the version from a propert file or from a parent?Garble
F
5

Another maven command line alternative to get directly get the version information

mvn --non-recursive help:evaluate -Dexpression=project.version -q -DforceStdout
Fortnightly answered 19/11, 2020 at 18:17 Comment(0)
W
3

You can use sed or grep.

It's faster than using mvn --non-recursive help:evaluate ...

Get the artifactID

grep -m1 '<artifactId>' pom.xml | grep -oP  '(?<=>).*(?=<)'

Get the version

grep -m1 '<version>' pom.xml | grep -oP  '(?<=>).*(?=<)'

If you are using docker, some images don't have newest version of grep, so you need to use come creative solution with cut, example:

grep -m1 '<artifactId>' pom.xml |cut -d '<' -f2  |cut -d '>' -f2 
Wynn answered 27/2, 2020 at 17:56 Comment(1)
"creative cut solution " helped ;) ThanksOverplus
L
3

You can use the below command in your .gitlab-ci.yml file :

VERSION=$(mvn --non-recursive help:evaluate -Dexpression=project.version -q -DforceStdout)

echo $VERSION

Furthermore you can get groupId and artifactId by changing Dexpression=project.version to Dexpression=project.artifactId and Dexpression=project.groupId

For more information see the maven documentation for help:evaluate.

Lombard answered 16/11, 2021 at 8:15 Comment(0)
B
1

As indicated by Ivan in his post, this worked in my script:

-RELEASE_VERSION=xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml

-echo $RELEASE_VERSION

Banks answered 26/9, 2019 at 13:24 Comment(0)
G
1

if you know the project name, here is another approach using shell; is to cut the version from the target .jar file created under ./target directory.

Note: This will work only after successful build commands:

   cd target
   version=`ls <PROJECT_NAME>*.jar`
   version=${version#<PROJECT_NAME>} 
   version=${version%.jar}
   cd ..
   echo $version

<PROJECT_NAME> is the name of the project (use without <> marks)

Garble answered 8/9, 2021 at 9:19 Comment(0)
P
1

I ended up using

vars:
  stage: prepare
  script:
    - echo "POM_VERSION=$(mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q)" > vars.env
    - echo "POM_NAME=$(mvn -Dexec.executable='echo' -Dexec.args='${project.name}' --non-recursive exec:exec -q)" >> vars.env
    - echo "POM_GROUP_ID=$(mvn -Dexec.executable='echo' -Dexec.args='${project.groupId}' --non-recursive exec:exec -q)" >> vars.env
    - echo "POM_ARTIFACT_ID=$(mvn -Dexec.executable='echo' -Dexec.args='${project.artifactId}' --non-recursive exec:exec -q)" >> vars.env
  artifacts:
    reports:
      dotenv: vars.env
Prosit answered 22/3, 2022 at 12:56 Comment(0)
D
1

Here is a solution with the Maven help:evaluate but with a regex (see the "-E" in command) :

export VERSION=$(mvn --non-recursive help:evaluate -Dexpression=project.version | grep -E '^[0-9]+.[0-9]+.[0-9]+(-[A-Z]+)?$')
echo $VERSION

It will be able to find for example 5.0.1-SNAPHOT or only 5.0.1 or again 5.0.1-GA in the output of the Maven command.

Tested on this kind of realistic Maven output :

Downloaded from fodfin: http://your.server/artifactory/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar (621 kB at 369 kB/s)
[INFO] No artifact parameter specified, using 'be.test.super.package:the-application:pom:5.0.1-SNAPSHOT' as project.
[INFO]
5.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  15.127 s
[INFO] Finished at: 2022-10-06T14:40:29+02:00
[INFO] ------------------------------------------------------------------------
Dumanian answered 6/10, 2022 at 13:17 Comment(0)
C
0
jobname:
  stage: stage
  before_script:
    - export "MAVEN_ID=$(mvn help:evaluate -Dexpression=project.id -q -DforceStdout)"
    - >
      IFS=: read -r MAVEN_GROUPID MAVEN_ARTIFACTID MAVEN_PACKAGING MAVEN_VERSION <<< ${MAVEN_ID}
  script:
    - >
      echo -e "groupId: ${MAVEN_GROUPID}\nartifactId: ${MAVEN_ARTIFACTID}\nversion: ${MAVEN_VERSION}\npackaging: ${MAVEN_PACKAGING}"
  • mvn help:evaluate -Dexpression=project.id -q -DforceStdout prints artifact identification information in the following format: com.group.id:artifactid:packaging:version
  • MAVEN_ID variable is parsed using IFS based on the colon (:) as a separator to get common maven variables like artifactId, groupId, version and packaging (explanation)
  • later these variables can be used in the code, for example for echoing values
  • this way maven is executed only 1 time to get the information, which can speed up pipeline
  • IFS is a bash feature, thus corresponding GitLab runner should have bash installed
Constitution answered 16/11, 2022 at 14:43 Comment(0)
M
0

Without assuming anything, and using versions for build reproductibility, and bonus caching maven:

build:
  stage: build
  cache:
    key: maven
    paths:
      - .m2/repository
  script:
    # Extract Maven version
    - export MAVEN_VERSION=$(docker run --rm -v $CI_PROJECT_DIR:/app -w /app maven:3.8.4-openjdk-11-slim mvn org.apache.maven.plugins:maven-help-plugin:3.1.1:evaluate -Dexpression=project.version -q -DforceStdout)
Mastoidectomy answered 21/7 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.