Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract version ID from POM in a Jenkins pipeline

I've created a pipeline and using the embedded groovy pipeline script definition and can't seem to get the version ID of the project from the POM. I tried this which works in a groovy console but on in the Jenkins build pipeline script:

def project = new XmlSlurper().parse(new File("pom.xml")) def pomv = project.version.toString() 

According to the documentation Jenkins has a $POM_VERSION but the value doesn't have anything in it when I assign it to a variable and echo it out.

def pomv = "$POM_VERSION" 

OR

def pomv = '$POM_VERSION" 
like image 375
user301693 Avatar asked Jun 02 '16 23:06

user301693


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.

Which command can be used to check Maven version in Jenkins?

You can use grep command to suck the version number with it.

What is artifactId in Jenkins?

artifactId is mandatory and uniquely identifies your plugin in Jenkins. It is the unique base name of the primary artifact being generated by this maven project. This plugin tutorial uses the name demo (user input highlighted in bold).


2 Answers

Use readMavenPom like this:

pom = readMavenPom file: 'pom.xml' pom.version 

See Model reference for properties (like the above version).

For this to work, one has to install Pipeline Utility Steps plugin

like image 132
Krzysztof Krasoń Avatar answered Oct 14 '22 04:10

Krzysztof Krasoń


In Jenkins 2.138.3 there are two different types of pipelines.

Declarative and Scripted pipelines.

"Declarative pipelines is a new extension of the pipeline DSL (it is basically a pipeline script with only one step, a pipeline step with arguments (called directives), these directives should follow a specific syntax. The point of this new format is that it is more strict and therefore should be easier for those new to pipelines, allow for graphical editing and much more. scripted pipelines is the fallback for advanced requirements."

jenkins pipeline: agent vs node?

Here is an example of a Declarative Pipeline:

pipeline {      agent any       environment {     //Use Pipeline Utility Steps plugin to read information from pom.xml into env variables     IMAGE = readMavenPom().getArtifactId()     VERSION = readMavenPom().getVersion()     }       stages {          stage('Test') {             steps {                 echo "${VERSION}"             }          }      }  } 

Example of Scripted Pipeline

node('master') {    stage('Test') {     IMAGE = readMavenPom().getArtifactId()     VERSION = readMavenPom().getVersion()     echo "IMAGE: ${IMAGE}"     echo "VERSION: ${VERSION}"   }  } 

Here are some good links:

Declarative https://github.com/jenkinsci/pipeline-examples/blob/master/declarative-examples/jenkinsfile-examples/mavenDocker.groovy

Scripted https://bulldogjob.com/articles/726-exploring-jenkins-pipelines-a-simple-delivery-flow

like image 28
pitchblack408 Avatar answered Oct 14 '22 04:10

pitchblack408