简体   繁体   中英

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.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM