Issue
I am using spring boot maven plugin to generate my docker images.
I defined the image name to be dynamic in my pom file. Format: my-registry.com/prefix/${project.artifactId}:${project.version}
How can i get the dynamic name of the image generated? I need it for further build steps (deployment)
My build pipeline looks like this:
pipeline {
agent {
docker {
image 'maven:3-jdk-11'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('Clone sources') {
steps {
git branch: 'master',
credentialsId: 'xxxxx',
url: 'xxxxxx'
}
}
stage('Build') {
steps {
sh "mvn -Dmaven.test.failure.ignore=true clean install"
}
post {
success {
junit '**/target/surefire-reports/TEST-*.xml'
archiveArtifacts 'myproject-server/target/*.jar'
}
}
}
stage('Build and Push Docker Image') {
steps {
withCredentials([usernamePassword(credentialsId: 'xxxxx', passwordVariable: 'NEXUS_PASSWORD', usernameVariable: 'NEXUS_USER')]) {
sh "mvn -pl myproject-server -DskipTests=true spring-boot:build-image -DDOCKER_REGISTRY=xxxx -DDOCKER_REGISTRY_USER=$NEXUS_USER -DDOCKER_REGISTRY_PASSWORD=$NEXUS_PASSWORD"
}
}
}
stage('Deploy to DEV') {
steps {
sshagent(credentials : ['xxxx']) {
sh 'ssh -o StrictHostKeyChecking=no user@server DOCKER RUN LOGIC HERE WITH CORRECT IMAGE NAME'
}
}
}
}
}
Solution
You could use Jenkins Pipeline Utility Steps plugin (https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/) to read the POM file and extract the value.
pom = readMavenPom file: 'pom.xml'
pom.version
But as stated in their docs
Avoid using this step and writeMavenPom. It is better to use the sh step to run mvn goals. For example
You could use this command instead:
def version = sh script: 'mvn help:evaluate -Dexpression=project.version -q -DforceStdout', returnStdout: true
Answered By - warch
Answer Checked By - Mary Flores (JavaFixing Volunteer)