Jib plugin not able to access project.version as updated by another Gradle plugin
I have my build.gradle
setup as follows (listing only plugins for brevity)
plugins {
id 'java'
id 'maven-publish'
id 'signing'
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'com.limark.gitflowsemver' version '0.3.1'
id 'com.google.cloud.tools.jib' version '1.8.0'
}
...
group = 'com.app.my'
// The below line remains commented
// version = '0.1.0'
...
jib {
from {
image = 'azul/zulu-openjdk-alpine:11-jre'
}
to {
image = 'aws_account_id.dkr.ecr.region.amazonaws.com/my-app'
tags = [version]
}
container {
format = 'OCI'
}
}
publishing {
repositories {
maven {
def releasesRepoUrl = "http://localhost:8081/repository/maven-releases/"
def snapshotsRepoUrl = "http://localhost:8081/repository/maven-snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username nexusUsername
password nexusPassword
}
}
}
publications {
mavenJava(MavenPublication) {
artifactId = 'my-app'
from components.java
pom {
name = 'My App'
description = 'My App'
url = 'https://my-app.com'
developers {
developer {
id = 'john'
name = 'John Doe'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:ssh://[email protected]:acme/my-app.git'
developerConnection = 'scm:git:ssh://[email protected]:acme/my-app.git'
url = 'https://bitbucket.org/acme/my-app'
}
}
}
}
}
signing {
sign publishing.publications.mavenJava
}
The gitflowsemver
plugin updates the project.version
based on the GitFlow branching strategy. The publish
task is able to access the version as updated by the gitflowsemver
plugin, but when I try to build a docker image using jib
, it does not tag the image with the updated version. It is always tagged as unspecified
. But if I un-comment the line version = '0.1.0'
the jib
plugin is able to pick up the version. I am unable to understand why. Any help is highly appreciated.
Solution 1:
Update: Jib 2.6.0 now has support for late evaluation of jib.to.image
and jib.to.tags
. You can configure these using project.provider
and they will only be evaluated when they are used.
jib {
...
to {
image = 'rishabh9/jib-demo'
tags = project.provider{[version]}
}
...
}
For older versions of jib you can try the previous answer:
From the solution discussed on gitter.im/google/jib
Looking into the code of the gitsemver plugin you can see that it updates the version after the project is evaluated: https://github.com/OpenLimark/GitFlowSemVerPlugin/blob/develop/src/main/groovy/com/limark/open/gradle/plugins/gitflowsemver/GitFlowSemVerPlugin.groovy#L51
So what you can do is just set your tag version in an afterEvaluate block and you'll correctly pick up the version:
jib {
...
to {
image = 'rishabh9/jib-demo'
project.afterEvaluate { // <-- so we evaluate version after it has been set
tags = [version]
}
}
...
}