I have spring boot application in my github public reposity. I have used gradle as a build tool for this spring boot application. I am using jenkins as CI/CD.
I have below task in my build.gradle file which is used to auto-increment the build number so that my generated executable jar will have unique version name in the generated jar file.
task versionIncr {
Properties props = new Properties()
File propsFile = new File('gradle.properties')
props.load(propsFile.newDataInputStream())
Integer nextbuildnum = ( ((props.getProperty('artifactBuildNumber')) as BigDecimal) + 1 )
props.setProperty('artifactBuildNumber', nextbuildnum.toString())
props.store(propsFile.newWriter(), null)
props.load(propsFile.newDataInputStream())
}
i am calling this task in jenkins as below.
"versionIncr bootJar docker --warning-mode=all"
this task is working perfectly. As a result of this task below is happening in jenkins server
- jenkins pulling the git $branch into the jenkins server workspace
- task =>
versionIncr
is getting executed and incrementing the version number and updating"gradle.properties"
file in the workspace that is there in jenkins server - generating executable jar file
- creating docker image with the newly generated executable jar file
problem::
the changes made to "gradle.properties
" file are left in the jenkins server workspace and the updated version number will not gets reflected in the git hub branch. since jenkins made changes locally so when i push any changes to the github and run the jenkins job then version number in "gradle.properties"
file will still remains same. I do not want to update the version number manually each time i push my commits. I want jenkins to handle the version change for me.
is there any way or gradle plugin or jenkins plugin which i can use to push the modified "gradle.properties"
file from jenkins
workspace back to "github"
repository.
Also if possible i would like to know the way of achieving using either github username/password
or by using SSH
.
let me know if i need to post any more information here.
Update:: Posting my build.gradle file just in case if some one is interested in how i am doing it. build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
}
}
plugins {
id 'org.springframework.boot' version '2.2.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'maven-publish'
id 'com.palantir.docker' version '0.25.0'
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
//apply plugin: 'io.spring.gradle.dependencymanagement.DependencyManagementPlugin'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
//apply plugin: 'org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin'
group 'com.javasree'
version project.properties.containsKey("releaseVersion") ? "${artifactMajorVersion}" : "${artifactMajorVersion}-${artifactBuildNumber}"
sourceCompatibility = 1.8
ext {
springCloudVersion ='Greenwich.RELEASE'
artifactName ='<artifact>'
artifactory = 'http://localhost:8081/artifactory/'
artifactoryRepo = 'gradle-lib-release'
artifactorySnapShotRepo = 'gradle-lib-snashot'
artifactoryRepo3pp = 'pub-gradle-remote'
artifactoryUser = System.getProperty("user", "")
artifactoryPassword = System.getProperty("password", "")
}
repositories {
mavenCentral()
maven {
url "${artifactory}${artifactoryRepo3pp}"
allowInsecureProtocol = true
credentials { // Optional resolver credentials (leave out to use anonymous resolution)
username = "admin" // Artifactory user name
password = "password" // Password or API Key
}
}
}
publishing.publications {
maven(MavenPublication) {
artifact bootJar
// groupId 'gatewayengine'
// artifactId artifactName
// version '1.0-SNAPSHOT'
from components.java
}
}
publishing.repositories {
maven {
allowInsecureProtocol = true
credentials {
username = "admin" // Artifactory user name
password = "password" // Password or API Key
}
if(project.version.endsWith('-SNAPSHOT')) {
url "${artifactory}${artifactorySnapShotRepo}"
} else {
url "${artifactory}${artifactoryRepo}"
}
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
//mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
}
docker {
name "localhost:5000/${project.name}:${project.version}"
files tasks.bootJar.outputs
//tag 'localhost:5000/${project.name}:${project.version}'
dockerfile file('Dockerfile')
//buildArgs([HOST_APP_JAR_LOC: 'version'])
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web',
'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.2.2.RELEASE',
'org.springframework.cloud:spring-cloud-starter-netflix-zuul:2.2.2.RELEASE'
}
task versionIncr {
Properties props = new Properties()
File propsFile = new File('gradle.properties')
props.load(propsFile.newDataInputStream())
Integer nextbuildnum = ( ((props.getProperty('artifactBuildNumber')) as BigDecimal) + 1 )
props.setProperty('artifactBuildNumber', nextbuildnum.toString())
props.store(propsFile.newWriter(), null)
props.load(propsFile.newDataInputStream())
}