jib icon indicating copy to clipboard operation
jib copied to clipboard

Feature request: Docker semantic tagging in Gradle plugin

Open djechelon opened this issue 4 years ago • 1 comments

This is a feature request.

I am currently actively working to fully implement Docker semantic tagging as documented.

To illustrate it short, given your project has version 1.0.1, I want to create Docker tags for the following tags:

  • 1
  • 1.0
  • 1.0.1

On the next patch release, 1.0.2, the two early tags should be overwritten, while the third retained.

Currently, I wrote some Groovy code that parses the version object (computed by reckon)

/**
 * Parses semantic version into multiple bindable versions
 *
 * Examples
 *
 * <pre>1.0.15</pre>
 * <pre>
 *     1
 *     1.0
 *     1.0.15
 * </pre>
 *
 *
 * <pre>1.4.13-SNAPSHOT</pre>
 * <pre>
 *     1-SNAPSHOT
 *     1.4-SNAPSHOT
 *     1.4.13-SNAPSHOT
 * </pre>
 *
 * @param version
 */
static def parseSemantic(String version) {
    int indexOfDash = version.indexOf('-')
    def versionType = indexOfDash < 0 ? '' : version.substring(indexOfDash)
    def ret = []
    for (def indexOfDot = 0; indexOfDot > -1; indexOfDot = version.indexOf('.', indexOfDot + 1)) {
        if (indexOfDot == 0) continue
        ret.push("${version.substring(0, indexOfDot)}$versionType")
    }
    ret.push(version)
    ret.push("latest${versionType}")
    ret
}


task jibDisplaySemanticVersions {
    description 'Prints semantic versions for Docker tags'
    group 'jib'
    doLast {
        parseSemantic(version.toString()).forEach {
            println "Version >> $it"
        }
    }
}

Example usage in a Spring Boot project

jib {
    to {
        image = 'xxxxx'
        tags = parseSemantic(version.toString())
    }
    container {
        ports = ['8080']
        environment = [
                SPRING_OUTPUT_ANSI_ENABLED: 'ALWAYS',
        ]
        creationTime = 'USE_CURRENT_TIMESTAMP'
    }
}

I believe that Semantic tagging is a very good idea to maintain images. I recommend the jib team to implement such a feature in the Maven and Gradle plugins

djechelon avatar Jan 11 '22 17:01 djechelon

Note: my code works with -SNAPSHOT versions too

djechelon avatar Jan 11 '22 17:01 djechelon