Home > Mobile >  How to read and increment the application version from/to gradle
How to read and increment the application version from/to gradle

Time:11-07

I have a Jenkinsfile where I read the current version of the application from gradle version attribute. Based on this version, a docker image is built and pushed to a remote repository.

However, I don't know how to 1. Increment the application version and 2. Push that change to the repository. This is my current 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 $2}\'').trim()
                 buildName = sh(returnStdout: true, script: 'cat gprops |grep name:|awk \'{print $2}\'').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

CodePudding user response:

I think you should add gradle task to do so for example

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
}

Then you can call it with sh './gradlew increment' in your pipe , this should increase the version from 1.0.0 to 1.0.1 .

  • Related