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?
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.
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.
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.
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.
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
This work for my variable: gitlab-ci.yml
mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With