如何读取和递增应用程序版本 from/to gradle

How to read and increment the application version from/to gradle

我有一个 Jenkinsfile,我从 gradle 版本属性中读取应用程序的当前版本。基于这个版本,构建了一个docker镜像并推送到远程仓库。

但是,我不知道如何 1. 增加应用程序版本和 2. 将该更改推送到存储库。这是我当前的 Jenkinsfile:

pipeline {
    agent any
    stages {
        stage("build") {
            steps {
               sh './gradlew -q properties > gprops'
               script {
                 buildVersion = sh(returnStdout: true, script: 'cat gprops |grep version:|awk \'{print }\'').trim()
                 buildName = sh(returnStdout: true, script: 'cat gprops |grep name:|awk \'{print }\'').trim()
               }
               sh './gradlew clean build -x test'
            }
        }

        stage("build image") {
            steps {
                script {
                    echo 'building the docker image'
                    withCredentials([
                        usernamePassword(credentialsId: 'docker-hub-repo', usernameVariable: 'USER', passwordVariable: 'PASSWORD')
                    ]) {
                        sh "docker build -t somename/${buildName}:${buildVersion} ."
                        sh "echo $PASSWORD | docker login -u $USER --password-stdin"
                        sh "docker push somename/${buildName}:${buildVersion}"
                    }
                }
            }
        }

    }
}

aa

我认为你应该添加 gradle 任务来这样做

version='1.0.0'  //version we need to change

task increment<<{
    def v=buildFile.getText().find(version) //get this build file's text and extract the version value
    String minor=v.substring(v.lastIndexOf('.')+1) //get last digit
    int m=minor.toInteger()+1                      //increment
    String major=v.substring(0,v.length()-1)       //get the beginning
    //println m
    String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+m+"'")
    //println s
    buildFile.setText(s) //replace the build file's text
}

然后你可以用 sh './gradlew increment' 在你的管道中,这应该将版本从 1.0.0 增加到 1.0.1。