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"
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.
You can use grep command to suck the version number with it.
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).
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
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
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