Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab-CI get pom version in pipeline

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?

like image 836
Krzysztof Bielak Avatar asked Sep 05 '18 15:09

Krzysztof Bielak


People also ask

What is POM model version?

It can have the following values like import, system, test, runtime, provided, and compile. Project: This is the root tag for the pom. xml file. Model version: This is a part of the project tag. It defines the model version and for Maven 2 and 3, its value is set to 4.0.

Does GitLab have a Maven repository?

To deploy to Gitlab navigate to the local root directory path. Run this command "ONLY" creates the Maven Repository on Gitlab. In this example the settings. xml file is being read from local directory.

Where does GitLab run pipeline?

In the project overview in GitLab, select on the right pane CI/CD --> Pipelines. Here you can find a list of recently executed pipelines. If you select a pipeline, you get a detailed overview where you can check which job failed (in case the pipeline failed) and see the output of individual jobs.

How pipelines work in GitLab?

A pipeline is a collection of jobs split in different stages. All the jobs in the same stage run concurrently (if there are enough runners) and the next stage begins only if all the jobs from the previous stage have finished with success. As soon as a job fails, the entire pipeline fails.


4 Answers

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
like image 105
Ruwanka Madhushan Avatar answered Oct 24 '22 04:10

Ruwanka Madhushan


This work for my variable: gitlab-ci.yml

mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q
like image 24
Erik Roky Avatar answered Oct 24 '22 03:10

Erik Roky


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)

like image 34
yerlilbilgin Avatar answered Oct 24 '22 03:10

yerlilbilgin


you can use below command in your .gitlab-ci.yml :

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

echo $VERSION

furthermore you can get groupId and artifactId by change this part Dexpression=project.version to Dexpression=project.artifactId and Dexpression=project.groupId

for more command and description you can use below link: https://maven.apache.org/plugins/maven-help-plugin/evaluate-mojo.html

like image 25
DaniyalVaghar Avatar answered Oct 24 '22 04:10

DaniyalVaghar