jenkinsjenkins-pipelineversion-numbering

Not getting version number plugin value in Jenkins


I am using declarative pipeline syntax for my pipeline job in Jenkins for my project. I wanted to use

pipeline {
    agent any

    environment {
        VERSION = VersionNumber projectStartDate: '', versionNumberString: '${BUILD_YEAR}.${BUILD_MONTH}.${BUILDS_TODAY}.${BUILD_NUMBER}', versionPrefix: 'v1.', worstResultForIncrement: 'SUCCESS'
    }

    stages {
        stage('Version Update'){
            steps{
                echo '${VERSION}'
                writeFile file: 'version.ini', text: '%VERSION%'
            }
        }
    }
}

I tried ${VERSION},%VERSION to print the version number, but it always print whats inside the echo, text inside writeFile step. (eg %VERSION%)

I am able to see the version in the side menu with the format I used.

enter image description here


Solution

  • In groovy,strings that use single quotes ' don't get interpolated. You should use double quotes instead, and use $ in front of each variable you want to get replaced (if you want to keep a $ in a string you need to escape it with \).

    For writeFile it's a pipeline command, so it runs as groovy on the jenkins master and not on a build node. That's why you need to treat it as such (double quotes and $). pipeline { agent any

        environment {
            VERSION = VersionNumber projectStartDate: '', versionNumberString: "${BUILD_YEAR}.${BUILD_MONTH}.${BUILDS_TODAY}.${BUILD_NUMBER}", versionPrefix: 'v1.', worstResultForIncrement: 'SUCCESS'
        }
    
        stages {
            stage('Version Update') {
                steps {
                    echo "${VERSION}"
                    writeFile file: 'version.ini', text: "$VERSION"
                }
            }
        }
    }
    

    Note: I don't use the version number plugin, so I wasn't able to test this exact code