[英]How do I include a Gradle auto-incremented version number in my code's repo?
I have spring boot application in my github public reposity.我的 github 公共存储库中有 spring 启动应用程序。 I have used gradle as a build tool for this spring boot application.我已使用 gradle 作为此 spring 引导应用程序的构建工具。 I am using jenkins as CI/CD.我使用 jenkins 作为 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.我的 build.gradle 文件中有以下任务,该文件用于自动增加内部版本号,以便我生成的可执行文件 jar 在生成的 jar 文件中具有唯一的版本名称。
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.我在 jenkins 中调用此任务,如下所示。
"versionIncr bootJar docker --warning-mode=all" “versionIncr bootJar docker --warning-mode=all”
this task is working perfectly.这项任务运行良好。 As a result of this task below is happening in jenkins server由于此任务在 jenkins 服务器中发生
versionIncr
is getting executed and incrementing the version number and updating "gradle.properties"
file in the workspace that is there in jenkins server task => versionIncr
正在执行并增加版本号并更新 jenkins 服务器中的工作区中的"gradle.properties"
文件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.问题::对“ gradle.properties
”文件所做的更改保留在 jenkins 服务器工作区中,并且更新的版本号不会反映在 git 集线器分支中。 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.由于 jenkins 在本地进行了更改,因此当我将任何更改推送到 github 并运行 jenkins 作业时, "gradle.properties"
文件中的版本号将保持不变。 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.我希望 jenkins 为我处理版本更改。
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.有什么方法或 gradle 插件或 jenkins 插件可以用来将修改后的"gradle.properties"
文件从jenkins
工作区推回"github"
存储库。 Also if possible i would like to know the way of achieving using either github username/password
or by using SSH
.另外,如果可能的话,我想知道使用github username/password
或使用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 文件以防万一有人对我的操作方式感兴趣。 build.gradle 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())
}
The nebula.release Gradle plugin is able to auto-increment versions by using git tags. nebula.release Gradle 插件能够使用 git 标签自动增加版本。 To get started:开始:
plugins {
id 'nebula.release' version '15.0.0' // or current latest
}
The plugin adds several tasks for the type of release and options for which version component to increment.该插件为发布类型添加了几个任务以及要增加哪个版本组件的选项。 By default it will increment the minor version.默认情况下,它将增加次要版本。 Here's an example of how versions will be incremented on a single branch:这是一个如何在单个分支上增加版本的示例:
$ ./gradlew final # releases 0.1.0; tags repo v0.1.0
$ ./gradlew final # releases 0.2.0; tags repo v0.2.0
$ ./gradlew final -Prelease.scope=minor # releases 0.2.1; tags repo v0.2.1
Since the version is incremented after finding the previous git tag, the starting version can be adjusted by manually creating a git tag on a recent commit.由于在找到之前的 git 标签后版本会增加,因此可以通过在最近的提交上手动创建 git 标签来调整起始版本。
Caveats:注意事项:
If this doesn't meet your use-case, there are many more versioning plugins to choose from on the Gradle Plugin Portal.如果这不符合您的用例,在 Gradle 插件门户上还有更多版本控制插件可供选择。
Version numbers are barely auto -increment, but build numbers are.版本号几乎不是自动递增的,但内部版本号是。
Jenkins exposes it's own auto-increment ${BUILD_NUMBER}
per project. Jenkins 为每个项目公开了它自己的自动增量${BUILD_NUMBER}
。
With System.getenv("BUILD_NUMBER")
it can be obtained at build-time:使用System.getenv("BUILD_NUMBER")
它可以在构建时获得:
// $BUILD_NUMBER only exists when building with Jenkins
project.ext.set('BUILD_NUMBER', System.getenv("BUILD_NUMBER") ?: "0")
Yesterday with Google Cloud Build, I've also defined a BUILD_NUMBER
;昨天使用 Google Cloud Build,我还定义了BUILD_NUMBER
;
since I need to to differ in between manual builds and automatic builds.因为我需要区分手动构建和自动构建。
... different builders may provided difference environment variables. ...不同的构建器可能会提供不同的环境变量。
Jenkins usually builds when a branch has changed. Jenkins 通常在分支发生更改时构建。 Therefore one can update version.properties
files through Git (a local pre-commit
hook can increment).因此可以通过 Git 更新version.properties
文件(本地pre-commit
挂钩可以增加)。
In case you really want to increment the version on every commit to Github.如果您真的想在每次提交到 Github 时增加版本。
I have previously solved your proposed issue in 2 separate ways.我之前以两种不同的方式解决了您提出的问题。 Firstly, by using a Gradle plugin, similar to the nebula-release
plugin @sghill linked above.首先,通过使用 Gradle 插件,类似于上面链接的nebula-release
插件@sghill。
However, that plugin worked by counting all the commits for a patch version, configured major and minor via a Gradle extension and appended metadata information, eg branch name and whether it was dirty or not.但是,该插件通过计算补丁版本的所有提交来工作,通过 Gradle 扩展配置主要和次要版本,并附加元数据信息,例如分支名称以及它是否脏。 That seemed too complex a workflow for my needs and was not useful for projects that didn't use Gradle.对于我的需求来说,这似乎过于复杂的工作流程,并且对于不使用 Gradle 的项目没有用处。 For your case, however, it's a quick off the shelf solution.但是,对于您的情况,这是一个快速的现成解决方案。
In my case, all I needed were unique version numbers automatically tagged upon a PR being merged into develop
or master
, and unique version numbers for each commit on a branch.就我而言,我所需要的只是在 PR 合并到develop
或master
时自动标记的唯一版本号,以及分支上每个提交的唯一版本号。 To do so, I did use Git tags and wrote a script for it.为此,我确实使用了 Git 标签并为它编写了一个脚本。
The 3 cases for versioning were:版本控制的 3 种情况是:
version.json
have been changed, a major or minor bump occurs and the patch version is reset to 0.如果version.json
中的主要或次要版本已更改,则会发生主要或次要颠簸并将补丁版本重置为 0。git describe
and the branch name, eg 0.1.0-x-branch-name
where x
is the number of commits ahead of the default branch.分支上的唯一版本:git 的git describe
和分支名称,例如0.1.0-x-branch-name
其中x
是默认分支之前的提交数。You can find it here and the docker container here你可以在这里找到它和 docker 容器在这里
As for configuring Jenkins to have write access to the repo, have you followed the instructions here?至于将 Jenkins 配置为对 repo 具有写入权限,您是否按照此处的说明进行操作? This is what I've been successfully doing in all of my repos: Git push using jenkins credentials from declarative pipeline这是我在所有回购中成功完成的工作: Git push using jenkins credentials from declarative pipeline
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.