12

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?

Krzysztof Bielak
  • 383
  • 1
  • 3
  • 11
  • 4
    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. – Neekoy Sep 05 '18 at 15:50

9 Answers9

12

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
Ruwanka Madhushan
  • 3,195
  • 5
  • 33
  • 48
  • 2
    i had to tail the output, my final statement: VERSION=$(mvn --batch-mode --non-recursive help:evaluate -Dexpression=project.version | grep -v '\[.*' | tail -1) – Mulder Feb 20 '19 at 11:08
7

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) .
Jakub Kania
  • 14,430
  • 2
  • 35
  • 44
6

This work for my variable: gitlab-ci.yml

mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q
Jasper de Vries
  • 16,868
  • 6
  • 60
  • 93
Erik Roky
  • 71
  • 1
  • 4
2

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 
Joao Vitorino
  • 2,641
  • 2
  • 29
  • 50
2

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

mvn --non-recursive help:evaluate -Dexpression=project.version -q -DforceStdout
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)

yerlilbilgin
  • 2,572
  • 2
  • 23
  • 19
1

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.

Besworks
  • 2,418
  • 12
  • 31
DaniyalVaghar
  • 98
  • 1
  • 8
0

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

luis-br
  • 473
  • 5
  • 9
0

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
Jasper de Vries
  • 16,868
  • 6
  • 60
  • 93