7

I'm trying to create some Docker images. For that I want to use the version number specified in the Maven pom.xml file as tag. I am however rather new to the declarative Jenkins pipelines and I can't figure out how to change my environment variable so that VERSION contains the right version for all stages.

This is my code

#!groovy

pipeline {
    tools { 
        maven 'maven 3.3.9' 
        jdk 'Java 1.8' 
    }
    environment {
        VERSION = '0.0.0'
    }

    agent any 

    stages {
        stage('Checkout') { 
            steps {
                git branch: 'master', credentialsId: '290dd8ee-2381-4c5b-8d33-5631d03ee7be', url: 'git@gitlab.crosslang.local:company/SOME-API.git'
                sh "git clean -f && git reset --hard origin/master"
            }
        }
        stage('Build and Test Java code') {
            steps {
                script {
                    def pom = readMavenPom file: 'pom.xml'
                    VERSION = pom.version
                }
                echo "${VERSION}"
                sh "mvn clean install -DskipTests"
            }
        }
        stage('Build Docker images') {
            steps {
                dir('whales-microservice/src/main/docker'){
                    sh 'cp ../../../target/whales-microservice-${VERSION}.jar whales-microservice.jar'
                    script {
                        docker.build "company/whales-microservice:${VERSION}"
                    }
                }
            }
        }
    }
}
Jacob Stamm
  • 1,660
  • 1
  • 29
  • 53
Bram Vandewalle
  • 1,624
  • 3
  • 20
  • 29
  • What is exactly your problem? The VERSION in stage 'Build Docker images' doesn't contain the version number which was assigned in `VERSION = pom.version`? – haschibaschi May 09 '17 at 18:23
  • 1
    That is indeed the problem. It is only available in the 'Build and Test Java code' stage. – Bram Vandewalle May 10 '17 at 07:02

2 Answers2

12

The problem is the single quote of the statement

sh 'cp ../../../target/whales-microservice-${VERSION}.jar whales-microservice.jar'

single quotes don't expand variables in groovy: http://docs.groovy-lang.org/latest/html/documentation/#_string_interpolation

so you have to double quote your shell statement:

sh "cp ../../../target/whales-microservice-${VERSION}.jar whales-microservice.jar"
haschibaschi
  • 2,734
  • 23
  • 31
  • 2
    This highlights the difference between Strings and GStrings (Groovy Strings). Variables are interpolated in GStrings (double quotes) and not in Strings (single quotes). – user944849 Jan 25 '18 at 17:26
0

I just wanted to mention that if you have pipeline-utility-steps plugin installed you can use readMavenPom() in the environment part, too. It looks like this:

environment {
    VERSION = readMavenPom().getVersion()
}
Tobske
  • 518
  • 3
  • 12
  • this is not working java.lang.NoSuchMethodError: No such DSL method 'readMavenPom' found among steps [archive, bat, build, – ImranRazaKhan Jul 06 '18 at 21:14
  • 1
    @ImranRazaKhan Do you have the pipeline-utility-steps plugin installed? See [here](https://stackoverflow.com/questions/37197013/cannot-use-readmavenpom-in-jenkinsfile) – Tobske Jul 11 '18 at 16:24